|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Conversion of character to hexadecimalHi 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. VM wrote:
> Hi all, If you're not concerned about Unicode, you can do it something like this (other variations possible):> > 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? 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.
Show quote
Hide quote
> > I have a simple task to print a String as a hexadecimal Here is one of those "other variations" you mentioned...> > 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. 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 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 > >
VB 6, SP 5 and Hyperthread
Organizing Constants for Easier Maintenance Give Focus to Background Form Count Numerous Matches Only Once XP Styles and Web Browser control VB6: Toolbar button sizeing... Newbie: remember chkbox from closed form beyond dynamic code execution Missing keyboard shortcuts IIS hangs when testing VB DLL |
|||||||||||||||||||||||