File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Convert Signed Short to Integer Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Convert Signed Short to Integer" Watch "Convert Signed Short to Integer" New topic
Author

Convert Signed Short to Integer

Nicole Monit
Greenhorn

Joined: Oct 06, 2009
Posts: 2
Hello,

I'm trying to write a RTF Parser and having a problem with Unicode Characters. The problem is that all Chinese characters represented with a "\u" following by a character code

\uc2\u23562\'d7\'f0\u25964\'be\'b4\u30340\'b5\'c4\u-28334\'be\'c6\u24215\'b5\'ea\u21512\'ba\'cf

Referring to RTF Specification, the code is a short value, which mean all values above 32767 will be written as Negative.

So my question is, how can I convert the -28334 to an Integer and find its real unicode character code value in codepage table? Thanks.



EDIT: Got it


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32651
    
    4
Welcome to the Ranch (only two years after logging on) and well done working it out.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32651
    
    4
How about (s & 0x7fff) + 0x8000 instead?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

+1. 0x8000 may be a negative value if that were a short, but all integer literals are ints (unless they have an extra L or l behind it, in which case they are longs. But don't ever use l, because it looks too much like a 1 in several fonts).


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16686
    
  19

Campbell Ritchie wrote:How about (s & 0x7fff) + 0x8000 instead?


Or how about just "s & 0xffff"? ... which will work for both positive and negative shorts. This way, there is no need to check for negative before applying the expression.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32651
    
    4
Yes, while I was out, I thought we were mistaken and (int) s & 0xffff would be a far better way to do it. And Henry beat me to it. Because I was engaged in a far more important activity ... beer

The cast may be unnecessary.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Convert Signed Short to Integer
 
Similar Threads
doubt
need answers
Char into Integer
Language doubt
chars