Android Question Convert number "long" to "hex"

vecino

Well-Known Member
Licensed User
Longtime User
Hello, how I can fix this?

Example:
B4X:
Dim fNum As Long = 856987458515
Dim cStr As String
'
cStr = Bit.ToHexString( fNum )
Log("cStr: "&cStr)    ' cStr:  886edbd3
I think the result is: C7886EDBD3

Thanks and regards.
 

DonManfred

Expert
Licensed User
Longtime User
B4X:
Dim bc As ByteConverter
Dim val(1) As Long
val(0) = 856987458515
Log(bc.HexFromBytes(bc.LongsToBytes((val))))

logs
000000C7886EDBD3

"Bit" works with an int, not long. But your value doesnt fix to an int
See documentation of Bit.ToHexString
 
Last edited:
Upvote 0

vecino

Well-Known Member
Licensed User
Longtime User
Hello, thank you

Then, to convert a long to octal, binary, etc.. What function is used?

Thanks and regards.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Find out how you can convert each long into INTs
with an int as base you can use the bit-command

Bit.ToBinaryString(intvalue)
Bit.ToOctalString(intvalue)
Dim data() As Byte
data = bc.LongsToBytes(val)

data now hold the single bytes... but i dont know how to convert it to an int which can be used with bit-command.
 
Upvote 0

vecino

Well-Known Member
Licensed User
Longtime User
I'm embarrassed to say this, but I do not understand what you said, I used the translate.google and nor understand.


I think it is time for me to learn English
 
Upvote 0

SPLatMan

Member
Licensed User
Longtime User
I just tried to wrap the above Long->Hex solution into a sub, but got a weird compile error. So I pasted just the sample code into my sub like so:

B4X:
Sub LongToHexStr(L As Long) As String
    Dim bc As ByteConverter
    Dim val(1) As Long
    Val(0) = 856987458515    '<<<<<<<<<<<
    Log(bc.HexFromBytes(bc.LongsToBytes((Val))))
End Sub

What I get is an error on the highlighted line:
Parsing code. 0.08
Compiling code. Error
Error compiling program.
Error description: Syntax error.
Occurred on line: 2214
Val(0) = 856987458515 '<<<<<<<<<<<
Word: =

What's wrong with that line? What does
mean?
 
Upvote 0

SPLatMan

Member
Licensed User
Longtime User
Thanks MaFu. You mean square brackets? Made no difference. But I did discover the problem after a good night's sleep ... I have a function called Val() elsewhere in my program!

What misled me was that the error message pointed at the line using array variable Val(0). If the declaration had been flagged as Dupe Def then it would have been clearer.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…