| 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
|
|
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
|
|
|
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
|
|
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
|
|
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.
|
 |
 |
|
|
subject: Convert Signed Short to Integer
|
|
|