2

I have some Java code that should be very simple and straightforward, and unfortunately, I'm getting very odd results.

Put simply, I'm doing a basic conversion from ASCII integers back into their string values.

For instance, if I do:

char n = (char) 152;

System.out.print(n); // should yield: ÿ.

Unfortunately, this is simply yielding: ?

Anybody have any familiarity with this type of problem?

2
  • There should be a semi-colon after print statement. Commented Feb 4, 2015 at 21:26
  • Yes thank you, this is my first question that I've actually posted since I've been able to find pretty much every problem I've had on here, so I guess I'm a little sloppy... Commented Feb 4, 2015 at 21:35

3 Answers 3

4

In UTF16, the character ÿ is decimal code 255. Decimal code 152 is a control character called "start of string". http://www.fileformat.info/info/unicode/char/0098/index.htm

You may be looking at, for example, asciitable.com extended codes, but these codes do not apply to Java which is UTF16.

Sign up to request clarification or add additional context in comments.

4 Comments

Ok. I will need to get java to use a different encoding type? I assume I need UTF-8?
In UTF-8 the code point would still be 255. Good catch by Radiodef! If you look at the code chart for the Latin-1 Supplement (PDF) you will see the codes for all these characters. 'ÿ' is the last one in that block.
Java char is always considered a UTF16 code. The library provides means to convert from byte arrays in other encodings but I can't say for sure if that's what you need to do. Perhaps the thing to do is to ask a new question pertaining to the program you are writing.
Ok. Thanks for the comment Radiodef and David Conrad. I will mark the answer complete iff I can figure out a solution to my problem, and will link it to any new question that I make.
1

The console you are printing out to may not support displaying that character.

Have you tried

System.out.println("ÿ");

To see if your console can display this character?

3 Comments

It's also possible the console does support it, but there's a mismatch between the encoding Java is emitting and what the console is expecting.
I'm using Microsoft command prompt. Interestingly enough, it's not that it's simply unable to print the string. I'm doing a comparison between an original string with ascii values and converting numbers from their integer ascii values to then see if the two strings are equal. In any case, the ÿ is yielded as part of the original string and when i try to explictly print it i get some weird upside down 4, but when i print the cast i get a ?. Thanks for the response tho
@DavidConrad, This is part of a larger problem i was having but in an effort to figure out what was going on I noticed that if i explicitly try and cast "(char) 152" it yields me with a ?, so I'm not sure how encoding would have anything to do with it as I'm hardcoding 152.
1

When you print the char it is converted to your default platform encoding, which might be Windows-1252 or who-knows-what. You probably need to tell Java what encoding your terminal is using, but you need to know what that is.

For instance, on my system, I would need to run your class with a -Dfile.encoding=UTF-8 switch, since my terminal (mintty under cygwin) is expecting UTF-8, but Java is defaulting to Windows-1252 (or, Cp1252) on Windows 7.

Additionally, as Radiodef pointed out, 152 is a Unicode control character; the correct code point for ÿ is 255.

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.