Android Code Snippet Numeric conversions

Hi everyone,
The class posted in this thread contains many functions that i use to convert numeric variables in others (ex. byte2int16, long2bytes, byte2hex, ecc..)

I hope you enjoy it.

B4X:
'Class module
Sub Class_Globals
    Private C_BYTE_LSB As Byte
    Private C_BYTE_MSB As Byte
    Private C_BYTE1 As Byte
    Private C_BYTE2 As Int
    Private C_BYTE3 As Long
    Private C_BYTE4 As Long
    Private C_BYTE_NEXT_BIT As Int
    Private C_WORD As Int
    Private C_WORD_NEXT_BIT As Long
    Private C_3BYTES As Long
    Private C_DWORD As Long
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
    C_BYTE_LSB = 15
    C_BYTE_MSB = 240
    C_BYTE1 = 255
    C_BYTE2 = 65280
    C_BYTE3 = 16711680
    C_BYTE4 = 4278190080
    C_BYTE_NEXT_BIT = 256
    C_WORD = 65535
    C_WORD_NEXT_BIT = 65536
    C_3BYTES = 16777215
    C_DWORD = 4294967295
End Sub


Sub Byte2Unsigned(B As Object) As Int
    Dim temp As Int = B
    Dim mask As Int = 255
    Dim temp2 As Int
    temp2 =  Bit.And(temp, mask)
    Return temp2
End Sub

Sub Byte2Lsb(Valore As Byte) As Byte
    Return Bit.And(Byte2Unsigned(Valore), C_BYTE_LSB)
End Sub

Sub Byte2Msb(Valore As Byte) As Byte
    Return Bit.ShiftRight(Bit.And(Byte2Unsigned(Valore), C_BYTE_MSB),4)
End Sub

Sub Nibbles2Byte(Lsb As Byte, Msb As Byte) As Byte
    Return Bit.ShiftLeft(Byte2Unsigned(Msb),4) + Lsb
End Sub

Sub Num2Lsb(Valore As Long) As Byte
    '
    'Estrae da un valore numerico a 16 bit il Byte meno significativo
    'Get the less important bit from a 16bit numeric value
    '
    If Valore > 0 And Valore < C_WORD_NEXT_BIT Then
        Return Bit.And(Valore, C_BYTE1)
    Else
        Return 0
    End If
End Sub

Sub Num2Msb(Valore As Long) As Byte
    '
    ' Estrae da un valore numerico a 16 bit il Byte più significativo
    'Get the most important bit from a 16bit numeric value
    '
    If Valore > 0 And Valore < C_WORD_NEXT_BIT Then
        Return Bit.And(Valore, C_BYTE2) / C_BYTE_NEXT_BIT
    Else
        Return 0
    End If

End Sub

Sub Long2Bytes(Valore As Long, Inv As Boolean) As Byte()
    'convert a int32 value in 4 bytes (byte array)
    'Inv boolean stays for big or little endian
    Dim Bytes(4), Res(4) As Byte
    Bytes(0) = Bit.UnsignedShiftRight(Bit.And(Valore, 0xff000000), 24)
    Bytes(1) = Bit.UnsignedShiftRight(Bit.And(Valore, 0xff0000), 16)
    Bytes(2) = Bit.UnsignedShiftRight(Bit.And(Valore, 0xff00), 8)
    Bytes(3) = Bit.And(Valore, 0xff)
    If Inv Then
        Res(0) = Bytes(3)
        Res(1) = Bytes(2)
        Res(2) = Bytes(1)
        Res(3) = Bytes(0)
    Else
        Res(0) = Bytes(0)
        Res(1) = Bytes(1)
        Res(2) = Bytes(2)
        Res(3) = Bytes(3)
    End If
    Return Res
End Sub

Sub Int2Bytes(Valore As Int, Inv As Boolean) As Byte()
    'convert a int16 value in 2 bytes (byte array)
    'Inv boolean stays for big or little endian
    Dim Bytes(2), Res(2) As Byte
    Bytes(0) = Bit.UnsignedShiftRight(Bit.And(Valore, 0xff00), 8)
    Bytes(1) = Bit.And(Valore, 0xff)
    If Inv Then
        Res(0) = Bytes(1)
        Res(1) = Bytes(0)
    Else
        Res(0) = Bytes(0)
        Res(1) = Bytes(1)
    End If
    Return Res
End Sub

Sub Long2List(Valore As Long, Inv As Boolean) As List
    'convert a int32 value in 4 bytes (list of bytes)
    'Inv boolean stays for big or little endian
    Dim Bytes(4) As Byte
    Dim Res As List
    Bytes(0) = Bit.UnsignedShiftRight(Bit.And(Valore, 0xff000000), 24)
    Bytes(1) = Bit.UnsignedShiftRight(Bit.And(Valore, 0xff0000), 16)
    Bytes(2) = Bit.UnsignedShiftRight(Bit.And(Valore, 0xff00), 8)
    Bytes(3) = Bit.And(Valore, 0xff)
    If Inv Then
        Res.Initialize
        Res.add(Bytes(3))
        Res.add(Bytes(2))
        Res.add(Bytes(1))
        Res.add(Bytes(0))
    Else
        Dim tmplst As List
        tmplst.Initialize
        Dim i As Int
        For i =0 To Bytes.Length -1
            tmplst.Add(Bytes(i))
        Next
        Res.Initialize2(tmplst)
    End If
    Return Res
