0

How can I "translate" this XSLT code to Java ?

<xsl:value-of select="number(string-to-codepoints(upper-case($char)) - string-to-codepoints('A'))+10"/>

I only know that: "The fn:string-to-codepoints function returns a sequence of xs:integer values representing the Unicode code points."

From the example that is given in (http://www.xsltfunctions.com/xsl/fn_string-to-codepoints.html) :

string-to-codepoints('a') = 97

I found this:

char ch = 'a';
System.out.println(String.format("\\u%04x", (int) ch));

But I get : \u0061

user2144555
  • 1,315
  • 11
  • 34
  • 55

2 Answers2

2

For a single char you can just cast it to int to get the decimal value:

System.out.println((int)ch);

For a String there's .toCharArray() to convert it to a char[] but that isn't quite the same as a "sequence of codepoints" if the String involves Unicode characters outside the BMP (i.e. above U+FFFF), which are represented in Java as a surrogate pair of two char values. To handle surrogates properly you would need to use a technique like the one described in this answer.

To answer the specific question you ask, you can do

number(string-to-codepoints(upper-case($char)) - string-to-codepoints('A'))+10

in Java as

char ch = // wherever you get $char from
int num = Character.toUpperCase(ch) - 'A' + 10;

since char is an integer type in Java and you can add or subtract char values like any other number.

But this will probably only give you a sensible answer when the initial ch is an ASCII letter.

Community
  • 1
  • 1
Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
1

You print the value as unicode escape sequence; XSLT prints a decimal value.

This should work much better:

System.out.println("a".codePointAt(0));
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820