Main Site Documentation

Convert Byte to Hex


#1

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 ?

Thanks


#2

We have some codeshare entries for that.

Here is one of them:
https://www.ghielectronics.com/community/codeshare/entry/46


#3

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 !


#4

Like this?

Dim b As Byte = 100

Debug.Print(b.ToString("X2"))

Btw, that codeshare is working for me.


#5

@ iamin - Right. Forgot about it.

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


#6

And I found a bug…

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


#7

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


#8

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


#9

Can anyone confirm?


#10

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


#11

:think:


#12

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


#13

Can you try this:

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

I am getting:

[quote]C1

FFFFFFC1[/quote]


#14

Yep, confirmed.


#15

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.


#16

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

        public static void Main()
        {
            Debug.Print(Resources.GetString(Resources.StringResources.String1));
            Byte b=193;
            string f = 193.ToString("X2");
            Debug.Print(f);
            Debug.Print(System.Math.Abs(b).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!
C1
C1


#17

@ 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.


Debug.Print((int)b).ToString("X2"));


#18

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


#19

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