B4R Question B4R coding problem

santook

Member
Currently, B4R USES UTF8 encoding for non-ascii characters.
Can you add an option in the B4R IDE to allow the user to choose the encoding type? For example GB2312, utf-32, ASCII and so on.
This is useful for some LCD applications that have a font library.
 

Cableguy

Expert
Licensed User
Longtime User
Do remember that you can use inline C for functions that are still not available in b4r...
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Inline C will not help here.

You can run this B4J code to create an array of bytes with any encoding you like:
B4X:
Sub StringToArrayOfBytes(s As String, Encoding As String) As String
   Dim sb As StringBuilder
   sb.Initialize
   sb.Append("Array As Byte(")
   For Each b As Byte In s.GetBytes(Encoding)
       sb.Append(Bit.And(0xff, b)).Append(",")
   Next
   sb.Remove(sb.Length - 1, sb.Length)
   sb.Append(")")
   Return sb.ToString
End Sub

Example:
B4X:
Log(StringToArrayOfBytes("שלדךגחשדךלג", "utf-32"))
Output:
Array As Byte(0,0,5,233,0,0,5,220,0,0,5,211,0,0,5,218,0,0,5,210,0,0,5,215,0,0,5,233,0,0,5,211,0,0,5,218,0,0,5,220,0,0,5,210)

Use it instead of using a constant string.
 
Upvote 0

santook

Member
I know this method, I mean, it would be better to assume that B4R can provide this kind of functionality, because direct text input is more intuitive.
Suggested methods:
(1) an optional encoding option is provided in the IDE, and the string that starts with "" is translated into the corresponding encoding before compilation.
(2) allow the IDE to use a special replacement blessing, such as "! # UTF8, Hello World! #!" Or "!" # ASCII, Hello World! #!"
 
Upvote 0
Top