1

I have a byte[] containing the value -110 (and other negative values). When I convert it to string I need it to display a ’ (right single quote). Currently, I am getting a question mark (?)

The ’ aligns to the special ASCII character #146 mentioned in this page but I am now stuck as to how I can input -110 or 146 (-110+256) and be a ’ value. I have also trued Any advice would be greatly appreciated.

byte[] b = {-110,84};
System.out.println(new String(b, Charset.forName("Windows-1252"))); //Displays ?T . The desired output should be ’T
System.out.println(new String(b, Charset.forName("UTF-8"))); //Displays ?T . The desired output should be ’T
System.out.println(new String(b, Charset.forName("ISO-8859-1"))); //Displays ?T . The desired output should be ’T
6
  • Your console probably can't print that character. When I try your code, it works fine with the first variant. Commented Nov 7, 2017 at 12:04
  • Your link doesn't work, but U+0092 is a private use character. Note that ASCII doesn't contain anything over 127 - there's no such thing as "special ASCII". It looks like the character you want (the ’ you pasted) is U+2019. That's not part of ISO-8859-1. Commented Nov 7, 2017 at 12:04
  • The page you've now linked claims that "In ISO-8859-1, the characters from 128 to 159 are not defined." That's not quite true - it's true for ISO 8859-1, but not for ISO-8859-1. (See Wikipedia) The page you linked to isn't terribly clear, but I suspect the part you're looking at is using Windows-1252, but that your console doesn't support it. Commented Nov 7, 2017 at 12:07
  • Take it from a person with umlauted characters in his name: There is an extended ASCII for every language, dialect, OS and/or phase of the moon, so it's probably a good idea to use Unicode. Commented Nov 7, 2017 at 12:15
  • 1
    @HeikkiMäenpää: I'd try to avoid the term "extended ASCII" entirely, as it causes pretty much nothing but confusion. Commented Nov 7, 2017 at 13:11

1 Answer 1

1

Thanks for the responses as John Skeet points out in his reply, the Java program needed to recognize the input data Windows-1252 and the Windows command line wasn't set to the code page either.

Setting the command line codepage to Windows-1252 was done by running

chcp 1252

Starting the Java program to use Windows-1252 as the default was done by adding the following parameter

-Dfile.encoding="Windows-1252"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.