Android Question Send Decimal numbers via Serial

Josh Dobson

Member
Licensed User
Hello all,

Been beating my head against a wall trying to achieve the following:

I'm using the example:
https://www.b4x.com/android/forum/threads/android-device-unique-id-alternative-to-phoneid.14759/
From the device id, I'm grabbing the last 8 hex digits in order to create a unique "user".

The reason I'm only grabbing the last 8 is, I am then trying to generate a 32 bit number from them.
For example:
Device ID: 1a2b3c4d5e6f7a10
32 bits: 5e6f7a10
What I am desperately seeking to do is convert the 32 bits (5e6f7a10) to a zero-padded (If needed) 10 digit decimal number: 1584364048 (zero padding not needed in this example)

I am needing this 10 digit decimal due to my microcontroller waiting to receive (2) 5 digit numbers for "word" sized variables.

I am currently using:
B4X:
Sub SendSerial(message As String)
    If AsyncStreams.IsInitialized = False Then Return
    Dim buffer() As Byte
    buffer = message.GetBytes("UTF8")
    AsyncStreams.Write(buffer)
End Sub
to send the payload but this is where I get tripped up.

I sure hope the above makes sense and if it does, hopefully one of you fine folks can assist this dummy :).

Thanks to all,
C
 

walterf25

Expert
Licensed User
Longtime User
Hello all,

Been beating my head against a wall trying to achieve the following:

I'm using the example:
https://www.b4x.com/android/forum/threads/android-device-unique-id-alternative-to-phoneid.14759/
From the device id, I'm grabbing the last 8 hex digits in order to create a unique "user".

The reason I'm only grabbing the last 8 is, I am then trying to generate a 32 bit number from them.
For example:
Device ID: 1a2b3c4d5e6f7a10
32 bits: 5e6f7a10
What I am desperately seeking to do is convert the 32 bits (5e6f7a10) to a zero-padded (If needed) 10 digit decimal number: 1584364048 (zero padding not needed in this example)

I am needing this 10 digit decimal due to my microcontroller waiting to receive (2) 5 digit numbers for "word" sized variables.

I am currently using:
B4X:
Sub SendSerial(message As String)
    If AsyncStreams.IsInitialized = False Then Return
    Dim buffer() As Byte
    buffer = message.GetBytes("UTF8")
    AsyncStreams.Write(buffer)
End Sub
to send the payload but this is where I get tripped up.

I sure hope the above makes sense and if it does, hopefully one of you fine folks can assist this dummy :).

Thanks to all,
C

What is exactly the issue? do you get any error? does anything get to the micro-controller at all?

What baudrate are you using, are you setting the same COM settings on your micro-controller etc..

Regards,
Walter
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Are you trying to get a 10 character, 0 padded string? If so, how about this?
B4X:
'Fake device id
Dim stringID As String = "1a2b3c4d0e6f7a10"
'Get last eight of device id
Dim lastEight As String = stringID.SubString(8)
'Convert from hex string to integer
Dim asInteger As Int = Bit.ParseInt(lastEight,16)
'Convert integer to string (B4A does it for us)
Dim asString As String = asInteger
'Pad string
Dim padding As String = "0000000000"
If asString.Length < padding.length Then
    asString = padding.SubString(asString.Length) & asString
End If
Log(asString)
 
Upvote 0

Josh Dobson

Member
Licensed User
Thanks guys,

I think I figured it out. The problem was, I was dealing with a 16 bit number that happened to be below 10000, causing my microcontroller to freak out.
At least that's what I see so far :confused:. I'll keep you posted if I run into any snags.

Thanks again,
C
 
Upvote 0
Top