B4J Question Negative value from HEX

Declan

Well-Known Member
Licensed User
Longtime User
I am working with a GPS device that sends both Lat and Lon in HEX format.
I must "extract" the negative value from the received HEX strings.
Example:

Latitude = 10D27FBC (HEX)
= 282230716
Actual Latitude is 28.2230716
This is correct as it is a positive value.

Longitude = F0B00A5E (HEX)
= 4038068830
This should be -25.6898466

How do I determine whether the HEX value is negative - then convert the decimal value?
 

teddybear

Well-Known Member
Licensed User
The first bit is the sign bit, that is, the first hex number is 8-F, the value is a negative, the number = number - 2^32.
it should be 4038068830-4294967296=-256898466‬
 
Last edited:
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
The first bit is the sign bit, that is, the first hex number is 8-F, the value is a negative, the number = number - 2^32.
it should be 4038068830-4294967296=-256898466‬
Yes, many thanks.
How would I calculate this in B4J?
 
Upvote 0

teddybear

Well-Known Member
Licensed User
Yes, many thanks.
How would I calculate this in B4J?
In B4J it just is a sign int
B4X:
    Dim Latitude As Int = 0x10D27FBC
    Dim Longitude As Int = 0xF0B00A5E
    Log(Latitude)  '282230716
    Log(Longitude) '-256898466
 
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
In B4J it just is a sign int
B4X:
    Dim Latitude As Int = 0x10D27FBC
    Dim Longitude As Int = 0xF0B00A5E
    Log(Latitude)  '282230716
    Log(Longitude) '-256898466
Great, it works
But I get this error:
B4X:
(NumberFormatException) java.lang.NumberFormatException: For input string: "10D2831F"
I must have the 0x before the 10D2831F (0x10D2831F)
 
Upvote 0

teddybear

Well-Known Member
Licensed User
Great, it works
But I get this error:
B4X:
(NumberFormatException) java.lang.NumberFormatException: For input string: "10D2831F"
I must have the 0x before the 10D2831F (0x10D2831F)
If you want to use a hexstring as input, you should convert the hexstring to int
B4X:
    Dim bc As ByteConverter
    Dim b() As Byte=bc.HexToBytes("F0B00A5E")
    Dim Longitude() As Int=bc.IntsFromBytes(b)
    Log(Longitude(0))
 
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
If you want to use a hexstring as input, you should convert the hexstring to int
B4X:
    Dim bc As ByteConverter
    Dim b() As Byte=bc.HexToBytes("F0B00A5E")
    Dim Longitude() As Int=bc.IntsFromBytes(b)
    Log(Longitude(0))
I have the following:
B4X:
                Dim myLon As String
                myLon = raw.SubString2(66,74)
                
                Dim bc As ByteConverter
                Dim b() As Byte=bc.HexToBytes(myLon)
                Dim Longitude() As Int=bc.IntsFromBytes(b)
                Log(Longitude(0))
I get the following error on compile:
B4X:
B4J Version: 9.80
Parsing code.    (0.01s)
    Java Version: 14
Building folders structure.    (0.02s)
Compiling code.    Error
Error compiling program.
Error description: Current declaration does not match previous one.
Previous: {Type=Int,Rank=0, RemoteObject=True}
Current: {Type=Int,Rank=1, RemoteObject=True}
Error occurred on line: 212
Dim Longitude() As Int=bc.IntsFromBytes(b)
 
Upvote 0

emexes

Expert
Licensed User
Try:
B4X:
Dim raw As String = "0123456789012345678901234567890123456789012345678901234567890.....F0B00A5E....."    'test sample

Dim myLon As String
myLon = raw.SubString2(66, 74)

Log(myLon)
 
'Dim bc As ByteConverter
'Dim b() As Byte=bc.HexToBytes(myLon)
'Dim Longitude() As Int=bc.IntsFromBytes(b)
'Log(Longitude(0))

Dim Longitude As Int = Bit.ParseLong(myLon, 16)    'there should be a law against this but, hey, if it works, it works
Log(Longitude)
Log output:
Waiting for debugger to connect...
Program started.
F0B00A5E
-256898466
 
Last edited:
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
I have the following:
B4X:
                Dim myLon As String
                myLon = raw.SubString2(66,74)
               
                Dim bc As ByteConverter
                Dim b() As Byte=bc.HexToBytes(myLon)
                Dim Longitude() As Int=bc.IntsFromBytes(b)
                Log(Longitude(0))
I get the following error on compile:
B4X:
B4J Version: 9.80
Parsing code.    (0.01s)
    Java Version: 14
Building folders structure.    (0.02s)
Compiling code.    Error
Error compiling program.
Error description: Current declaration does not match previous one.
Previous: {Type=Int,Rank=0, RemoteObject=True}
Current: {Type=Int,Rank=1, RemoteObject=True}
Error occurred on line: 212
Dim Longitude() As Int=bc.IntsFromBytes(b)
Many thanks for all you assistance.
This works:
B4X:
                Dim myLat As String
                Dim myLon As String
                myLat = raw.SubString2(58,66)
                myLon = raw.SubString2(66,74)
                    
                Dim b() As Byte=bc.HexToBytes(myLat)
                Dim Latitude() As Int=bc.IntsFromBytes(b)
                Log(Latitude(0))

                Dim b() As Byte=bc.HexToBytes(myLon)
                Dim Longitude() As Int=bc.IntsFromBytes(b)
                Log(Longitude(0))

                Log("Latitude: " & Latitude(0))
                Log("Longitude: " & Longitude(0))
 
Upvote 0
Top