B4R Library rConvert

B4R Library rConvert


Brief
rConvert
is a lightweight, open-source helper library for B4R that provides practical conversion routines often needed when working with microcontrollers, sensors, and communication protocols.

It includes simple methods for converting between:
- Unsigned integers (UInt, ULong) and byte arrays.
- Floats and byte arrays (IEEE 754).
- Numbers and formatted strings.
- Bytes and hexadecimal strings.
- Common convenience helpers (e.g. On/Off → Boolean).
- Endianness, Checksum, BCD, Bin, Modbus, BitWise operations.
- Constants for numeric ranges with reference table.

The goal is to keep the routines small, efficient, and compatible with B4R’s limitations (no StringBuilder, limited standard libraries, etc.), so you can drop them into any project.

- Developed with B4R 4.00 (64 bit), arduino-cli 1.2.2.
- Tested with an Arduino UNO.


Files
The rConvert.zip archive contains the library (b4xlib).


Install
Copy the rConvert library folder from the ZIP into your B4R Additional Libraries folder, keeping the folder structure intact.


Example Selected Functions with Log output
B4X:
'========================================================================
' Main Test App
'========================================================================
Private Sub AppStart
    Serial1.Initialize(115200)
    Log(CRLF, "*****", CRLF)
    Log(CRLF, "[Main.AppStart] Convert ", Convert.VERSION, CRLF)
   
    TestUInt
    TestULong
    TestFloat
    TestTwoBytesHex
    TestBCD
    TestBinary
    TestBoolean
    TestXORChecksum
    TestSwap
    TestModbusCRC16
    TestBitWise

    Log(CRLF, "[Main.AppStart] Done", CRLF)
End Sub

'========================================================================
Private Sub TestUInt
    Dim testvalue As UInt
    Dim testbytes() As Byte

    Log("[TestUInt] Start")
    testvalue = 10
    testbytes = Convert.UIntToBytes(testvalue)
    Log("[UIntToBytes] int=", testvalue, " > result hex=", Convert.BytesToHex(testbytes), ", length=",testbytes.Length)
   
    testbytes = Array As Byte(0x0A, 0x00)
    testvalue = Convert.BytesToUInt(testbytes)
    Log("[BytesToUInt] bytes=", Convert.BytesToHex(testbytes), ", length=",testbytes.Length," > result uint=", testvalue)
    Log("[TestUInt] Done")
End Sub

'========================================================================
Private Sub TestULong
    Dim testvalue As ULong
    Dim testbytes() As Byte
    Dim teststring As String

    Log("[TestULong] Start")
    testvalue = 10
    testbytes = Convert.ULongToBytes(testvalue)
    Log("[ULongToBytes] long=", testvalue, " > result hex=", Convert.BytesToHex(testbytes), ", length=",testbytes.Length)
   
    testbytes = Array As Byte(0x0A,0x00,0x00,0x00)
    testvalue = Convert.BytesToULong(testbytes)
    Log("[BytesToULong] bytes=", Convert.BytesToHex(testbytes), ", length=",testbytes.Length," > result ulong=", testvalue)
   
    teststring = testvalue
    Log("[ULongToString Cast] ulong=", testvalue, " > result teststring=", teststring)
   
    teststring = NumberFormat(testvalue, 0, 0)
    Log("[ULongToString NumberFormat] ulong=", testvalue, " > result teststring=", teststring)
    Log("[TestULong] Done")
End Sub

'========================================================================
Private Sub TestFloat
    Dim testvalue As Float
    Dim testbytes() As Byte

    Log("[TestFloat] Start")
    testvalue = 18.58
    testbytes = Convert.FloatToBytes(testvalue)
    Log("[FloatToBytes] float=", testvalue, " > result hex=", Convert.BytesToHex(testbytes), ", length=",testbytes.Length)
    Log("[TestFloat] Done")
End Sub

'========================================================================
Private Sub TestTwoBytesHex
    Dim b1 As Byte = 10
    Dim b2 As Byte = 15
    Log("[TestTwoBytesHex] Start")
    Log("[TwoBytesToHex] b1=", b1, ", b2=", b2, " > result hex=", Convert.TwoBytesToHex(b1, b2))
    Log("[TestTwoBytesHex] Done")
