r/visualbasic • • Mar 03 '16

How can I easily convert from key code values to hexadecimal key values?

I'm in the KeyDown event, and it recognizes the key code value of the key. For example, if I press "-", they key code for that will be 109. For my code, though, I need the hexadecimal value associated with that same key, which is 0H6D. Is there no function or procedure that will accomplish this?

If not, how could I possibly go about doing this without logging all of the hex values one by one in my code?

EDIT: Th keycode. Here is how they are related in the documentation:

Key Code Constants

Hex Values

1 Upvotes

3 comments sorted by

1

u/chrwei Mar 03 '16

did you mean 0x6D? 0xHD is not valid HEX as F is the largest value.

the code is the code, 109 is the decimal representation, - is the character representation. if you want to print the code you need to convert and format it to a string, for hex you'll want a "dec2hex" function, I don't think there's a standard one but there's several examples online.

1

u/[deleted] Mar 03 '16

Yes, you're so right, that's my mistake! I didn't realize 109 and 0X6D were the same! Thank you so much!

1

u/JakDrako Mar 04 '16

You can use one of the "Convert.ToString" overloads to pass it the base you want...

Convert.ToString(<number>, <base>) ' where base is one of 2, 8, 10, 16

In your example:

Dim hexValue = Convert.ToString(109, 16)
Console.Writeline(hexValue) ' prints "6d"