c# - Can't convert my string to hex representation -
i'm receiving data using serial port, , i'm use following code convert string hex representation , show in richtextbox5:
string hex = "";             foreach (char c in rxstring)             {                 uint tmp = c;                 hex += string.format("{0:x2}", (uint)system.convert.toint16(tmp.tostring())) ;             }              richtextbox5.appendtext(hex + "   <= hex");   where rxstring store data serial port. problem : when send data 127(decimal)=> 01111111(binary)=> 7f(hex) converted correctly, while when send data 191 or 167 share significant bit 1 , 8 bits output 3f despite other bits, (the representation of 8 bits start 1 3f), whats wrong code? can help, thx.
this example of using bytes - , seems work want:
        string hex = "";         byte[] rxstring = { 0xff, 0xcf, 0xb8, 167,191 };         foreach (byte c in rxstring)         {             uint tmp = c;             hex += string.format("{0:x2}", (uint)system.convert.toint16(tmp.tostring()));         }          system.console.writeline("{0}   <= hex", hex);      
Comments
Post a Comment