Android Question ByteConverter

Steve_B

Member
Licensed User
Longtime User
I'm having problems with the ByteConverter libary. I read the bytes from the file, and if I move the byte array to the TextEdit they are in fact the value of the ascii
chars in the file. When I BConverter.CharsFromBytes and move the char buffer to the TextEdit they're displayed as chinese chars. I then added an ascii string to the TextEdit and it's displayed correctly

Sub
WriteFile_Click

Dim buffer(1000) As Byte

Dim cbuff(1000) As Char
Dim In1 As InputStream

Dim BConverter As ByteConverter

Dim count As Int


In1 = F
ile.OpenInput(File.DirRootExternal,"FSCREEN.TXT")

In1.ReadBytes(buffer,
0,100)


cbuff = BConverter.CharsFromBytes(buffer)

For count = 0 To 100 Step1

TxtEdit.Text = TxtEdit.Text & buffer(count) & " "

Next


For count = 0To100Step1

TxtEdit.Text = TxtEdit.Text & cbuff(count)

Next

TxtEdit.Text = TxtEdit.Text & " SSSSSS"

End Sub
 

Steve_B

Member
Licensed User
Longtime User
Please use [ code ] [ /code ] tags (without spaces) when posting code.

What is the output of Log(cbuff) ?

This is my first post, so I'm still learning the ins and outs. I'm new to android programming so I'm unsure of how to use Log().
I put a Log(cbuff) after the cbuff = BConverter.CharsFromBytes(buffer), and when I run the program I get what looks like an address
in the log screen [C@410ab918.


Also when the code TxtEdit.Text = TxtEdit.Text & " SSSSSS" executes the S's apear on the screen, but they have a red underline.
 
Upvote 0

Steve_B

Member
Licensed User
Longtime User
Don't use bc.CharsFromBytes. Use bc.StringFromBytes instead (or use BytesToString keyword).


I tried StringFromBytes, but it requires a second parameter "Encoding as string". I searched the web and been unable to find an example to
see what I need for the encoding parameter.
 
Upvote 0

Steve_B

Member
Licensed User
Longtime User
Use "UTF-8" as the Encoding string.
You might have a look at chapter 14.10.6 Text encoding in the Beginner's Guide.

StringFromBytes (bytes() AsByte, encoding AsString) AsString
Returns a string containing the bytes in the array interpreted as characters according to encoding

I had tried cbuff = BConverter.StringFromBytes(buffer,"ASCII") then
cbuff = BConverter.StringFromBytes(buffer,"UTF8")

when I got your reply I tries
cbuff = BConverter.StringFromBytes(buffer,"UTF-8") I even tried

Dim ST as string = "UTF-8"
cbuff = BConverter.StringFromBytes(buffer,ST)

But every time I get a compile error

Cannot cast type: {Type=String,Rank=0, RemoteObject=True} to: {Type=Char,Rank=1, RemoteObject=True}

I know I'm making a trivial mistake somewhere, but I know not where to look.
 
Upvote 0
Top