Connecting Tech Pros Worldwide Help | Site Map

Display a byte as two hex digits

Moderator
 
Join Date: Oct 2006
Location: Australia
Posts: 7,748
#1   Jul 11 '07
Using Hex$() you can easily convert a byte (or larger value) to a hex representation. However, it comes out as one or two bytes, which can mess up your text alignment. This simple function provides a consistent two-digit hex string.
Expand|Select|Wrap|Line Numbers
  1. Public Function HexByte2Char(ByVal Value As Byte) As String
  2.   ' Return a byte value as a two-digit hex string.
  3.   HexByte2Char = IIf(Value < &H10, "0", "") & Hex$(Value)
  4. End Function



Newbie
 
Join Date: Sep 2007
Posts: 21
#2   Sep 14 '07

re: Display a byte as two hex digits


Quote:

Originally Posted by Killer42

Using Hex$() you can easily convert a byte (or larger value) to a hex representation. However, it comes out as one or two bytes, which can mess up your text alignment. This simple function provides a consistent two-digit hex string.

Expand|Select|Wrap|Line Numbers
  1. Public Function HexByte2Char(ByVal Value As Byte) As String
  2.   ' Return a byte value as a two-digit hex string.
  3.   HexByte2Char = IIf(Value < &H10, "0", "") & Hex$(Value)
  4. End Function

How do you convert hex2byte?
Moderator
 
Join Date: Oct 2006
Location: Australia
Posts: 7,748
#3   Sep 15 '07

re: Display a byte as two hex digits


Quote:

Originally Posted by aviraldg

How do you convert hex2byte?

Can you explain what you mean? If you just want to convert a hex string to a byte value, try something like Val("&h" & <your-value>).



In future, please don't ask questions here in the Articles area. They should go in the main forum.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#4   Sep 15 '07

re: Display a byte as two hex digits


Quote:

Originally Posted by Killer42

Can you explain what you mean? If you just want to convert a hex string to a byte value, try something like Val("&h" & <your-value>).



In future, please don't ask questions here in the Articles area. They should go in the main forum.

This is exactly the reason I'd prefer articles in this section to be closed.

kind regards,

Jos
Reply


Similar Visual Basic 4 / 5 / 6 bytes