Convert a string variable in B4A from UTF8 to 8 bit encoding (ANSI)

tdocs2

Well-Known Member
Licensed User
Longtime User
Greetings all, and once again, thank you for answering my question.

Summary
What B4A code can I use to convert a string variable (encoded in UTF-8) to 8 bit encoding (ANSI?)?

Background

I have a set of files in different languages in my DirAssets all encoded in UTF-8. My app uses parts of these files to display information to the user. The user can select portions of these files and compose a text that he sends to email addresses he designates. This works just fine.

A second feature of the app is to send SMS. SMS has a restriction of 140 characters - better defined as 140 octets = 140 * 8 bits = 1120 bits. This works fine for ANSI, but for UTF-8, the SMS limit is 70 characters which apparently require 16 bits per character.

Question

For languages that can be supported with 8 bit characters, English, Spanish, French, Italian, etc., what B4A code can I use to convert the string variable (encoded in UTF-8) to be sent via SMS to 8 bit encoding (ANSI?)? That way, the user will be able to send the full 140 characters in the SMS.

I have looked in the Forum, in the documentation, but I cannot seem to find a solution.

Any and all help will be welcomed and appreciated.
 

boredsilly

Member
Licensed User
Longtime User
Had a similar problem just yesterday.
I think you may have to convert the string to bytes
e.g

Dim bytedata() as byte

bytedata=ucodestring.getbytes("ASCII")

And send the sms message using the byte array.
 
Last edited:
Upvote 0

tdocs2

Well-Known Member
Licensed User
Longtime User
Thank you

Thank you for your help.

I was experimenting with the conversion to bytes, and you confirmed I was in the ball park.

For your solution to work, you have to then convert the bytes back to a string.

Returns : String

BytesToString(Data As Byte(), StartOffset As Int, Length As Int, CharSet As String)
 
Upvote 0

Dim Baznr

Member
Licensed User
Longtime User
I use these two conversion utilities:

B4X:
Sub Utf2Bytes( s As String ) As Byte()
    Return s.getbytes("UTF-8")
End Sub

Sub Bytes2Utf( b() As Byte ) As String
    Return BytesToString( b, 0, b.Length, "UTF-8" )
End Sub

Ex: Log( Bytes2Utf( Utf2Bytes( "αβγδεζηθ" ) ) ) ' --> αβγδεζηθ
 
Last edited:
Upvote 0
Top