Click here to Skip to main content
15,888,009 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi everyone

How can I send this packet?
{68}{31}{39}{33}{32}{32}{37}{16}

What I have tried:

C#
string serialnumber = "193227";
byte[] k = Encoding.Default.GetBytes(serialnumber);
var hexString = BitConverter.ToString(k); //My string converted to hex: {31}{39}{33}{32}{32}{37}

byte start = 0x68;
int middle = hexString;
byte end = 0x16;
byte[] packet = new byte[] { start, middle, end };

ns.Write(packet, 0, packet.Length);


Finaly I want to send: {68}{31}{39}{33}{32}{32}{37}{16}
thanks for help
Posted
Updated 18-Oct-16 0:07am
Comments
#realJSOP 18-Oct-16 6:10am    
Why aren't you just doing this?

string serialNumber = "h193227" + char(22);
byte[] packet = Encoding.Default.GetBytes(serialNumber);
ns.Write(packet, 0, packet.Length);
Michael Corbett 24-Jan-22 19:16pm    
Yep! No need to reinvent the wheel. This is a simpler solution.

1 solution

That is unlikely to be anything like what you want - because the BitConverter.ToString Method (Byte[]) (System)[^] mocverts an array of byte values to a string of hex digits, separated by hyphens:
00-01-02-04-08-10-20-40-80
And it's a lot, lot more likely that you destination needs the raw hex values - which you already have inside the byte array.
Try this:
C#
private const byte start = 0x68;
private const byte end = 0x16;
private byte[] Enpack(byte[] data)
    {
    int packetLength = 1 + data.Length + 1;
    byte[] packet = new byte[packetLength];
    packet[0] = start;
    packet[packetLength - 1] = end;
    Array.Copy(data, 0, packet, 1, packetLength - 2);
    return packet;
    }

And you might want to replace the Default encoding with a specific set: Ascii perhaps, or UTF8 depending on the destination.
 
Share this answer
 
Comments
versers777 18-Oct-16 6:27am    
thanks for answer
i get this error:
Cannot convert method group 'Enpack' to non-delegate type 'byte'.
OriginalGriff 18-Oct-16 6:37am    
That's probably because you are trying to store the result in a byte, not a byte array...or calling it without brackets...
Member 12855870 15-Sep-22 3:42am    
did you get answer this question ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900