• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to Return Extended ASCII Decimal Code  RSS feed

 
Lars Nett
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying understand how I can return the extended ASCII decimal value for characters in the 128 to 159 range using the default Windows codepage instead of a negative value.

Example

String character = new String(new byte[] { (byte) 150 }, "Cp1252");
System.out.println("character: " + character);
System.out.println(Arrays.toString(character.getBytes("Cp1252")));

Results

character: –
[-106]

Instead of returning 150 in this exampe it returns -106. I am just beginning to understand Java and signed numbers. How can I code a module to return decimal 150 instead of -106?

I am examining an input file and would like to replace extended ascii characters with a printable character based on the value returned in getBytes()
 
Zandis Murāns
Ranch Hand
Posts: 174
Java Java ME Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See, the problem is that byte data type is only 8 bit long. So as byte data type is signed, it can hold values between -128 and +127. If you cast 150 to byte, actual value you get from this is equal to -106.
So you should construct your string using other data type (for instance - char, that is 16bit variable):

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!