Converting to Byte problem

hi, i have a problem in converting to byte and i hope if anyone can help me  in this

the problem in the delimiter that i use to separate fields from each other i insert it as Hex but when i convert to byte i get wrong hex value 

here  its my code


string _delma = "\xC0\x80";
System.Collections.Specialized.
NameValueCollection nv = new System.Collections.Specialized.NameValueCollection();
nv.Add(
"1", "shakalama2");
StringBuilder sb = new StringBuilder();
foreach (string key in nv)
{
sb.Append(key).Append(_delma).Append(nv[key]).Append(_delma);
}
byte[] data = Encoding.ASCII.GetBytes(sb.ToString());
Console.WriteLine(BitConverter.ToString(data));


 

item               key delimiter    value                                          delimiter   

expected     -31  -C0-80 -73-68-61-6B-61-6C-61-6D-61-32  -C0-80

real               -31  -3F-3F -73-68-61-6B-61-6C-61-6D-61-32  -3F-3F

N.P. i use stringbuilder to know the byte array length from the length i creat the array length, i depending on string to be able to split the packet when i recieve one, but that doesn't work too


static Regex splitter = new Regex(_delma);

string[] dataItems = splitter.Split(strdata);

string strdata = Encoding.ASCII.GetString(data, 20, _Pkt_Len);
string[] dataItems = splitter.Split(strdata);
_Data.Clear();
for (int i = 0; i < dataItems.Length; i += 2)
{
_Data.Add(dataItemsIdea, dataItems[i +
1]);
}

 

i must use those 2 bytes in particular as delimiter, but seems teh string builder is wrong choice even when i enter the value as hex

how can i solve this

thx in advance

 




Answer this question

Converting to Byte problem

  • Nat Raja

    hi,

    this is authentication packet, i need the byte sequence here i can't use unicode when the server use AscII

    best regards



  • shnacooti

    Yes, use the Unicode property, not ASCII.

  • Natiq

    Then don't store it in a String.  String is Unicode (two bytes per character) you have to use Encoding.Unicode to convert it to bytes properly.  If you're dealing with bytes, use a byte array instead of String.

  • urraca

    C# strings are UNICODE strings, not ASCII strings. Change the Encoding.ASCII.GetBytes() to Encoding.Unicode.GetBytes().


  • The_Gangster

    hi,

    yes i know C# strings are unicode and this is the problem now , this string for network packets , unfortunatly the network packets use the AscII not the unicode. 

     AcsII use one byte

    -31  -c0-80

     but  unicdoe use 2 bytes so, if i used the unicode the byte order will be

    -31-00  -c0-00-80-00

    and the server will not recognize it, so i need to do this in ASCII

    thx



  • Converting to Byte problem