• Post Reply Bookmark Topic Watch Topic
  • New Topic

Convert Signed Short to Integer  RSS feed

 
Nicole Monit
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch (only two years after logging on) and well done working it out.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about (s & 0x7fff) + 0x8000 instead?
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
+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).
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!