B4R Question i have an issue to convert variable int to array as byte

candide

Active Member
Licensed User
just i want to convert an integer to array of byte, to introduce it in a string but i cannot convert my integer to byte: why ? i don't undersant... if you can help me, you will be welcome!

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial1 As Serial
    Public bc As ByteConverter
    Public timer1 As Timer
    Public sosnb As Int = 0
    Public sosB() As Byte
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    timer1.Initialize("timer1_tick",1000)
    timer1.enabled = True
End Sub
Sub timer1_tick
    sosnb = sosnb +1
    sosB = bc.IntsToBytes(sosnb)
    Log(sosB)
End Sub
 

candide

Active Member
Licensed User
i have an interger moving from 0 to 999 and i want to integrate this integer in an array of byte representing a string

at last i found a solution but not clean!
global variable is :
public sosnb as int = 0

in my sub i have :
Dim ss As String = sosnb
Dim sas() As Byte = " "
bc.ArrayCopy(bc.StringToBytes(ss),sas)
bc.ArrayCopy2(sas,0,payload_out,20,3)

=> and i have my integer in an array of byte representing my string

i didn't find a way to be more efficient..

thanks for your help
 
Upvote 0

tigrot

Well-Known Member
Licensed User
i have an interger moving from 0 to 999 and i want to integrate this integer in an array of byte representing a string

at last i found a solution but not clean!
global variable is :
public sosnb as int = 0

in my sub i have :
Dim ss As String = sosnb
Dim sas() As Byte = " "
bc.ArrayCopy(bc.StringToBytes(ss),sas)
bc.ArrayCopy2(sas,0,payload_out,20,3)

=> and i have my integer in an array of byte representing my string

i didn't find a way to be more efficient..

thanks for your help
if you have a number between 0 and 99 your code may fail. Use numberformat.
 
Upvote 0
Top