Re: How to convert hex to string?
- From: "Oenone" <oenone@xxxxxxxxxxx>
- Date: Thu, 07 Apr 2005 09:52:36 GMT
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
.
- Follow-Ups:
- Re: How to convert hex to string?
- From: Crouchie1998
- Re: How to convert hex to string?
- References:
- How to convert hex to string?
- From: Mika M
- How to convert hex to string?
- Prev by Date: Re: translation from c#
- Next by Date: Re: 2 easy questions
- Previous by thread: How to convert hex to string?
- Next by thread: Re: How to convert hex to string?
- Index(es):
Relevant Pages
|