Hex

From REALbasicWiki

Jump to: navigation, search

Contents

[edit] Prototype

Function Hex(value as integer datatype) as String

[edit] Description

Hex returns a string containing the hexadecimal representation of the passed value.

[edit] Discussion

[edit] Sample Code

dim s as String = Hex(47)


[edit] Hex representation of a String

One sometimes needs to get the hex representation of a binary string; passing a hex representation of an MD5 hash, for example. REALbasic does not have a method for this; here is one that should suffice for most purposes.

Function Hex(s as String, separator as String = "") As String
#pragma disableBackgroundTasks

static lookupTable() as String = NewLookupTable

dim characters() as String = SplitB(s, "")
dim hexData() as String
redim hexData(UBound(characters))

for i as Integer = 0 to UBound(characters)
hexData(i) = lookupTable(AscB(characters(i)))
next
return Join(hexData, separator)
End Function


Private Function NewLookupTable() As String()
#pragma disableBackgroundTasks

dim table() as String
for i as Integer = 0 to 15
dim h as String = Hex(i)
for j as Integer = 0 to 15
table.Append h + Hex(j)
next
next

return table
End Function
Personal tools
related