Android Question String Manipulation Of hex string

Izmirov.Yaminovich

Member
Licensed User
Good day to all of you, I want to ask something

I have hex like this

6B800800 42BBD001 BD769B3C DA010100 BAFFFFFF 00000000 01009A53 34DB


1. I want to get the 42BBD001 hex and turn it structure like this 01D0BB42. Is it possible? Is it using string manipulation (char at()) ) and store it to an array and construct it again by calling one by one or is there any other method?

2. Second of all, how to convert the hex to decimal? Is it use the "hextoByte" function and pass it to the other function "IntfromByte" that found in the byteconv library?I want to turn this hex "01D0BB42" to ssomething like this in decimal "30456642"

Thanks in advance
 

sorex

Expert
Licensed User
didn't test it but this might work for question 1 & 2

B4X:
dim t as string ="6B800800 42BBD001 BD769B3C DA010100 BAFFFFFF 00000000 01009A53 34DB"
dim data() as string
data=regex.split(" ",t) 'split elements
data=regex.split("",data(1)) 'take the 42BBD001 element
t=data(6)&data(7)&data(4)&data(5)&data(2)&data(3)&data(0)&data(1) 'reverse it
log ( Bit.ParseInt(t, 32) ) ' show as non hex value
 

James Chamblin

Active Member
Licensed User
didn't test it but this might work for question 1 & 2

B4X:
dim t as string ="6B800800 42BBD001 BD769B3C DA010100 BAFFFFFF 00000000 01009A53 34DB"
dim data() as string
data=regex.split(" ",t) 'split elements
data=regex.split("",data(1)) 'take the 42BBD001 element
t=data(6)&data(7)&data(4)&data(5)&data(2)&data(3)&data(0)&data(1) 'reverse it
log ( Bit.ParseInt(t, 32) ) ' show as non hex value
two problems
Line 6 should read log( Bit.ParseInt(t, 16) )

line 4 for some reason wants to put an empty string in data(0). Might be a bug in regex?
 

Izmirov.Yaminovich

Member
Licensed User
Good day to all of you. Thanks for help me out of my problem.

I have change a little bit of your coding and now it work.

B4X:
Dim t As String ="6B800800 42BBD001 BD769B3C DA010100 BAFFFFFF 00000000 01009A53 34DB"
Dim data() As String
data=Regex.split(" ",t) 'split elements
data=Regex.split("",data(1)) 'take the 42BBD001 element
t=data(7)&data(8)&data(5)&data(6)&data(3)&data(4)&data(1)&data(2) 'reverse it
Log ( Bit.ParseInt(t, 16) ) ' show as non hex value
End Sub
For some reason data(0) hold value of 0. I have change it like the above code.

Cheers
 

sorex

Expert
Licensed User
For some reason data(0) hold value of 0. I have change it like the above code.
right, that's different and only the case when using "" in the split. the first element is at position 1 instead of 0.

not sure why because in other languages it's at 0, it's something android specific it seems.
 

James Chamblin

Active Member
Licensed User
I'm thinking it might be a bug in the Java implementation of regex. The regular expression "" might be making a match at the start of the string and not just between each character.

Calling the sub below instead of regex.split() will work as expected.
B4X:
Sub StringSplit(s As String) As String()
    Dim a(s.Length) As String
    Dim i As Int
    For i = 0 To s.Length - 1
        a(i) = s.CharAt(i)
    Next
    Return a
End Sub
 
Last edited:
Top