1

So I'm trying to understand how a special character normally seen as '<' could be converted to '+ADw-' in UTF-7.

Is there an online tool or a built in library in JavaScript or Java that can do this?

What is the math behind this? I know that UTF-7 uses 7bits to store the character, so I am guessing that the '+ADw-' is just the numerical representation of '<' in ASCII? Meaning, if you converted to '<' to a number, that would be equal to '+ADw-' as a number?

Thanks!

eternalminerals.com
  • 395
  • 1
  • 4
  • 14
  • No, UTF-7 uses one byte to store a 7-bit value, which is either the entire codepoint or some of its 21 bits, depending on the codepoint being encoded. – Tom Blodget Sep 08 '15 at 22:42

1 Answers1

2

Java itself does not have UTF-7 support.

But that library provides a UTF-7 charset implementation and when you add its jar to your Java application you can simply write:

OutputStreamWriter out = new OutputStreamWriter(System.out, "UTF-7");
out.write("<");

to see how a string is translated into UTF-7.

wero
  • 32,544
  • 3
  • 59
  • 84
  • Hey I tried using that but when I looked in the source of the jutf7 library it doesn't have a OutputStreamWriter, it uses java.io.OutputStreamWriter: Using this code I get no result, but I didnt just include the jar though, I copied all the src into my Test project in eclipse: – eternalminerals.com Sep 08 '15 at 15:47
  • import java.io.IOException; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; public class Test { public static void main(String[] args) { OutputStreamWriter out; try { out = new OutputStreamWriter(System.out, "UTF-7"); out.write("<"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } – eternalminerals.com Sep 08 '15 at 15:47
  • Yes the `OutputStreamWriter` in my example is `java.io.OutputStreamWriter`. Copying the sources to your project might not be enough. If you look into the `jutf7-1.0.0.jar` there is also a file `META-INF/services/java.nio.charset.spi.CharsetProvider` which is needed to register the UTF-7 charset. Therefore include this file as resource to your project or simply use the jar. – wero Sep 08 '15 at 15:52
  • Following the usage info in the src, I get bad results too: java -jar jutf7-1.0.0.jar encode < The syntax of the command is incorrect. java -jar jutf7-1.0.0.jar encode a a C:\Downloads\jutf7-1.0.0\jutf7-1.0.0>java -jar jutf7-1.0.0.jar enc de b b C:\Downloads\jutf7-1.0.0\jutf7-1.0.0>java -jar jutf7-1.0.0.jar enc de ss ss C:\Downloads\jutf7-1.0.0\jutf7-1.0.0>java -jar jutf7-1.0.0.jar enc de 1 1 C:\Downloads\jutf7-1.0.0\jutf7-1.0.0>java -jar jutf7-1.0.0.jar enc de < The syntax of the command is incorrect. – eternalminerals.com Sep 08 '15 at 15:55
  • wero, thank you so much for your guidance. I added the jar AND the src to the build path of the sample project in eclipse but its still not displaying anything. I will continue to tinker around with this and keep you posted. – eternalminerals.com Sep 08 '15 at 15:57
  • I think this post/person is having/was having the same issue I have now http://stackoverflow.com/questions/6308587/loading-a-java-charset-manually – eternalminerals.com Sep 08 '15 at 15:59