Android Question [Solved] How can I convert this string (number) to int??

Dadaista

Active Member
Licensed User
Longtime User
Hi

I am not able to convert a string in to a number

The string is "0x416" and I want to convert into a int

This woks:
B4X:
Dim vendor As Int = 0x416

But I have the "number" in a text file. I read the "number" from text file but does not work

and this is the error
B4X:
java.lang.NumberFormatException: Invalid hex double:0x416

How can I solve it??

Thx in advance
 

Dadaista

Active Member
Licensed User
Longtime User
Hi Mahares

Thx for your response. No, does not work.

I think I need hexadecimal to decimal converter
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Thx for your response. No, does not work.

The code I posted for you in my previous post does the same thing you are trying to do with this below code. If not, what is your purpose of converting the string "0x416" to integer:
B4X:
Dim vendor As Int = 0x416
 Label1.Text=Chr(vendor)

The label text will be this icon symbol:
 

Attachments

  • IconSymbol.png
    6.5 KB · Views: 372
Upvote 0

Dadaista

Active Member
Licensed User
Longtime User
Hi Again Mahares

Thx for your reply

I need the number in decimal.
0x416 is 1046 in decimal. that is what I need. The code you posted in your first post does not work for me

the code below, does not work either... for "0x416" returns -1051626

B4X:
#if java
import java.util.Scanner;

public static int hex2decimal(String s)
    {
             String digits = "0123456789ABCDEF";
            
             int val = 0;
             for (int i = 0; i < s.length(); i++)
             {
                 char c = s.charAt(i);
                 int d = digits.indexOf(c);
                 val = 16*val + d;
             }
             return val;
    }
#End If

I do not know what can I do
 
Upvote 0

Dadaista

Active Member
Licensed User
Longtime User
Hi.

thx Peter and Maharus

I have made a sub to convert from hex to dec. and Works!!!!

Regards
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
Really, a sub?
As stated previously you just need ByteConverter and a couple of lines of code.
B4X:
'Something like this, NOT TESTED
    Dim BC As ByteConverter
    Log($"Value ${BC.IntsFromBytes(BC.HexToBytes("0x416".Replace("0x", "00000")))(0)}"$)
 
Upvote 0

Dadaista

Active Member
Licensed User
Longtime User
Hi Peter

Your Code does not work

B4X:
java.lang.Exception: hex string has odd number of characters

And for the "number" 0x5011 returns 80.... it must returns 20497

NeverMind, the sub I made works

Thx!!
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
B4X:
Waiting for debugger to connect...
Program started.
Value 20497
Value 1046
Picked up _JAVA_OPTIONS: -Xmx512m -Xms512m

Not according to the logs above. It only took an extension to the line to get it working with both 0x000 and 0x0000 values.

Goodnight...
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
it was a string too...

B4X:
Dim strCC As String="0x416"

Edit: I tried that code and it works as supposed to be returning that 1048 or whatever it was.
 
Last edited:
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I need the number in decimal.
0x416 is 1046 in decimal. that is what I need

@Dadaista: The code I put in post#2 was tested and it does what you asked for. The Log(intCC)
displays 1046 which is what you said you want to see in post#5. You need to show us your code that you are hiding from us. Please share your code with us. Perhaps we can learn something. The forum is a two way street. You must also give not just take.
I tried that code and it works as supposed to be returning that 1048 or whatever it was.
@Mahares works as it should do, I have no idea why @Dadaista couldn't get it working.

I know it works because I tested it before posting unless @Dadaista says something and means something else.
 
Upvote 0

Dadaista

Active Member
Licensed User
Longtime User
Hi

No problem I share the code. I do not know why code from @Mahares does not work for me ... but @Mahares returns a symbol and I need a number

I wrote a quickly sub last night. I was in a great hurry. It is awful but by the moment works. More later I will modify the code for it nice seems


B4X:
Sub Hexa2Decimal (sNumber As String) As Int
'    Dim BC As ByteConverter
'    'Log($"Value ${BC.IntsFromBytes(BC.HexToBytes(sNumber.Replace("0x", "00000")))(0)}"$)    ' 1046
'    Return BC.IntsFromBytes(BC.HexToBytes(sNumber.Replace("0x", "00000")))(0)
    Dim i, k As Int = 0
    Dim iSuma As Int = 0
    Dim sCar As String
    Dim iBase As Int = 1
 
    If sNumber.Contains("h") Then
        sNumber = sNumber.SubString2(0,sNumber.Length-1)
    End If
    If sNumber.Contains("0x") Then
        sNumber = sNumber.SubString(3)
    End If
 
    For i = sNumber.Length -1 To 0 Step -1
        sCar = sNumber.CharAt(i)
        Select sCar
            Case "A", "a"
                sCar = "10"
            Case "B", "b"
                sCar = "11"
            Case "C", "c"
                sCar = "12"
            Case "D", "d"
                sCar = "13"
            Case "E", "e"
                sCar = "14"
            Case "F", "f"
                sCar = "15"
        End Select
        'If k = 0 Then iBase = 1
        If k = 1 Then iBase = 16
        If k = 2 Then iBase = 256
        If k = 3 Then iBase = 4096
        If k = 4 Then iBase = 65536
        If k = 5 Then iBase = 1048576
        iSuma = iSuma + (sCar * iBase)
        k = k + 1
    Next
    Return iSuma
End Sub
 
Last edited:
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
The symbol just represents the Chr value, Log(intCC) showed you the actual int value.

Interesting code @Dadaista, but why didn't you just convert the code in your post #5, the code was already there waiting for you to use.

Converted using your original java code in post 5#
B4X:
    Dim HexValue As String= "0x416"   ' or whatever HEX value you need.
    Log("Value " & Hex2Decimal(HexValue.Replace("0x", "")))

Sub Hex2Decimal(s As String) As Int
    Dim Digits As String = "0123456789ABCDEF"
    Dim Val = 0, I As Int = 0
 
    Do While I < s.length()
        Dim C As Char = s.charAt(I)
        Dim D As Int = Digits.indexOf(c)
        Val = 16 * Val + D
        I = I + 1
    Loop
    Return Val
End Sub

Oh well no harm done, at least you got you code working @Dadaista which is the main thing...
 
Upvote 0

Dadaista

Active Member
Licensed User
Longtime User
Interesting code @Dadaista, but why didn't you just convert the code in your post #5, the code was already there waiting for you to use.

I do not know why the code of the post#5 did not works for me ... is amazing!! ... but I do not write java code... I do not know... just found in the www and copy & paste

You can also simplify your SELECT...CASE by doing this:
thx @NJDude

But I will test the sub o post#18 from @Peter Simpson, although I suppose it works... and I changed for the mine. His code is much more elegant.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…