End Sub

'========================================================================
Private Sub TestBCD
    Dim testbyte As Byte = 15
    Log("[TestBCD] Start")
    Log("[ByteToBCD] byte=",testbyte, " > result hex=0x", Convert.OneByteToHex(Convert.ByteToBCD(testbyte)), ", bin=", Convert.ByteToBCDBin(testbyte))
    Log("[TestBCD] Done")
End Sub

'========================================================================
Private Sub TestBinary
    Dim testbyte As Byte
    Dim teststring As String
    Log("[TestBinary] Start")
    testbyte = 0x43
    Log("[ByteToBin] byte=",testbyte, ", hex=0x", Convert.OneByteToHex(testbyte), " > result bin=", Convert.ByteToBin(testbyte))
   
    testbyte = 0x0A
    Log("[NibbleToBin] byte=",testbyte, ", hex=0x", Convert.OneByteToHex(testbyte), " > result bin=", Convert.NibbleToBin(testbyte))
   
    teststring = "11100011"
    Log("[BinToDec] bytes=",teststring.GetBytes, ", dec=", Convert.BinToDec(teststring))
    Log("[TestBinary] Done")
End Sub

'========================================================================
Private Sub TestBoolean
    Log("[TestBoolean] Start")
    Log("[OnOffToBool] on > result ", Convert.OnOffToBool("on"), ", off > result ", Convert.OnOffToBool("off"))
    Log("[TestBoolean] Done")
End Sub

'========================================================================
Private Sub TestXORChecksum
    Dim testbytes() As Byte = Array As Byte(0x0A, 0x0B)
    Dim testbyte As Byte
    Log("[TestXORChecksum] Start")
    testbyte = Convert.XORChecksum(testbytes)
    Log("[XORChecksum] bytes=", Convert.BytesToHex(testbytes), " > result byte=", Convert.OneByteToHex(testbyte))
    Log("[TestXORChecksum] Done")
End Sub

'========================================================================
Private Sub TestSwap
    Dim testvalue As UInt = 23
    Dim testbytes() As Byte
    Log("[TestSwap] Start")
    Log("[SwapUInt16] uint=", testvalue, " > result uint=", Convert.SwapUInt16(testvalue))
   
    testbytes = Convert.SwapUInt16ToBytes(testvalue)
    Log("[SwapUInt16ToBytes] uint=", testvalue, " > result bytes=", Convert.OneByteToHex(testbytes(0)), Convert.OneByteToHex(testbytes(1)))
    Log("[TestSwap] Done")
End Sub

'========================================================================
Private Sub TestModbusCRC16
    Dim testframe() As Byte = Array As Byte(0x01, 0x03, 0x00, 0x00, 0x00, 0x0A)
    Dim testbytes() As Byte
    Dim crcNum As ULong
    Dim valid As Boolean
    Log("[TestModbusCRC16] Start")
   
    Log("[ModbusCRC16Test] Frame=" , Convert.BytesToHex(testframe))
    testbytes = Convert.ModbusCRC16(testframe)
    Log("[ModbusCRC16] CRC bytes [low, high]=", Convert.BytesToHex(testbytes))
   
    crcNum = Convert.ModbusCRC16UInt(testframe)
    Log("[ModbusCRC16UInt] CRC numeric=0x" , Convert.BytesToHex(Array As Byte(Bit.ShiftRight(crcNum, 8), Bit.And(crcNum, 0xFF))), " (decimal=" , crcNum , ")")
   
    testbytes = Convert.ModbusCRC16TransmittedFrame(testframe)
    Log("[ModbusCRC16TransmittedFrame] Transmitted frame=" , Convert.BytesToHex(testbytes))
   
    valid = Convert.ModbusCRC16Check(testbytes)
    Log("[ModbusCRC16Check] CRC valid (1=true)? " , valid)
    Log("[TestModbusCRC16] Done")
End Sub

'========================================================================
Private Sub TestBitWise
    Dim testbyte As Byte
    Dim testbytes() As Byte
    Log("[TestBitWise] Start")

    testbyte = 0
    testbyte = Convert.SetBit(testbyte, 3, True)
    Log("[SetBit] Set bit 3 for value 0. result=", testbyte)
   
    testbyte = Convert.SetBit(testbyte, 3, False)
    Log("[SetBit] Clear bit 3 for value 8. result=", testbyte)
   
    testbyte = 8
    testbyte = Convert.ToggleBit(testbyte, 3)
    Log("[ToggleBit] Toggle bit 3 for value 8. result=", testbyte)
   
    testbyte = Convert.ToggleBit(testbyte, 1)
    Log("[ToggleBit] Toggle bit 1 for value 0. result=", testbyte)
   
    testbyte = 8
    Log("[GetBit] Get bit 3 for value 8=", Convert.GetBit(testbyte, 3))
    Log("[GetBit] Get bit 2 for value 8=", Convert.GetBit(testbyte, 2))
   
    testbyte = 170
    Log("[ByteToBitsString] b=",testbyte, ", bitsstring=", Convert.ByteToBitsString(testbyte))
   
    testbytes = Array As Byte(5, 170)
    Log("[BytesToBitsString] Byte 1=5, byte 2=170. result=", Convert.BytesToBitsString(testbytes))
    Log("[TestBitWise] Done")
End Sub



Function Index
Function Index:
-- Bytes --
ByteToBool(bytes) : First byte "1" > True, Else False.
ByteToInt(bytes) : ASCII digit byte "0"-"9" > integer 0–9.
BytesToHex(bytes) : Byte Array > hex string.
OneByteToHex(byte) : Convert single byte To HEX string.
TwoBytesToHex(b1,b2) : Convert 2 bytes To HEX string.
ReverseBytes(b) : Reverse byte order in Array.
BytesToString(b): Convert bytes to string.

-- Bool --
BoolToString(state) : True > "1", False > "0".
BoolToOnOff(state) : True > "ON", False > "OFF".
OnOffToBool(value) : "ON"/"On"/"on"/"oN" > True.
IntToBool(value) : Convert int To bool.

-- UInt --
UIntToBytes(value) : 16-Bit unsigned > little-endian bytes.
BytesToUInt(b) : Little-endian 2 bytes > unsigned 16-Bit.
UIntToHex(value) : Converts an UInt To HEX string with 2 bytes.

-- ULong --
ULongToBytes(value) : 32-Bit unsigned > little-endian bytes.
BytesToULong(b) : Little-endian 4 bytes > unsigned 32-Bit.
ULongToHex(value) : Converts an ULong To HEX string with 4 bytes.

-- Float --
FloatToBytes(value) : 32-Bit float > little-endian bytes.
BytesToFloat(b) : Little-endian 4 bytes > 32-Bit float.
 
-- Bin --
ByteToBin(b) : Convert 0–255 byte > "xxxxxxxx" binary string.
BytesToBin(b()) : Converts byte array to a binary string representation.
NibbleToBin(nibble) : Convert 0–15 nibble > "xxxx" binary string.
BinToDec(string): Converts a binary string like "11100011" > 227.

-- BCD --
ByteToBCD(value) : Decimal 0–99 > single-byte BCD.
ByteToBCDBin(value) : Decimal 0–99 > BCD > binary string.
BCDToByte(b) : Single-byte BCD > decimal 0–99.
UIntToBCDArray(value) : UInt 0–9999 > 2-byte BCD Array.
BCDArrayToUInt(b) : 2-byte BCD Array > integer 0–9999.

-- Checksum --
XORChecksum(b) : XOR of all bytes.

-- Endianness --
SwapUInt16(value) : Swap 2-byte unsigned integer.
SwapUInt32(value) : Swap 4-byte unsigned integer.
SwapUInt16ToBytes(value) : UInt16 > reversed 2-byte Array.
BytesToUInt16Swapped(b) : Reversed 2-byte Array > UInt16.
SwapUInt32ToBytes(value) : UInt32 > reversed 4-byte Array.
BytesToUInt32Swapped(b) : Reversed 4-byte Array > UInt32.

-- String --
StringTrim(s) : Trim spaces/tabs from both ends.
ToUpperCase(s) : ASCII lowercase > uppercase.
ToLowerCase(s) : ASCII uppercase > lowercase.
EqualsIgnoreCase(s1,s2) : Compare ignoring ASCII Case.
ReplaceString(orig,search,repl) : Replace all occurrences in byte Array.
GetSplitCount(buffer) : Get number of items from CSV string.
AsciiBufferToInt(buffer) : Convert buffer containing ASCII digits To an integer.

