2

I'm trying to print to a device which supports CP866 encoding only.

Unfortunately the device from which I'm printing (an Android device) does not support CP866, resulting in "abc".getBytes("CP866") throwing the UnsupportedEncodingException.

So, I guess, I have to do Unicode to CP866 encoding myself. Is there any freeware java library that does that?

Charles
  • 50,943
  • 13
  • 104
  • 142
Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158
  • According to [this page](http://docs.oracle.com/javase/7/docs/technotes/guides/intl/encoding.doc.html) CP866 is supported out-of-the-box on every install of the Oracle Java 7 Runtime. What JRE/JDK do you use? Which version? – Joachim Sauer Jun 17 '13 at 10:54
  • Sidenote: "freeware" is an ... ancient term with a very specific meaning. Hardly any software these days is released as freeware. You're *probably* looking for either "open source" or "free software" (or simply "freely available" software). – Joachim Sauer Jun 17 '13 at 10:59
  • 1.6 because my sending device is an Android phone – Alexander Kulyakhtin Jun 17 '13 at 11:17
  • so the code runs on Android? Then chances are they've got an *entirely different* (and probably much smaller) set of supported encodings. Why not just [transmit data as UTF-8](http://utf8everywhere.org/)? Everyone supports *that*. – Joachim Sauer Jun 17 '13 at 11:18
  • Nah, the receiving device supports only Cp866. It's a Russian device. – Alexander Kulyakhtin Jun 17 '13 at 11:19

4 Answers4

3

According to the Oracle documentation, Cp866 is a supported encoding for Java 7. So either

  • you are using an old version of Java that doesn't support Cp866 (e.g. see @Joachim's comment!!!), or
  • the Java runtime is not recognizing the name you are using. (The canonical name for the charset is "Cp866" not "CP866".)

UPDATE - it is unlikely to be the latter. From what I can make out from the source, the charset lookup mechanism used by the standard "provider" is case insensitive.

References:

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • 1
    In Java 5 that encoding was [provided by `charsets.jar`](http://docs.oracle.com/javase/1.5.0/docs/guide/intl/encoding.doc.html), which isn't always installed (depending on the language settings of the computer). [Java 6 already had it in `rt.jar`](http://docs.oracle.com/javase/6/docs/technotes/guides/intl/encoding.doc.html). – Joachim Sauer Jun 17 '13 at 10:55
  • In Java 6 this is supported (according to the [documentation](http://docs.oracle.com/javase/6/docs/technotes/guides/intl/encoding.doc.html)). – Uwe Plonus Jun 17 '13 at 10:56
  • My sending device is an Android device. It has Java 1.6. "abc".getBytes("Cp866") throws an exception. So do I use any other code instead? – Alexander Kulyakhtin Jun 17 '13 at 11:18
  • 1
    @Alex: Android is **not** Java 1.6. It's "something similar to Java" (google for Dalvik). I assume they only support a small set of encodings (because there's less of a legacy story on Android). – Joachim Sauer Jun 17 '13 at 11:19
  • Hm ... [this question](http://stackoverflow.com/questions/15074225/android-adding-a-cp866-charset) is related, but doesn't have a full answer. – Joachim Sauer Jun 17 '13 at 11:23
1

The class java.nio.charset.Charset supports both Cp866 and of course Unicode. I guess you could use that with the encode and decode methods.

Mena
  • 47,782
  • 11
  • 87
  • 106
1

http://msdn.microsoft.com/en-us/goglobal/cc305166 has the list of characters; should no pre-made option work, writing code to iterate through an array translating Unicode characters to bytes suitable for CP866 shouldn't take much time at all.

prosfilaes
  • 1,268
  • 13
  • 17
0

I was needed to encode string with Cp866 in android. You can use java library with made up charset classes. Cp866 among them.

This is the link: http://www.doc.ic.ac.uk/~awl03/cgi-bin/trac.cgi/miro/browser/trunk/gcc/libjava/classpath/gnu/java/nio/charset

If you want extend Charset class and add you private Charset: Java NIO. Chapter 6 Character sets