Hex representation of a byte

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ThatThatGuy
    Recognized Expert Contributor
    • Jul 2009
    • 453

    Hex representation of a byte

    I have a byte value which i want to convert to hex to transport it to a memory stream..

    something like this
    e.g. For A hex value- 0x41

    I've tried but the only output i get is 41 but i want it to be 0x41
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    What is your application here? Like, why do you need to have the 0x?

    Without knowing more about what you're trying to do, I'd say you don't really need to worry about it. The 0x is just a device to tell humans that the number is a hex representation. If your data is 41 and you know it's hex, you don't need the 0x. If you need 0x for output purposes, just add it as a string prefix when you output.

    Code:
    Console.WriteLine("0x" + data.ToString());
    Sorry if that doesn't really help...
    Last edited by GaryTexmo; Jun 2 '10, 03:00 PM. Reason: Clarity

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      Example:
      [code=c#]
      byte b = 42;
      string s="0x"+((int)b) .ToString("X2") ;
      [/code]

      Comment

      Working...