Android Question Help: how to convert string to int big Endian

RushilD

Member
Licensed User
Hello All,

I am making an app for a project where I have a requirement to do the following

1. There is a string X
2. Convert the string to Md5 string
3. Truncate the string to Y
4.convert Y string to big Endian INT

I have successfully reached till step 3 but struggling at point 4
After searching the forums I was not able to find anything

can someone show how to convert the string to unsigned 32 Big Endian int ?

thank you
 

RushilD

Member
Licensed User
The question isn't well defined. Can you post a specific example?
Hi Erel,

basically how can I convert a string to unsigned 32 big Endian .

I found an example where the conversion is for little Endian .

but I cannot find an example for big Endian.

I will try to share an example .
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Either use Bit methods or include ByteConverter library. Java is BigEndian by default (source: https://www.b4x.com/android/forum/threads/byteconverter-littleendian.36126/post-212106)

B4X:
    Dim bc As ByteConverter
    Log(bc.LittleEndian)
    Dim test As Int = Bit.And(Bit.ParseLong("FFFFFFFF",16), 0xFFFFFFFF)
    Log(test)
    Log(Bit.ToHexString(test))
    Dim testBC As Int = bc.IntsFromBytes(bc.HexToBytes("FFFFFFFF"))(0)
    Log(testBC)
    Log(Bit.ToHexString(testBC))

With Bit, you first have to convert to a long (since you want unsigned) and then mask the long down to an integer. With ByteConverter, create an array of bytes from the hex string and then convert those bytes to an integer array, selecting the first array element (element 0) as your output. You may want to validate your input or wrap either method in a try/catch, just in case if invalid input.

Even though both numbers print out as -1, it does not matter. The bit representation is the same as the unsigned number you are looking for.
 
Upvote 0

RushilD

Member
Licensed User
I tried the following code


B4X:
  Dim bc As ByteConverter
  Dim DATA as String = "f1eea25a"
 
   Log(bc.LittleEndian)
 
  '  Dim test As Int = Bit.And(Bit.ParseLong("FFFFFFFF",16), 0xFFFFFFFF)
  ' Log(test)
  ' Log(Bit.ToHexString(test))
 
    Dim testBC As Int = bc.IntsFromBytes(bc.HexToBytes(DATA))(0)
    Log(testBC)
    Log(Bit.ToHexString(testBC))


Got the following log output


B4X:
false

-236019110

f1eea25a


Expected should be

4058948186


By using this website

if you input "f1eea25a"
select 32 bit unsigned interger and select big endian
the output is 4058948186
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Both numbers have the same bit format. Java just happens to display that bit format as an unsigned signed int. Unless you do computations with that number, you're good to go
 
Upvote 0

RushilD

Member
Licensed User
i found a solution in another post

B4X:
    Dim converter As ByteConverter
    
    Dim a(8) As Byte
    Dim i() As Byte = converter.HexToBytes(reduced_string)
    Bit.ArrayCopy(i, 0, a, 4, 4)
    Dim ll() As Long = converter.LongsFromBytes(a)
    Log(ll(0))

the log output is as expected now
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
4058948186
For display purposes, you'll have to convert the 32 bits into a long in order to display properly. But technically, the 32 bits represent the same 32 bits as an "unsigned" integer. In the end, the bits are the same, but how the system treats them is determined by signed / unsigned status.

B4X:
Dim test As Int = Bit.And(Bit.ParseLong("FFFFFFFF",16), 0xFFFFFFFF)
Log(test)
Log(UnsignedIntToLong(test))

Sub UnsignedIntToLong(input As Int) As Long
    Return Bit.ParseLong(Bit.ToHexString(input), 16)
End Sub

-1
4294967295

It all depends on what you are trying to accomplish. If you are trying to create a 32 bit representation of an unsigned integer that you later need to transfer to another system, store away, do some Bit operations on, then I've shown you one way of doing it. If all you are trying to do is display the content of a 32 bit representation of an unsigned integer, then I would go with @emexes solution above.
 
Upvote 0
Top