B4J Question Unicode sequence in module

warwound

Expert
Licensed User
Longtime User
I'm re-creating some java code in b4j and have this line of java:

B4X:
String unicode = "\u20AC\u0020\u201A\u0192\u201E\u2026\u2020\u2021\u02C6\u2030\u0160";

So in my b4j Main module i have written:

B4X:
Dim Unicode As String="\u20AC\u0020\u201A\u0192\u201E\u2026\u2020\u2021\u02C6\u2030\u0160"

The unicode sequence is passed to a class that is part of a PDF creation library.
The java example for this library renders as the unicode sequence in a PDF document:

€ ‚ƒ„...†‡ˆ‰Š

My b4j example renders the unicode sequence literally:

\u20AC\u0020\u201A\u0192\u201E\u2026\u2020\u2021\u02C6\u2030\u0160

Looking at the compiled main.java source file for my b4j example the relevant line is:

B4X:
_unicode = "\\u20AC\\u0020\\u201A\\u0192\\u201E\\u2026\\u2020\\u2021\\u02C6\\u2030\\u0160";

Each slash in my sequence has been escaped - is this preventing the sequence from being recognised as a unicode sequence?
Is there a supported method to pass a unicode sequence from a b4j module to a library method?

Thanks.

Martin.
 

Daestrum

Expert
Licensed User
Longtime User
I use js to convert unicode as it handles unicode natively. I wrote a tiny lib, that just contains a scriptengine.
B4X:
Sub readUnicode(st As String) As String
    Dim m As MyEval
    m.evalString("var x = '" & st & "';")
    Return m.engineGet("x")
End Sub
the java source for the lib is :
B4X:
package myEval;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

import anywheresoftware.b4a.BA.Author;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;
@ShortName("MyEval")
@Version(1.0f)
@Author("Daestrum")
public class MyEval {
    ScriptEngineManager factory = new ScriptEngineManager();
    ScriptEngine engine;
    Object reply;

    public MyEval() {
        this.engine = factory.getEngineByName("JavaScript");
    }
    public Object evalString(String s){
        try {
            reply =  this.engine.eval(s);
        } catch (ScriptException e) {
            e.printStackTrace();
            reply = null;
        }
        return reply;
    }
    public void enginePut(String a,Object b){
        this.engine.put(a, b);
    }
    public Object engineGet(String a){
        return this.engine.get(a);
    }
}
Plus the library exposes js for use in B4J.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Sorry Erel, I will post it in there with a documented B4J project to show how to use it for people not familiar with javascript engine.

Would jScriptEngine be ok for it's name ?
 
Upvote 0
Top