C# Convert Char to Byte (Hex representation)

后端 未结 5 1277
半阙折子戏
半阙折子戏 2021-02-07 00:23

This seems to be an easy problem but i can\'t figure out.

I need to convert this character < in byte(hex representation), but if i use



        
5条回答
  •  旧时难觅i
    2021-02-07 00:54

    get 60 (decimal representation) instead of 3c.

    No, you don't get any representation. You get a byte containing the value 60/3c in some internal representation. When you look at it, i.e., when you convert it to a string (explicitly with ToString() or implicitly), you get the decimal representation 60.

    Thus, you have to make sure that you explicitly convert the byte to string, specifying the base you want. ToString("x"), for example will convert a number into a hexadecimal representation:

    byte b = Convert.ToByte('<');  
    String hex = b.ToString("x");
    

提交回复
热议问题