Home All Groups Group Topic Archive Search About

Conversion of character to hexadecimal

Author
16 Oct 2005 1:09 AM
VM
Hi all,

I have a simple task to print a String as a hexadecimal representation, but
I cannot find any reference in VB 6.0 help. I have a source string "ABC" and
I would like to get a result string "414243" (what actualy means sequence of
single characters "&H41", "&H42" and "&H43"). Is there any simple method to
do that?

Thanks in advance,
P.

Author
16 Oct 2005 1:40 AM
Jim Mack
VM wrote:
> Hi all,
>
> I have a simple task to print a String as a hexadecimal
> representation, but I cannot find any reference in VB 6.0 help. I
> have a source string "ABC" and I would like to get a result string
> "414243" (what actualy means sequence of single characters "&H41",
> "&H42" and "&H43"). Is there any simple method to do that?


If you're not concerned about Unicode, you can do it something like this (other variations possible):

Dim b() as Byte
Dim Idx As Long
Dim Res As String

b() = StrConv(DataStr, vbFromUnicode)
For Idx = 0 to UBound(b)
  Res = Res & Right$("0" & Hex$(b(Idx)), 2)
Next

Now 'Res' contains the hex-ascii version of the Ansi string characters.

--

    Jim Mack
    MicroDexterity Inc
    www.microdexterity.com
Author
16 Oct 2005 3:03 AM
Rick Rothstein [MVP - Visual Basic]
Show quote Hide quote
> > I have a simple task to print a String as a hexadecimal
> > representation, but I cannot find any reference in VB 6.0
> > help. I have a source string "ABC" and I would like to get
> > a result string "414243" (what actualy means sequence
> > of single characters "&H41", "&H42" and "&H43"). Is
> > there any simple method to do that?

> If you're not concerned about Unicode, you can do it
> something like this (other variations possible):
>
> Dim b() as Byte
> Dim Idx As Long
> Dim Res As String
>
> b() = StrConv(DataStr, vbFromUnicode)
> For Idx = 0 to UBound(b)
>   Res = Res & Right$("0" & Hex$(b(Idx)), 2)
> Next
>
> Now 'Res' contains the hex-ascii version of the Ansi
> string characters.

Here is one of those "other variations" you mentioned...

Dim Idx As Long
Dim Res As String
Dim DataStr As String
DataStr = "ABC"
Res = Space$(2 * Len(DataStr))
For Idx = 1 To Len(DataStr)
  Mid$(Res, 2 * Idx - 1) = Hex$(Asc(Mid$(DataStr, Idx, 1)))
Next

Rick
Author
16 Oct 2005 9:57 PM
Someone
Yet another variation:

' Single byte per char
For i = 1 To Len(s)
    sHex = sHex & GetHex(Asc(Mid(s, i, 1)), 2)
Next

' Unicode
For i = 1 To Len(s)
    sHex = sHex & GetHex(AscW(MidW(s, i, 1)), 4)
Next


' Get Hex value padded with 0 to the left
Public Function GetHex(ByVal i As Long, ByVal HexWidth As Integer) As String
    GetHex = Right("0000000" & Hex(i), HexWidth)
End Function



Show quoteHide quote
"Rick Rothstein [MVP - Visual Basic]" <rickNOSPAMnews@NOSPAMcomcast.net>
wrote in message news:O$aYm7f0FHA.2752@TK2MSFTNGP12.phx.gbl...
>> > I have a simple task to print a String as a hexadecimal
>> > representation, but I cannot find any reference in VB 6.0
>> > help. I have a source string "ABC" and I would like to get
>> > a result string "414243" (what actualy means sequence
>> > of single characters "&H41", "&H42" and "&H43"). Is
>> > there any simple method to do that?
>
>> If you're not concerned about Unicode, you can do it
>> something like this (other variations possible):
>>
>> Dim b() as Byte
>> Dim Idx As Long
>> Dim Res As String
>>
>> b() = StrConv(DataStr, vbFromUnicode)
>> For Idx = 0 to UBound(b)
>>   Res = Res & Right$("0" & Hex$(b(Idx)), 2)
>> Next
>>
>> Now 'Res' contains the hex-ascii version of the Ansi
>> string characters.
>
> Here is one of those "other variations" you mentioned...
>
> Dim Idx As Long
> Dim Res As String
> Dim DataStr As String
> DataStr = "ABC"
> Res = Space$(2 * Len(DataStr))
> For Idx = 1 To Len(DataStr)
>  Mid$(Res, 2 * Idx - 1) = Hex$(Asc(Mid$(DataStr, Idx, 1)))
> Next
>
> Rick
>
>