Android Question Converting to UTF-8

JTmartins

Active Member
Licensed User
Longtime User
Hi everyone, Let's see if I can explain my problem.

I have an array of unsigned values (they came as int's with some negative values). The only way I can receive the array is as an object from an external lib. (tried other possibilities, but always get cast errors)

I can convert array elements to signed, no problem there.

Now I want to convert that array of int's to characters (I guess they are utf-8 encoded), and I'm completely lost in this conversion.

Currently I have


B4X:
    Private Dados As Object
    Dados=CardReader.TransmitADPU (slotNum,SendBuffer,RecvBuffer)
    Private DadosUnsigned() As Int
    DadosUnsigned=CardReader.ConvertToUnsignedHex(Dados)
    Private PTEID (DadosUnsigned.Length) As string
    For f=0 To PTEID.Length -1
    PTEID(f)=Chr(DadosUnsigned(f))
    Next

This works, But accented chars come wrong.

Some Hint would be very helpfull, as to be honest I'm not getting it.

(actually I think the thread title should be different (sorry)



Thanks
José
 
Last edited:

JTmartins

Active Member
Licensed User
Longtime User
Here they are Erel. (Jpeg Attached)

I've learned in this forum that when you are suspicious of something, you are usually right, so your question made me look carefully into the data received from the card reader. I've realized that the UTF-8 chars I'm converting incorrectly are composed by 2 bytes. So if I join the -61 (0xFFFFFFC3) with the imediate following byte -70 (0xFFFFFFBA), and rip the "FFFFFF" out, that will give me C3BA and in the UTF-8 char table C3BA is the LATIN SMALL LETTER U WITH ACUTE, wich is precisely the character I want. How the hell I'm I going to convert this in a easy way ? I mean I have 1500 bytes of data with lots of situations like this.

currently puzzled !!! :)
 

Attachments

  • dados.jpg
    dados.jpg
    140.7 KB · Views: 284
Last edited:
Upvote 0

JTmartins

Active Member
Licensed User
Longtime User
That's more or less what I'v done, without success..-Characters still are coming out wrong.

I have tested with something similar with this

B4X:
    Private PTEID() As Int
    Private PTEID2() As Int
    Private PTEID3() As Int
    PTEID=CardReader.ByteArrayConversion(Dados)
    PTEID2=CardReader.ByteArrayConversion(Dados2)
    PTEID3=CardReader.ByteArrayConversion(Dados3)
    Private bc As ByteConverter
    Private info1() As Byte
    Private info1_2 As String
    Private info2() As Byte
    Private info2_2 As String
    Private info3() As Byte
    Private info3_2 As String
    Dim a As List : a.Initialize
    info1=bc.IntsToBytes (PTEID)
    info1_2=bc.StringFromBytes(info1,"utf-8")
    info2=bc.IntsToBytes(PTEID2)
    info2_2=bc.StringFromBytes(info2,"utf-8")
    info3=bc.IntsToBytes (PTEID3)
    info3_2=bc.StringFromBytes(info3,"utf-8")
    a.AddAll (PTEID):a.AddAll(PTEID2):a.AddAll(PTEID3)

It's a little messy, but for testing purposes it's ok and I have a bug here, as I think I'm converting twice to bytes.

I will have to dig deeper into this, and request your help when I exaust all my ideas. But many thanks Erel. You are five stars.
 
Upvote 0

JTmartins

Active Member
Licensed User
Longtime User
So doing things steps by step, I'm having casting problems.

If I do :


B4X:
Private info1() As Byte
    Private bc As ByteConverter
    info1=bc.IntsToBytes (dados)

It compiles but when running I get : java.lang.IllegalArgumentException: argument 1 should have type int[], got byte[]

and If I do :

B4X:
    Private info1() As String
    Private bc As ByteConverter
    info1=bc.StringFromBytes (dados,"UTF-8")

It does not compile and throws :
Compiling code. Error
Error compiling program.
Error description: Cannot cast type: {Type=String,Rank=0, RemoteObject=True} to: {Type=String,Rank=1, RemoteObject=True}
Occurred on line: 193
info1=bc.StringFromBytes (dados,"UTF-8")
Word: )

If I try (as a curiosity)


B4X:
Private info1() As Int
    Private bc As ByteConverter
    info1=bc.IntsFromBytes  (dados)

I get barbaric numbers as 1382379715 (0x526570C3) / -1167954839 (0xBA626C69) / 1667309648 (0x63612050)...etc

So I'v created a sub in CardReader code module as this :

B4X:
Sub ByteArrayConversion (barray() As Byte ) As String()
    Private arraysaida(barray.Length) As String
    For f=0 To barray.Length-1
    Private bc As ByteConverter
    arraysaida(f)=bc.StringFromBytes (barray(f),"UTF-8")
    Next
    Return arraysaida
   
End Sub

Then trwoing the Dados Object as a parameter, expecting it to be returned as a string..Same problems.

Grrrrr. getting crazy !!!!
 
Last edited:
Upvote 0

JTmartins

Active Member
Licensed User
Longtime User
SOLVED

B4X:
    Private info1() As Byte
    info1=Makebyte(Dados)
    Private S As String
    S=BytesToString(info1,0,info1.Length,"UTF-8")

and sub

B4X:
Sub Makebyte (entrada As Object ) As Byte()
Return entrada
End Sub


You're right as usual :)
 
Upvote 0
Top