Android Question Convert AsciiHex to Binary

Steve Piehl

Member
Licensed User
Longtime User
I am trying to convert a two digit AsciiHex representation of a single character to it's binary equivalent. What is the best function for this?

Example: AsciiHex(1C)= Binary(00011100)

From what I have pieced together by reading other posts, it appears that there isn't a function to do this directly. I believe will have to build the Binary string bit by bit.

Thanks in advance for any assistance.
 

Steve Piehl

Member
Licensed User
Longtime User
I figured it out... here is the "almost completely" borrowed code I pieced together from other posts...

B4X:
Sub ToBinaryString(a) As String
    Dim i As Int
    'Convert the passed variable (a) to Decimal
    i = Bit.ParseInt(a, 16)
    'build the String bit by bit
   Dim sb As StringBuilder
   sb.Initialize
   Dim x As Int = Bit.ShiftLeft(1, 7)
   For j = 0 To 7
     Dim ii As Int = Bit.And(i, x)
     If ii <> 0 Then
       sb.Append("1")
     Else
       sb.Append("0")
     End If
     x = Bit.UnsignedShiftRight(x, 1)
   Next
   Return sb.ToString
End Sub
 
Upvote 0
Top