Encoding/Decoding hex packet

Posted by Roberto Pulvirenti on Stack Overflow See other posts from Stack Overflow or by Roberto Pulvirenti
Published on 2012-06-24T21:13:51Z Indexed on 2012/06/24 21:15 UTC
Read the original article Hit count: 282

Filed under:
|
|
|

I want to send this hex packet:

00 38 60 dc 00 00 04 33  30 3c 00 00 00 20 63 62 
39 62 33 61 36 37 34 64  31 36 66 32 31 39 30 64 
30 34 30 63 30 39 32 66  34 66 38 38 32 62 00 06 
35 2e 31 33 2e 31 00 00  02 3c

so i build the string:

string packet = "003860dc0000" + textbox1.text+ "00000020" + textbox2.text+ "0006" + textbox3.text;

then "convert" it to ascii:

conn_str = HexString2Ascii(packet);

then i send the packet... but i have this:

00 38 60 **c3 9c** 00 00 04 33  30 3c 00 00 00 20 63 
62 39 62 33 61 36 37 34 64  31 36 66 32 31 39 30 
64 30 34 30 63 30 39 32 66  34 66 38 38 32 62 00 
06 35 2e 31 33 2e 31 00 00  02 3c **0a**

why?? Thank you! P.S. the function is:

private string HexString2Ascii(string hexString)
    {
        byte[] tmp;
        int j = 0;
        int lenght;
        lenght=hexString.Length-2;
        tmp = new byte[(hexString.Length)/2];
        for (int i = 0; i <= lenght; i += 2)
        {
            tmp[j] =(byte)Convert.ToChar(Int32.Parse(hexString.Substring(i, 2), System.Globalization.NumberStyles.HexNumber));
            j++;
        }
        return Encoding.GetEncoding(1252).GetString(tmp);
    }

© Stack Overflow or respective owner

Related posts about c#

Related posts about hex