Android Question Bit.ToHexString Issue

vincentehsu

Member
Licensed User
Longtime User
My Command as below,could anybody tell me what wrong?
B4X:
Sub Button1_Click
    Dim t(1) As Byte
    t(0)=27
    Dim f(1) As Byte
    Log(Bit.ToHexString(t(0)))
    f(0)=Bit.ToHexString(t(0))
    Msgbox(f(0),"")
End Sub

Error:
B4X:
1b

java.lang.NumberFormatException: Invalid double: "1b"


    at java.lang.StringToReal.invalidReal(StringToReal.java:63)
    at java.lang.StringToReal.parseDouble(StringToReal.java:269)
    at java.lang.Double.parseDouble(Double.java:295)
    at b4a.example.main._button1_click(main.java:329)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:163)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:159)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:77)
    at android.view.View.performClick(View.java:4626)
    at android.view.View$PerformClick.run(View.java:19293)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:157)
    at android.app.ActivityThread.main(ActivityThread.java:5293)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
    at dalvik.system.NativeStart.main(Native Method)
java.lang.NumberFormatException: Invalid double: "1b"
 

James Chamblin

Active Member
Licensed User
Longtime User
You are trying to cast a hex string into a byte. This can't be done like this in B4A. There is a difference between a value such as 27 and 0x1b, and a string such as "127" and "0x1b". Doing f(0) = "127" will work because Java has an implicit function for converting decimal strings to bytes. There is no such function for hex strings in Java, and by extension B4A. In order to cast a hex string to a byte, you must explicitly call a function to do so. So to get your code to work, do something like this:
B4X:
Sub Button1_Click
    Dim t(1) As Byte
    t(0)=27
    Dim f(1) As Byte
    Log(Bit.ToHexString(t(0)))
    f(0)=Bit.ParseInt(Bit.ToHexString(t(0)),16)
    Msgbox(f(0),"")
End Sub
However, this doesn't really serve any practical purpose because B4A stores bytes exactly the same way in memory regardless of what format is on the other side of the equal sign, so doing f(0)=t(0) would accomplish exactly the same thing.
 
Upvote 0

vincentehsu

Member
Licensed User
Longtime User
Dear Sir:
I am tring do this for an reason:I got to connect to an BT device and download a file from server, the file is the config file for this BT device,but the BT device only accept the HEX binary command,so i try to convert the file binary data bytes to hex bytes,is there another way to convert the binary to hex, attachment is my soucecode.
 

Attachments

  • BT_Command.zip
    506.4 KB · Views: 204
Upvote 0

James Chamblin

Active Member
Licensed User
Longtime User
There is no such thing as a HEX binary, nor a HEX byte. It is an easy mistake to make as you can type b = 0x1b and it works. It would seem that you are assigning a hex number to b, but in reality the compiler is just converting the 0x1b into binary (00011011) when you compile. Actually, b = 0x1b and b = 27 compile to exactly the same code as both values must be converted to binary.

As for your BT device, it is either accepting a byte (a group of 8 binary digits) or a hex string (a two character string representing the value in hexadecimal notation). Is there any example code that comes with the device API? How does the example code do it?

Is this sub an example of sending a command to the device?
B4X:
Sub cmd_getDeviceInfo_Click
    Dim TriggerPayment() As Byte = Array As Byte(0x00, 0x02, 0xF2, 0xF2)
    AStream.Write(TriggerPayment)
End Sub

If so, then what is actually being sent is not a HEX array, but a byte array. You could type this and it would be the same.
B4X:
Sub cmd_getDeviceInfo_Click
    Dim TriggerPayment() As Byte = Array As Byte(0, 2, 242, 242)
    'or even (0, 2, -14, -14) since -14 and 242 and 0xF2 compile to the same binary value (11110010)
    AStream.Write(TriggerPayment)
End Sub
 
Upvote 0
Top