Android Question String of text to ASCII HEX string?

Discussion in 'Android Questions' started by techknight, Jul 13, 2015.

  1. techknight

    techknight Well-Known Member Licensed User

    Question:

    How do I convert a string into an ASCII HexString?

    For example if I have

    somestring = "012"

    it becomes
    hexstring = "303132" string representation of ASCII hex bytes. Without the LF and CR

    thanks.
     
  2. Roycefer

    Roycefer Well-Known Member Licensed User

    Code:
    Dim b() As Byte = "Hello".GetBytes("UTF8")
    Dim ints(b.Length) As Int
    For j =0 To b.Length-1
        ints(j) = b(j)
        
    Log(ints(j))
    Next
    This will Log() the decimal values of the ASCII bytes (note that ASCII is a subset of UTF-8). You can implement an easy "decimalToHex(dec as Int) as String" method to turn those decimal values into hex values.
     
  3. techknight

    techknight Well-Known Member Licensed User

    But.. I dont see where its turned into a hex string? Just a byte array?

    I wouldnt need the Ints. Just some way to convert each byte into a string representation of the hex byte, thats NOT an array. but a single string.

    I guess I would just need to use the Convert library.
     
  4. Roycefer

    Roycefer Well-Known Member Licensed User

    Code:
    Sub decimalToHex(dec As Int) As String
        
    Dim hexString As String
        
    Dim again As Boolean = True
        
    Do While again
            
    Dim hexdigit As Int = dec mod 16
            hexString = singleDigitToHex(hexdigit) & hexString
            dec = (dec-hexdigit)/
    16
            
    If dec<1 Then again = False
        
    Loop
        
    Return hexString
    End Sub

    Sub singleDigitToHex(dig As Int) As String
        
    If dig<10 Then Return dig
        
    If dig==10 Then Return "A"
        
    If dig==11 Then Return "B"
        
    If dig==12 Then Return "C"
        
    If dig==13 Then Return "D"
        
    If dig==14 Then Return "E"
        
    If dig==15 Then Return "F"
    End Sub
    The ByteConverter library could also do all that for you.
     
  5. techknight

    techknight Well-Known Member Licensed User

    I did it way easier using your first subroutine. Thanks for your insight.

    Code:
    Sub TextToHexString(data As StringAs String
    Dim b() As Byte = data.GetBytes("UTF8")
    Dim I As Int
    Dim Str As String
    Str = 
    ""
    For I=0 To b.Length-1
        Str = Str & Convert.HexFromBytes(b(I))
    Next
    Return Str
    End Sub
     
  6. Roycefer

    Roycefer Well-Known Member Licensed User

    If you're using the ByteConverter library, you don't need to send HexFromBytes one Byte at a time. You can send it the whole array and it'll send you back the hex string all at once.

    Code:
    Log(Convert.HexFromBytes("123".GetBytes("UTF8")))
     
  7. techknight

    techknight Well-Known Member Licensed User

    Ah... Even more efficient. Thanks!
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice