Android Question Bytes converter. Can someone help me with my confusion with IntsFromBytes

jimseng

Active Member
Licensed User
Longtime User
Hello.
I am sending datetimes from a microcontroller as a byte array. It took me so long to work out how to get the data into b4x. My confusion was around LongsFromBytes and IntsFromBytes. The code here is working but I don't understand why the value is stored in "dtime()" and not "dtime" (the first looks, to me, like an array and the second just an int, but it is declared as an int)
A sub question is that I couldn't get it to work with the value being created in a 3 byte chunk. So in Micropython:
value.to_bytes(3,'big') doesn't work.
value.to_bytes(4,'big') does work.
4 bytes just gets padded with 0x00 for the first byte.
I spent so long trying to figure it out and though I have got it working I don't really understand it, which is annoying. Any pointers or some further reading would help.

B4X:
Dim bb As B4XBytesBuilder
    Dim bc As ByteConverter
    bb.Initialize
    For i =0 To b.Length -1
        bb.Append(Array As Byte(b(i)))
    Next
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''TimeA
    Dim ta() As Byte =bb.SubArray2(1,5)
    Dim dtime() As Int = bc.IntsFromBytes(ta) '<---------------why dtime() and not dtime?
    Log(dtime(0))
 

JohnC

Expert
Licensed User
Longtime User
ChatGPT says...

It looks like you're dealing with a few different issues related to byte manipulation and datatype conversion in your programming environment (such as B4X, which is used for Android, iOS, etc.). Let’s break down each part of your question to clarify what's happening.

Why dtime() and not dtime

In the context of B4X (and many other programming languages), dtime() is indeed used to indicate that dtime is an array, not a single integer variable. When you use the method bc.IntsFromBytes, you're converting a byte array into an array of integers. Therefore, dtime needs to be declared as an array of integers (Int) because it will store potentially multiple integers derived from the byte array.

Each integer in the array dtime represents a conversion of a subset of the original byte array into an integer. For example, if your byte array is 4 bytes long, bc.IntsFromBytes will typically convert these 4 bytes into a single integer. But if the byte array is longer, multiple integers will be created, hence the need for an array.

3 Bytes vs. 4 Bytes Issue

Regarding your issue with 3-byte chunks not working, but 4-byte chunks do:
  1. Byte Alignment: Many systems and libraries are optimized for handling data that is aligned to specific boundaries, commonly 4 bytes or 8 bytes. This alignment can make processing faster and more efficient due to the way hardware and software interact with memory.
  2. Data Padding: When you use .to_bytes(4,'big'), Micropython pads the output to ensure it is 4 bytes long, which probably matches the expected input for bc.IntsFromBytes. However, .to_bytes(3,'big') results in a 3-byte array, which might not be properly interpreted by bc.IntsFromBytes because it expects chunks of 4 bytes (or possibly 2 bytes, but usually 4 is more common due to the typical size of an int in many systems).

Reading Suggestions

To better understand these concepts, here are a few topics you might explore:
  • Data Types and Memory Alignment: Understanding how different data types are stored in memory and how alignment affects performance.
  • Byte Manipulation: Explore how bytes are manipulated, converted, and used in programming. Look into endianess (big-endian vs. little-endian) and how it affects byte order in memory.
  • B4X Specific Documentation: Since B4X might have specific ways of handling bytes and integers, reviewing its documentation on ByteConverter and data type handling can provide specific insights.
These concepts are fundamental in lower-level programming and systems programming. You might find chapters on these topics in books about operating systems, computer architecture, or even specific programming languages like Python, Java, or C and C++.
 
Last edited:
Upvote 0

jimseng

Active Member
Licensed User
Longtime User
Thank you. That is a very good explanation. This is one of the more helpful and friendly forums I use.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
I am sending datetimes from a microcontroller as a byte array.
I confused with your question.
Does your microcontroller installed with B4A app?
Isn't this is more appropriate to post in B4R?
Do you mean this B4A code is the receiver of the data in an Android device?
Where do you get the code from?
 
Upvote 0

jimseng

Active Member
Licensed User
Longtime User
I can be quite confusing. I am creating an B4A app to control 4 relays (for a heating system on my boat, not that anybody was asking). My B4A app is communicating with a Raspberry Pi Pico, via BLE, running Micropython, which I am developing in parallel. I am trying to be efficient and keeping the amount of bytes sent between the devices as small as possible by creating a protocol rather than just using strings, like I would have done in the past. Needless to say I am finding it a challenge given that I am not that experienced with bitwise stuff, let alone jumping back and forth between different syntaxes.
B4X:
if mictopython:
    print("I hate indentation")
    'syntax error, you have used the wrong comment character again.
if b4a then
    log("breakpoints in IDEs are easier to debug")
    #and you keep doing it, almost every time
 
Upvote 0

jimseng

Active Member
Licensed User
Longtime User
I am succeeding and have it all pretty much working, it just took me a long time to work out how to decode a 17 byte array into different chunks of data using ByteBuilder and ByteConverter, the two confusing things were that ByteConverter seems to need 4 bytes to convert to an int and the whole IntsFromBytes producing and array not a single int. JohnC and this chatGPT fellow have sorted me out.
 
Upvote 0
Top