Re: How to convert hex to string?

Tech-Archive recommends: Fix windows errors by optimizing your registry



Mika M wrote:
> ... and it's working fine. For example my name "MIKA" will be
> "4D494B41" as hex string, but I don't find out how to do this
> opposite way? I mean how to get "MIKA" of "4D494B41" hex string.

Here's one way to do it:

\\\
Private Function DecodeHex(ByVal HexString As String) As String
Dim thisChar As String
Dim ascii As Integer
Dim ret As String
'Keep going until we exhaust all the source string
Do While Len(HexString) > 0
'Get the next two-digit hex number
thisChar = HexString.Substring(0, 2)
'Remove this hex number from the source string
HexString = HexString.Substring(2)
'Get the value in decimal of this hex number
ascii = CInt(Val("&H" & thisChar))
'Convert it to a character
ret &= Chr(ascii)
Loop
'All done
Return ret
End Function
///

Call this with "4D494B41" as the HexString parameter value and it will
return "MIKA".

It works by using a handy feature of the Val() command. If you pass a number
prefixed with "&H", it will treat that as a hex number when it parses it. So
if you ask it for Val("&H10"), it will return 16.

The code simply loops through each pair of characters (each 8-bit hex value)
and decodes the value to a number. It then gets the ASCII character
represented by this number.

There's no validation or anything so you'll need to add that yourself if
there's a chance of passing non-hex values to the function.

Hope that helps,

--

(O) e n o n e


.



Relevant Pages

  • Re: PCL images and mvEnterprise
    ... decimal then CHAR to get the Ascii character. ... to print the letter "A" {'A' is hex 41}, ... or by instead sending the equivalent hex pairs string ...
    (comp.databases.pick)
  • Re: ASCIIEncoding and "Display Hex"
    ... hex" string into a byte array? ... I have a string that represents a hexadecimal key e.g. ... Rather than ending up with a byte array containing ... Private Shared Function GetBytes(ByVal HexString As String) As Byte ...
    (microsoft.public.dotnet.languages.vb)
  • Re: How to find linefeeds from data
    ... This method is not correct beacuse the string may contain two symbols one of ... which has HEX value ending with '0' and another beginning with 'A'. ... > Just substitute with the HEX value of the ASCII character you are ...
    (comp.databases.ingres)
  • Hex editor display - can this be more pythonic?
    ... I'm building a hex line editor as a first real Python programming exercise. ... I had considered using the .translatemethod of strings, however this would require a larger translation table than my printable string. ... Where printing chars are shown in parenthesis, characters with Python escape sequences will be shown as their escapes in parens., while non-printing chars with no escapes will be shown with nothing in parens. ...
    (comp.lang.python)
  • Re: compilation problem
    ... want this utility to print decimal for hex numbers entered. ... IF the third argument passed is a string specifying a decimal number ... TERMINATE with an EXIT_SUCCESS returncode ...
    (comp.lang.c)