Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert integer to 4-digit hex string?

Tags:

vb.net

vba

I need to convert a 2-byte signed integer into a string of it's hex equivalent, but I need the string to be 4 characters. I've tried the Hex() function but when I convert 0 or say, 10, the result is 0 or A. I need the result to be 0000 or 000A. Any tips or advice?

like image 351
C Lampo Avatar asked Oct 22 '25 06:10

C Lampo


1 Answers

It is just

 Dim hexS As String = i.ToString("X4")

This is well explained in the The Hexadecimal (X) Format specifier.
The 4 after the X specifies how many chars you want in the output and if there are not enough chars, the missing ones are supplied as an appropriate number of "0".

like image 92
Steve Avatar answered Oct 23 '25 22:10

Steve



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!