Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Format Function and Hexadecimal Representation

Status
Not open for further replies.

BobRaney

Programmer
Nov 22, 2001
59
US
Hello,

I would like to Format the Result of the Hex function formatted to represent the type of variable that I pass to it, including high order zeros and the space between the words.

Examples:

Dim MyLong as long
MyLong = 15
The return of Hex(MyLong) is F.
I would like to see this as: 0000 000F

Dim MyInteger as Integer
MyInteger = 15
I would like to see this as: 000F

Dim MyByte as Byte
MyByte = 15
I would like to see this as: 0F

It is the Format Function that I am having trouble with.
Format(Hex(MyLong),"Whatever to get the 0000 000F")

Thanks,

Raney
 
I encourage you to read this:

thread222-1084219



-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Thanks,

Just what I was looking for.

Format(Right("00000000" & Hex(11259375),8),"@@@@ @@@@")

Gives Me:00AB CDEF

Raney

 
George,

Thankyou again for pointing me to that thread.

During supper I thought that I should use something like this to Set/Get High/Low or any character or set of characters of numeric variables.

I think that it will be a lot easier to read and work with than my current function that does this.

When I have finished what I am working on I will put a simple function together, test, time and post the results.

Raney
 
You can also write a single function with a variant argument. The function will see which type of argument is passed and format its output accordingly.
___
[tt]
Private Sub Form_Load()
Dim B As Byte, I As Integer, L As Long
B = 15: I = 15: L = 15
Debug.Print Hex(B), Hex(I), Hex(L)
Unload Me
End Sub

Private Function Hex(Number As Variant) As String
Hex = VBA.Hex$(Number)
Select Case VarType(Number)
Case vbByte
Hex = Right$("0" & Hex, 2)
Case vbInteger
Hex = Right$("000" & Hex, 4)
Case vbLong
Hex = Format$(Right$("0000000" & Hex, 8), "@@@@ @@@@")
End Select
End Function[/tt]
 
Ah! I see that George has already posted a similar solution in the other thread.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top