B4J Question BytesToString Charset

MarrowZero

Member
Licensed User
Hello all,

Where do I find the valid CharSet parameters values? If I use "utf8" it works, but if I try "ANSI" it does not work.




Working Example UTF8:
Log(BytesToString(b, 16508,32, "utf8"))

Not Working Example ANSI:
Log(BytesToString(b, 16508,32, "ANSI"))

Error occurred on line: 37 (B4XMainPage)
java.io.UnsupportedEncodingException: ANSI
    at java.base/java.lang.StringCoding.decode(StringCoding.java:243)
    at java.base/java.lang.String.<init>(String.java:467)
    at anywheresoftware.b4a.keywords.Common.BytesToString(Common.java:609)
    at b4j.example.b4xmainpage._button2_click(b4xmainpage.java:87)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:632)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:234)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:108)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
    at anywheresoftware.b4a.BA$1.run(BA.java:233)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    at java.base/java.lang.Thread.run(Thread.java:834)


B4J v9.00

Thanks
--Paul
 

Attachments

  • 1623492720995.png
    7.1 KB · Views: 172
  • 1623493067142.png
    6.2 KB · Views: 172

MarrowZero

Member
Licensed User
I'd like to feedback that if the CharSet value is missing, you'll get a compile error. Its not clear to me that the CharSet parameter is required (maybe again I miss something about documentation/expectations, I am new to B4X world, please correct me)

Compile error if paramter not specified:
Error compiling program.
Error description: ',' expected.


Also, if the CharSet value is an empty string, the code will complile but then gives a runtime error

Runtime error if Charset parameter is empty string:
Log(BytesToString(b, 16508,32,""))


==============
Error occurred on line: 30 (B4XMainPage)
java.io.UnsupportedEncodingException:
    at java.base/java.lang.StringCoding.decode(StringCoding.java:243)
    at java.base/java.lang.String.<init>(String.java:467)
    at anywheresoftware.b4a.keywords.Common.BytesToString(Common.java:609)
    at b4j.example.b4xmainpage._button1_click(b4xmainpage.java:72)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:632)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:234)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:108)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
    at anywheresoftware.b4a.BA$1.run(BA.java:233)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    at java.base/java.lang.Thread.run(Thread.java:834)
 
Upvote 0

MarrowZero

Member
Licensed User
Yep, 'ASCII' works and thanks for the link to that thread about Encoding. However, how can I find the true defined list of acceptable values of CharSet ?

For example, UTF8 vs utf-8, US-ASCII vs ASCII vs WINDOWS-1252
Does case matter?
I would prefer not to guess, just be told what the value are in black and white.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Case doesn't matter. UTF8, UTF-8, ASCII and WINDOWS-1252 will work on all devices.

You can use this code to find all available charsets, this list can vary:
B4X:
Sub GetCharsets As List
    Dim Charset As JavaObject
    Dim map As JavaObject = Charset.InitializeStatic("java.nio.charset.Charset").RunMethod("availableCharsets", Null)
    Dim o() As Object = map.RunMethodJO("keySet", Null).RunMethod("toArray", Null)
    Return o
End Sub
Don't use it unless you are doing something very special.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…