B4J Question Binary String to Hex

paddy12309

Member
Licensed User
Hi everyone,

hoping i can get some advice, I have a String (which is binary) but need a hex number is there any nice way of doing this?

binary is off unknown length... but minimum 8

cheers
Paddy
 

emexes

Expert
Licensed User
Upvote 0

paddy12309

Member
Licensed User
So yep working now! So ill put the code here, messy as it is, but its an alarm mask, so its not then being used in calculation...
B4X:
Sub Process_Globals
    'Dim conv As ByteConverter
   
End Sub
Sub SuffixSN (myName As String, mySN As Int) As String
    Return myName & " " & NumberFormat2(mySN,8,0,0,False)
End Sub
Sub BinaryToHex (Binary As String) As String
    AlarmActive
    Binary = AlarmActive
    Do While Binary.Length Mod 4 <> 0
        Binary = "0" & Binary
    Loop
    Dim res As StringBuilder
    Dim Returner As String
    res.Initialize
    For i = 0 To Binary.Length - 1 Step 4
        res.Append(Bit.ToHexString(Bit.And(0xf, Bit.ParseInt(Binary.SubString2(i, i + 4), 2))))
    Next
    'Log(res.ToString)
    Returner = res.ToString
    'Returner = Returner & "00"
    Returner = "0x" & Returner
    Log(Returner)
    Return Returner
End Sub
Sub AlarmActive As String
    If Not(Main.sql1.IsInitialized) Then
        Log("SQL1 Failed")
        'Return
    End If
'    Dim a As Int
'    Dim RS As ResultSet = Main.sql1.ExecQuery("SELECT * FROM DeviceStatus INNER JOIN DeviceLive ON DeviceStatus.DeviceSN = DeviceLive.DeviceSN ")
'    Do While RS.NextRow
'       
'        a = a + 1
'    Loop
    Dim RS As ResultSet = Main.sql1.ExecQuery("SELECT * FROM DTDevice ORDER BY DeviceSN DESC") 'SELECT * FROM Device INNER JOIN DTDevice ON Device.DeviceSN = DTDevice.DeviceSN
    Dim i As Int
    Dim AlMask As String
    AlMask = ""
'    Dim count As Int
'    count = 0
    i = 0
    Do While RS.NextRow
        i = i + 1
       
        If RS.GetInt("E3Status")  < "4" Then
            AlMask = AlMask & "0000"
    '        Log("0000")
        Else If RS.GetInt("E3Status") = "4" Then
            AlMask = AlMask & "0001"
    '        Log("0001")
        Else If RS.GetInt("E3Status") = "5" Then
            AlMask = AlMask & "0010"
    '        Log("0010")
        Else If RS.GetInt("E3Status") = "6" Then
            AlMask = AlMask & "0100"
    '        Log("0100")
        Else if RS.GetInt("E3Status") = "7" Then
            AlMask = AlMask & "1000"
    '        Log("1000")
        Else
            Log("error")
            Log(RS.GetInt("EmStatus"))
        End If
'        Log("DevSN; " & RS.GetInt("DeviceSN") &  "   E3Status; " & RS.GetInt("E3Status"))
    Loop
'        Log(AlMask)
    AlMask = AlMask & "00000000"
    Return  AlMask
End Sub

Nut again thankyou for bearing with me!
 
Upvote 0
Top