Convert Byte to Hex

Hi guys,

I know that this might be something basic but I can’t find any working solution. I have a byte value that I need converted into hexadecimal.

Could you please point me in the right direction ?


We have some codeshare entries for that.

Here is one of them:

1 Like

Been there, tried that and I got this:

The name ‘hex’ does not exist in the current context

and couldn’t find the right solution … I’m afraid some things just elude me.

LE: works better if I copy everything :))

Thanks, problem solved !

Like this?

Dim b As Byte = 100


Btw, that codeshare is working for me.


@ iamin - Right. Forgot about it.

Custom solution is for old version of NETMF. “X2” is supported now.

And I found a bug…

Any value from 128 to 255 including is not correctly shown when ToString(“X2”) is used.

@ iamin - are you sure ? 193 got me C1 which seems alright …

I am getting FFFFFFC1. With every value above 128 I get added those FFFFFF.

Can anyone confirm?

Tried these two:

static readonly string hex = "0123456789ABCDEF";

            /// <summary>
            /// Converts Byte to a Hexadecimal string, like .ToString("X2") would do;
            /// </summary>
            /// <param name="number">Byte to represent has a hexadecimal string.</param>
            /// <returns>2 character string of hexadecimal representation of the number</returns>
            public static string FromByteToHex(byte number)
                return new string(new char[] { hex[(number & 0xF0) >> 4], hex[number & 0x0F] });

        public static void Main()
            string s = FromByteToHex(193);
            string f = 193.ToString("X2");

Works both ways in emulator

1 Like


I wonder if it is VB.NET issue. That is the only thing that is different between our tries.

Can you try this:

byte b = 193;
string f = 193.ToString("X2");

I am getting:



Yep, confirmed.

I was going to report this bug, but apparently somebody did it already. Let’s hope MS addresses all these small bugs in the near future.

1 Like

This was the fix that I ended up using: (math.abs())

        public static void Main()
            Byte b=193;
            string f = 193.ToString("X2");

‘Microsoft.SPOT.Emulator.Sample.SampleEmulator.exe’ (Managed): Loaded ‘C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.3\Assemblies\le\mscorlib.dll’, Symbols loaded.
‘Microsoft.SPOT.Emulator.Sample.SampleEmulator.exe’ (Managed): Loaded ‘C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.3\Assemblies\le\Microsoft.SPOT.Native.dll’, Symbols loaded.
‘Microsoft.SPOT.Emulator.Sample.SampleEmulator.exe’ (Managed): Loaded ‘c:\users\mike\documents\visual studio 2013\Projects\MFConsoleApplication1\MFConsoleApplication1\bin\Debug\le\MFConsoleApplication1.exe’, Symbols loaded.
The thread ‘’ (0x2) has exited with code 0 (0x0).
Hello World!

@ mtylerjr - That is working because Math.Abs is promoting the byte to an int. You could also just cast the byte to an int which is a native IL op code and will not incur the cost of the function call overhead which is significant in .NETMF.


Well then. This is why I claimed you were a genius. Of course just casting it to int is better!

Thank you, but I can assure you I am no genius. Not even close, just ask my wife.

1 Like