B4A Library ByteConverter library

It's occasionally useful to be able to poke around in things as arrays of bytes so this library lets you do it.

It can transform arrays of primitive types to and from arrays of bytes with the "endian-ness" of the conversion specified. If you need endian-ness you will either know about it or rather quickly find out. :)

Byte arrays can be transformed to and from a hexadecimal string representation.

Strings can be transformed to and from arrays of Bytes using specified encodings and to and from arrays of Chars.

An array copy method is thrown in for good measure.

EDIT :- Version 1.1 posted. See post #4 for details.

Download link: www.b4x.com/android/files/ByteConverter1.1.zip
 
Last edited by a moderator:

kimble01

Member
Licensed User
Longtime User
A byte is shown as a value between -128 and +127, so FF (255) is shown as -1.
If you want to convert a byte to an integer value between 0 and 255, you can use this :
Dim Int_Byte As Int
Int_Byte = Bit.AND(0xff, Byte)

Yes, that is exactly what I did!
 

gfpos

New Member
Licensed User
Longtime User
hi agraham,

i just downloaded you lib und tried to compile the example: ByteConverterDemo.b4a.
unfortunately it does not compile. see the attached image.
do you know, what is wrong? does it depend on my java-version?

many thanks in advance
 

Attachments

  • compile-error.PNG
    compile-error.PNG
    31.3 KB · Views: 377

gfpos

New Member
Licensed User
Longtime User
Are you sure that you copied the jar file together with the XML file?

i copied into the wrong folder.
after creating "additional library path" and copying both files in it everthing is fine.

thank you
 

coolice

New Member
Licensed User
Longtime User
Hello All.

I'm new to the forum and new to trying to get my head around Android code.
I stumbled across basic4android by chance and after a short while took the plunge and brought it. I must say I am impressed and am starting to learn my way around little by little around work.

Basically I am looking to build and app that interrogates a Bluetooth module (HC-05) and sends it a hex command to generate a response, then displays the returned hex code interpreted as ASCII in various text boxes.
Using the Bluetooth chat application as a starting point and with the tutorials here, I have managed to get the chat app to send out the hex code and receive the response, which when translated is giving me the information I'm requesting. However I am struggling to now find the lines of code I'd need to change this ASCII hex code to readable text.
For example if I send 1C0A I get sent back 33 on one line and 322E34B043 on the 2nd, which is telling me the temperature reading.

The last sub of the chat app;

"Sub LogMessage(From AsString, Msg AsString)

txtLog.Text = txtLog.Text & From & ": " & Msg & CRLF

txtLog.SelectionStart = txtLog.Text.Length

End Sub"

So I'm guessing this is were I need to put another line of code or two, to take the received information and convert to display correctly. Various searches have not shown me anything obvious to try, other than what I already have and due to my newbie nature to Android coding it's taking me longer to work things out. I'm hoping that by working out what I need to do to the chat app, I can apply that knowledge to build the app I am looking for.

Any help would be greatly appreciated on my exciting adventure of trying to learn the basics of what I'm trying to achieve.

Ian


P.S. Sorry, may have posted in the wrong thread. Have made a post in the Questions pages.
 
Last edited:

Turbo3

Active Member
Licensed User
Longtime User
When will a B4i version be available?
================
Never mind. I see it is built-in now.
 
Last edited:

Thuong

Member
Licensed User
Longtime User
Dear
I have array of hex [55 , 0A ,FA] and can i convert array to Int ?
 

Thuong

Member
Licensed User
Longtime User
Dear
Sorry, I have a stream hex value [550AFA], I would like convert to integer is 5573370
Help me
 

stu14t

Active Member
Licensed User
Longtime User
That code in the post I have shown will work but you will need to delineate the stream.
 

Thuong

Member
Licensed User
Longtime User
Dear Raphaelcno
I receive byte buffer from Bluetooth (value of analog), i can use
Dim msg AsString
msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")

then convert string to Int
This is correct
 

stu14t

Active Member
Licensed User
Longtime User
I think this is what you are trying to do

B4X:
Dim newval As Int
Dim msg As String

msg = BytesToString(buffer, 0, buffer.Length, "UTF8")   

newval = Bit.ParseInt(msg, 16)
    
Log(newval)
 

kaudie

Member
Licensed User
Longtime User
Why do I get an error when I try this?

Dim Str_HexNumber As String
Str_HexNumber = "DA2DDE56"
Dim Int_Number As Int
Int_Number = Bit.ParseInt(Str_HexNumber, 16)

The error is:
java.lang.NumberFormatException: Invalid int: "DA2DDE56"

thanks
 

Kwame Twum

Active Member
Licensed User
Longtime User
Hello guys, please why is no one paying attention to Post #20?
 

power67

New Member
Licensed User
Longtime User
I installed the latest b4A and added the lib to my additional lib folder, however, I get the following error when compiling.

anywheresoftware.b4a.agraham.byteconverter does not exist
public static anywheresoftware.b4a.agraham.byteconverter.ByteConverter _conv = null;


any advice would be great, I had this app working in previous version

Thanks in advance for your assistance.

Ross
 
Top