Android Question ASCII Character to Decimal Value

aaronk

Well-Known Member
Licensed User
Hi,

When using the code below with the value1 as D it works. It Logs 20 which is correct.

However when I have value1 as Š it returns 15 when it should return 90.

Another few examples that are also failing to return the correct value:
When I have value1 as ‹ it should return 91.
When I have value1 as Πit should return 92.

Anyone have any ideas on where I have done wrong ?

I came across this ASCII chart which shows the Š ‹ Œ etc, so I would have thought my code should have worked, but maybe I am doing something wrong: http://www.aboutmyip.com/AboutMyXApp/AsciiChart.jsp

B4X:
Dim value1 as string = "D"
Log((HexToLong(AsciiToHex(value1))-48)). ' logs 20 which is correct

Dim value1 as string = "Š"
Log((HexToLong(AsciiToHex(value1))-48)). ' logs 15 which is wrong and should be 90

Sub AsciiToHex (a As String) As String 'ignore
    Try
        Dim bc As ByteConverter
        Return bc.HexFromBytes(a.GetBytes("ASCII"))
    Catch
    End Try 'ignore
End Sub

Private Sub HexToLong(HexStr As String) As Long    'ignore
    Try
        Dim Lng As Long
        Dim Digit As String
        Dim DigitVal As Int

        For i = HexStr.Length -1 To 0 Step -1
            Digit = HexStr.SubString2(i, i+1).ToUpperCase
            If Not("0123456789ABCDEF".Contains(Digit)) Then
                ' not valid char
                Return 0
            End If

            If Digit.CompareTo("9") > 0 Then
                DigitVal = Asc(Digit) - 55
            Else
                DigitVal = Digit
            End If
            Lng = Lng + DigitVal * Power(16, HexStr.Length - 1 - i)
        Next

        Return Lng
    Catch
        Log("error: HexToLong")
    End Try
End Sub
 

OliverA

Expert
Licensed User
Instead of using ASCII, try ISO-8859-1
 

aaronk

Well-Known Member
Licensed User
(I left some code in my first post which minus 48 from the value. Don't worry about that part, and refer to the below on what I am trying to do).

I will see if I can help understand it a little better..

Maybe this strange code isn't required, but not sure how to do it so I gave it a go.

Basically I am wanting to convert the Character to a Decimal Value.

You are correct in saying D is 68.
Š should be 138 as another example.

I am trying get the decimal value (138) from the Character (Š) as an example.

I have now put all my code in one function and hopefully helps understand what I am trying to do..

B4X:
    Log(GetDecimal_From_Character("D"))    ' returns 68 which is correct
    Log(GetDecimal_From_Character("Z"))    ' returns 90 which is correct
    Log(GetDecimal_From_Character("x"))    ' returns 120 which is correct
    Log(GetDecimal_From_Character("Š")) ' returns 63 but should have returned 138
    Log(GetDecimal_From_Character("€")) ' returns 63 but should have returned 128

Sub GetDecimal_From_Character(Character As String) As Int
    Dim HexStr As String
   
    Dim bc As ByteConverter
    HexStr = bc.HexFromBytes(Character.GetBytes("ISO-8859-1"))
   
    Dim Lng As Long
    Dim Digit As String
    Dim DigitVal As Int
   
    For i = HexStr.Length -1 To 0 Step -1
        Digit = HexStr.SubString2(i, i+1).ToUpperCase
        If Not("0123456789ABCDEF".Contains(Digit)) Then
            ' not valid char
            Return 0
        End If

        If Digit.CompareTo("9") > 0 Then
            DigitVal = Asc(Digit) - 55
        Else
            DigitVal = Digit
        End If
        Lng = Lng + DigitVal * Power(16, HexStr.Length - 1 - i)
    Next

    Return Lng ' return the Decimal Value
End Sub
 

Erel

Administrator
Staff member
Licensed User
Why aren't you using unicode?

B4X:
Dim codepoint As Int = Asc("Š")
 

aaronk

Well-Known Member
Licensed User
I couldn't get that to work correctly.

When I tried it turned higher number:

B4X:
Log(Asc("Š")) ' returns 352
 

amykonio

Member
Licensed User
The following site shows it as 138 and this is the value I need to use.
This is in the extended ascii table. Original ascii table has 128 chars (0-127 -> 2^7).
Extended ascii table was displaying different characters in the range bellow 127, which were characters based on specific code pages...

Example in ascii table in greek cp 869 shows character | with dec value 138.
And ascii table in greek cp 737 show greek capital "Κ" with dec value 138.

Unicode came to solve this kind of issues.
So you can see that ascii table isn't a "universal" standard. Thats why @Erel suggested to use unicode... And yes, 30 years have passed since nineties. :)
 
Top