End Sub

Sub Int2List(Valore As Int, Inv As Boolean) As List
    'convert a int16 value in 2 bytes (list of bytes)
    'Inv boolean stays for big or little endian
    Dim Bytes(2) As Byte
    Dim Res As List
    Bytes(0) = Bit.UnsignedShiftRight(Bit.And(Valore, 0xff00), 8)
    Bytes(1) = Bit.And(Valore, 0xff)
    If Inv Then
        Res.Initialize
        Res.add(Bytes(1))
        Res.add(Bytes(0))
    Else
        Dim tmplst As List
        tmplst.Initialize
        Dim i As Int
        For i =0 To Bytes.Length -1
            tmplst.Add(Bytes(i))
        Next
        Res.Initialize2(tmplst)
        'Res.Initialize2(Bytes)
    End If
    Return Res
End Sub

Sub Num2Word(Valore As Long) As String
    '
    ' Trasforma un valore numerico a 16 bit in una stringa di due caratteri (Word)
    'convert a 16bit numeric value in a couple of chars
    '
    '
    If Valore >= 0 And Valore < C_WORD_NEXT_BIT Then
        Return Chr(Num2Msb(Valore)) & Chr(Num2Lsb(Valore))
        'Return Chr((Valore AND C_BYTE2) / C_BYTE_NEXT_BIT) & Chr(Valore AND C_BYTE1)
    Else
        Return ""
    End If
End Sub

Sub Num2Dword(Valore As Long, Inv As Boolean) As String
    '
    ' Trasforma un valore numerico a 32 bit in una stringa di 4 caratteri (DWord)
    ' convert a 32bit numeric value in 4 chars
    '
    Dim Byte1, Byte2, Byte3, Byte4 As String
  
    Byte1 = Chr(Bit.And(Valore, C_BYTE4) / C_3BYTES)
    Byte2 = Chr(Bit.And(Valore, C_BYTE3) / C_WORD)
    Byte3 = Chr(Bit.And(Valore, C_BYTE2) / C_BYTE_NEXT_BIT)
    Byte4 = Chr(Bit.And(Valore, C_BYTE1))
    If Inv Then
        Return Byte1 & Byte2 & Byte3 & Byte4
    Else
        Return Byte4 & Byte3 & Byte2 & Byte1
    End If

End Sub

Public Sub DWord2Num2(values() As Object, bigEndian As Boolean) As Int
    '
    ' Trasforma una stringa di quattro caratteri nel suo valore numerico a 32 bit
    'Convert a 4 chars string in his int32 value
    '
    Dim RetValue As Int = 0
    Dim Val1 As Int = values(0)
    Dim Val2 As Int = values(1)
    Dim Val3 As Int = values(2)
    Dim Val4 As Int = values(3)
    If bigEndian = False Then
        Val2 = Bit.ShiftLeft(Val2, 8)
        Val3 = Bit.ShiftLeft(Val3, 16)
        Val4 = Bit.ShiftLeft(Val4, 24)
    Else
        Val1 = Bit.ShiftLeft(Val1, 24)
        Val2 = Bit.ShiftLeft(Val2, 16)
        Val3 = Bit.ShiftLeft(Val3, 8)
    End If
    RetValue = Val1 + Val2 + Val3 + Val4
    Return RetValue

End Sub


Sub Hex2Num(carHex As String) As Byte
    '
    ' dato un valore esadecimale (2 byte) calcola il rispettivo
    ' valore numerico
    'Calculating hex value starting from a char
    '
    Dim valHex As Byte : valHex = 0
    Dim nfh As Int : nfh = 48
    Dim nlh As Int
  
    If carHex.length = 2 Then
        nfh = Asc(carHex.CharAt(0))  '  Asc(Left(carHex, 1))
    End If
    nlh = Asc(carHex.CharAt(1))  ' Asc(Right(carHex, 1))
  
    'calcola il valore decimale del primo carattere
    Select Case nfh
        Case 48,49,50,51,52,53,54,55,56,57
            valHex = (nfh - 48) * 16
        Case 65,66,67,68,69,70
            valHex = (nfh - 55) * 16
    End Select

    'calcola il valore decimale dell'ultimo carattere
    Select Case nlh
        Case 48,49,50,51,52,53,54,55,56,57
            valHex = valHex + (nlh - 48)
        Case 65,66,67,68,69,70
            valHex = valHex + (nlh - 55)
    End Select

    Return valHex

End Sub

Sub Num2Hex(valDec As Int) As String
    '
    ' trasforma un valore numerico in esadecimale (2 byte)
    'convert a numeric value in a hex string
    '
    Dim cHex As String

    'cHex = Bit.ToHexString(valDec) '  Hex$(valDec)
    If cHex.length = 1 Then cHex = "0" & cHex

    Return cHex

