Android Question Hex to Decimal

Beja

Expert
Licensed User
Longtime User
Hi all,
Is there a function to convert hex to integer value (decimal value)>> there are some but I though maybe some has a better solution with simple and shorter function.
 

Beja

Expert
Licensed User
Longtime User
Hi teddybear, Thanks but I thought Byte is Hex
I have a hex value in string (EditTex) and want to convert it to decimal.. (FF >> 256)
inline java is ok.
 
Upvote 0

rbghongade

Active Member
Licensed User
Longtime User
Dear Beja,
Please try this:

Hex to Decimal:
Sub HexToDecimal(hex As String) As Int
    Try
        Dim decimalValue As Int
        decimalValue = Bit.ParseInt(hex, 16) ' Convert hex to decimal
        Return decimalValue
    Catch
        Log("Error: Invalid Hexadecimal value")
        Return -1
    End Try
End Sub
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
I found this code snippet somewhere and want to reverse the conversion parts so the data is displayed as it comes in (not converted to Hex)


B4X:
Sub AStream_NewData (Buffer() As Byte)
    Dim hexString As String = BytesToHex(Buffer)
    ParseAndDisplayData(hexString)
End Sub

Sub BytesToHex(bytes() As Byte) As String
    Dim sb As StringBuilder
    sb.Initialize
    For i = 0 To bytes.Length - 1
        Dim intValue As Int = Bit.And(bytes(i), 0xFF) ' Safely convert byte to integer
        Dim hexString As String = Bit.ToHexString(intValue).ToUpperCase ' Convert to uppercase hex
        sb.Append(hexString).Append(" ")
    Next
    Return sb.ToString.Trim
End Sub

Sub ParseAndDisplayData(data As String)
    Dim parsedData As String = data'.Replace("00", "00") ' Remove the "00" termination
    If parsedData.Length > 0 Then
        lblData.Text = parsedData
    End If
End Sub
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
Thanks to all..
Found this and it worked, I tested it in B4A and B4J.. fast and perfect


result =Bit.ParseInt("FF",16)
xui.MsgboxAsync(result, "B4X")
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
"I feel like your next wondering will be why are Byte values are -128 to 127 rather than 0 to 255, and the solution is:"

In fact I finished this calculation in 1984 AD, 4682 in Chinese calendar and 1404 in Hijri calendar and 5744 in Jewish calendar. You do the arithmatic.
 
Upvote 0

rbghongade

Active Member
Licensed User
Longtime User
You forgot Vikram Samvat 2044 according to Hindu Calendar!
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…