-- Modbus CRC-16 --
ModbusCRC16(frame) : Calculate CRC16 (Modbus RTU) And Return As 2-byte Array in little-endian order: [low byte, high byte].
ModbusCRC16UInt(frame) : Calculate CRC16 (Modbus RTU) And Return As numeric 16-Bit value [high byte, low byte].
ModbusCRC16TransmittedFrame(frame) : Append CRC16 To the end of a frame (low byte first, high byte second).
ModbusCheckCRC16 : Validate that a frame ends with the correct Modbus CRC.
ModbusCRC16Test(frame) : Test the Modbus CRC16 functions For a frame.

-- BitWise ---
SetBit(b, index, on) : Sets Or clears a Bit in a byte at the given index.
ToggleBit(b, index) : Flips (toggles) a Bit in a byte at the given index.
GetBit(b, index) : Tests If a Bit at the given index in a byte is set.
ByteToBitsString(b) : Converts a single byte To an 8-character binary string (same As ByteToBin).
BytesToBitsString)b()) : Converts a byte Array To a binary string representation (same As BytesToBin).

 --- Misc ---
DirectionToString(direction) : Convert direction given as byte to string.



Licence
MIT.
 

Attachments

  • rConvert.b4xlib
    8.2 KB · Views: 3
Last edited:

rwblinn

Well-Known Member
Licensed User
Longtime User
Update v1.1.0 (Build 2025-10-01) (See Post #1)

-NEW: Constants for numeric ranges with reference table.
-NEW: AsciiBufferToInt - Convert buffer containing ASCII digits to an integer.
-NEW: Endianness, Checksum, BCD, Bin.
-NEW: Example with edge test cases.
-UPD: Various minor improvements.
 
Last edited:

peacemaker

Expert
Licensed User
Longtime User
B4X:
Private Sub CRC(buf() As Byte) As Byte()
   Dim CRC1 As Short
   CRC1 = 0xFFFF
   For i = 0 To buf.Length - 1
        CRC1 = Bit.Xor(CRC1,Bit.And( buf(i),0xFF))
         For j = 0 To 7
            If Bit.And(CRC1, 1) > 0 Then
                 CRC1 =Bit.And( Bit.ShiftRight(CRC1,1),0x7FFF)
                CRC1=Bit.Xor(CRC1, 0xA001)
            Else
                 CRC1 =Bit.And( Bit.ShiftRight(CRC1,1),0x7FFF)
            End If
        Next  
    Next      
    Return (bc.IntsToBytes(Array As Int(CRC1)))
End Sub

Hi,
Does your lib have such CRC-function of MODBUS operations ?
If interesting, you may add.

This code calculates CRC-16 (Cyclic Redundancy Check) using the Modbus RTU algorithm (IBM CRC-16 variant).
The function returns a CRC in the little-endian format (the lowest byte is the first), which corresponds to the Modbus standard.
 

rwblinn

Well-Known Member
Licensed User
Longtime User
Update v1.2.0 (Build 2025-10-04) (See Post #1)

- NEW: Functions Modbus RTU CRC-16 - equivalent to IBM CRC-16 (poly 0xA001), initial value 0xFFFF, little-endian transmission order (low byte first).
- NEW: Functions HEX - UIntToHex (2 bytes), ULongToHex (4 bytes).
- NEW: Functions BitWise - SetBit, ToggleBit, GetBit, ByteToBitsString, BytesToBitsString.
- UPD: Basic Example with Modbus, BitWise functions.
 

rwblinn

Well-Known Member
Licensed User
Longtime User
Update v1.3.0 (Build 2025-10-18) (See Post #1)

- NEW: Function BytesToBin - Convert Byte Array to a binary string representation.
- NEW: Function DirectionToString - Convert direction given as byte to string.
- NEW: Functions ByteToBitsString, BytesToBitsString - Convert byte(s) to bit string.
- NEW: Function BinToDec(string) - Convert binary string to decimal.
- FIX: All convert functions tested on an ESP32 and fixed identified flaws.
- UPD: Basic Example - Added new and grouped selective functions calls.
 
Top