End Sub


Sub Num2Bin(Value As Long, Base As Byte, TypeList As Byte) As List
    ' Converte un numero max 32 bit in un elenco di valori
    ' La lunghezza dell'elenco è data da Base che può essere 8, 16, 32
    ' Il tipo di valori ritornato è dipendente da TypeList
    ' TypeList = 0 : ritorna True se bit = 1, oppure False se bit = 0
    ' TypeList = 1 : ritorna 1 se bit > 0, oppure 0 se bit = 0
    '
    'Convert a int32 numeric value in a list of values.
    'List length is setted on Base variable.
    'Typelist = 0 : return True if bit=1; False if bit=0
    'typelist = 1 : return 1 if bit>0; 0 if bit = 0
  
    Dim NumBit As Byte
    Dim Bits As List
    Dim BitValue As Long
    Dim BitOn, BitOff As Object
    If TypeList = 0 Then
        BitOn = True
        BitOff = False
    Else
        BitOn = 1
        BitOff = 0
    End If
    Bits.Initialize
    For NumBit = 0 To Base
        BitValue = Power(2, NumBit)
        If Bit.And(Value,BitValue) = 0 Then
            Bits.Add(BitOff)
        Else
            Bits.Add(BitOn)
        End If
    Next
    Return Bits
End Sub

Sub Num2BinStr(Value As Long, Base As Byte) As String
    ' Converte un numero max 32 bit in una stringa di valori
    ' La lunghezza della stringa è data da Base che può essere 8, 16, 32
    '
    ''Convert a int32 numeric value in a string of values.
    'List length is setted on Base variable.
    '
  
    Dim NumBit As Byte
    Dim Bits As String
    Dim BitValue As Long
    Dim BitOn, BitOff As Byte
    BitOn = 1
    BitOff = 0
    Bits = ""
    For NumBit = Base - 1 To 0 Step -1
        BitValue = Power(2, NumBit)
        If Bit.And(Value,BitValue) = 0 Then
            Bits = Bits & BitOff
        Else
            Bits = Bits & BitOn
        End If
    Next
    Return Bits
End Sub

Sub Bin2Num(Values As List) As Object
    Dim NumBit As Byte
    Dim RetValue As Object
    RetValue = 0
    For NumBit = 0 To Values.Size - 1
        'ValBit = Values.Get(NumBit)
        If Values.Get(NumBit) = True Then RetValue = Bit.Or(RetValue, Power(2, NumBit))
    Next
    Return RetValue
End Sub

Sub Bytes2Int(Byte1 As Byte, Byte2 As Byte) As Int
    Dim B1 As Int : B1 = Bit.And(Byte1,255)
    Dim B2 As Int : B2 = Bit.And(Byte2,255)
    Dim Result As Int
    Result = Bit.And(Bit.ShiftLeft(B1,8),C_BYTE2) + B2
    Return Result
End Sub

Sub Bytes2Long(Byte1 As Byte, Byte2 As Byte, Byte3 As Byte, Byte4 As Byte) As Long
    Dim B1 As Int : B1 = Bit.And(Byte1,255)
    Dim B2 As Int : B2 = Bit.And(Byte2,255)
    Dim B3 As Int : B3 = Bit.And(Byte3,255)
    Dim B4 As Int : B4 = Bit.And(Byte4,255)
    Dim Result As Long
    Result = Bit.ShiftLeft(B1,24) + Bit.ShiftLeft(B2,16) + Bit.ShiftLeft(B3,8) + B4
    Result = Bit.And(Result,C_DWORD)
    Return Result
End Sub

Sub TestBit(Value As Long, NumBit As Int) As Boolean
    ' Verifica lo stato del Bit NumBit nel valore Value e restituisce True se attivo e False se non attivo
  
    'Verify the state of a bit over a variable
    'Return true if NumBit of Value = 1; Return false if NumBit of Value = 0
    '
    Dim BitValue As Long : BitValue = Power(2, NumBit)
    Return Not(Bit.And(Value, BitValue) = 0)
End Sub

Sub BitOnOff(Value As Object, NumBit As Byte, BitOn As Boolean) As Object
    ' Accende o spegne un Bit della variabile Value
    ' dipendentemente dal valore di BitOn ( True = accende ; False = Spegne )
    ' Restituisce il valore di Value modificato
  
    '
    'Chang ethe state of the selected Bit. Return the "Value" variable changed value
    '
    Dim BitValue As Long
    Dim RetValue As Object : RetValue = Value
    Dim Mask As Object
    BitValue = Power(2, NumBit)
    If BitOn Then
        RetValue = Bit.Or(Value, BitValue)
    Else
        If NumBit > 15 Then                ' Double word
            Mask = C_DWORD
        Else If NumBit > 7 Then         ' Word
            Mask = C_WORD
        Else                               ' Byte
            Mask = C_BYTE1
        End If
        RetValue = Bit.And(Value, (Mask - BitValue))
    End If
    Return RetValue
End Sub

EDIT:
I forgot to say that you can paste this code also in you B4i or B4J project.
 
Last edited:
Top