Link to home
Start Free TrialLog in
Avatar of lplayer
lplayer

asked on

Convert Byte Array to Hex String (and Hex to Byte Array)

In VB6

I have a Byte array

Type PlayerBitType
    bit(1 To 13) As Byte '13 bytes = 13 x 8 =104 bit values
End Type

Dim mybytearray() As PlayerBitType

I can set and clear and check these 104 bits in this array no problem

However, I need to save this byte array into an Access table as a string, and therefore need to convert mybytearray() into a string. I thought to convert to a hex string eg "FFFFFFFFFFFFFFFFFFFFFFFFFF" so that I can represent the 104 bit values as string containing 26 hex letters. (26 x bit bits = 104).

Q1. How can I  convert bits in mubytearray to a hex string?

Q2. I woul dthen need to reverse th eprocess and convert the hex string back to mybyte(array).


Thanks

 

Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Here is a first approach, sorry if any bug...

Function Binary2HexString(theArray as PlayerBitType) as String
      Binary2HexString = ""

       For I = 1 to 13
           hexnumber$ = HEX$(theArray.bit(I)
           if len(hexnumber$)<2 then hexnumber$ = "0" & hexnumber$    ' ensure it is 2 digits
           Binary2HexString = Binary2HexString & hexnumber$
       Next I
end function
Avatar of lplayer
lplayer

ASKER

Thanks. That got me closer.

In the end I decided to  pass a byte at a time:

Public Function Binary2HexString(ByRef mybyte As Byte) As String
     
Dim hexnumber As String
Dim i As Integer

    Binary2HexString = ""
   
    hexnumber = hex$(mybyte)
    If Len(hexnumber$) < 2 Then hexnumber$ = "0" & hexnumber$  ' ensure it is 2 digits
    Binary2HexString = hexnumber$
 
End Function


The more difficult aspect of converting the hex string back to binary still remains.

ASKER CERTIFIED SOLUTION
Avatar of Erick37
Erick37
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial