• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to Return Extended ASCII Decimal Code

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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()
 
Ranch Hand
Posts: 174
Java ME Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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):

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic