The problem with unsigned char.
I am reading a PPM image file which has data in ASCII/Extended ASCII.
For a character, eg. ‘†’ ,
In JAVA, after reading it as char and typecasting into int its value is 8224.
In C/C++, after reading it as a unsigned char and typecasting into int its value is 160.
How would i read in JAVA so as to get value 160 ?
The followng C++
unsigned char ch1 ='†';
char ch2 = '†';
cout << (int) ch1 << "\n"; // prints 160
cout << (int) ch2 << "\n"; // prints -96
In Java,
char ch1 = '^';
char ch2 = '†';
System.out.println (" value : " + (int) ch1); // prints 94
System.out.println (" value :" + (byte) ch1); // prints 94
System.out.println (" value : " + (int) ch2); // prints 8224
System.out.println (" value :" + (byte) ch2); // prints 32
Following are some exceptions
8224 †
8226 •
8800 ≠
8482 ™
8710 ∆
8211 –
8221 ”
8216 ‘
9674 ◊
8260 ⁄
8249 ‹
8249 ‹
8734 ∞
8747 ∫
8364 €
8730 √
8804 ≤
Following are some good ones
94 ^
102 f
112 p
119 w
126 ~
196 Ä
122 z
197 Å
197 Å
Any help is appreciated
In C++ you are using “narrow” characters in some specific encoding that happens to define character ‘†’ as 160. In other encodings 160 may mean something else, and character ‘†’ may be missing altogether.
In Java, you are always dealing with Unicode. 8660 = 0x2020 = U+2020 “DAGGER”.
To get “160”, you need to convert your string to the same encoding you are using with C++. See String.getBytes(charset).