PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

signed char problems

Having big headaches with the signed char problem.
I need to read in various text file, which are in several different languages. These text strings thus include extended ascii characters - those above 127.

As EVC++ only sees signed chars ( even with the _CHAR_UNSIGNED compiler pragma set) it gives you incorrect output values i.e

'ä'=0xe4=byte(228)=signed char(-28)
'ü'=0xfc=byte(252)=signed char(-4)
'ö'=0xf6=byte(246)=signed char(-10)

I need to obtain the correct ascii values for these letters :evil:

'ä' should give the decimal value 132.

any help would be much appreciated ( even a dirty fix would do).

Jim.
I don't exactly understand what problem you're having. A char is signed. If you want it unsigned, you have to cast it to an unsigned char. Perhaps if you expand on the problem you're having we could help more.

Code:

unsigned char c='ö';
//c==0xf6 && c==246;

char d=c;
//d==0xf6 && d==-10;

unsigned char e=d;
//e==0xf6 && e==246


Dan East
Try something like this:
Code:

#define tobyte(x) (((x)<0)?((x)+256):(x))


As you probably know, 128 as a signed char comes out to -128. This code just checks for that and adds 256 to bring it back into range. If it doesn't work, you might have to cast "x" to an "int" first.

You could also just define the values as "unsigned char"s instead of just "char", I think some other people have reported that this works.

Update:
Sorry about the -128 thing, I realized after the fact that I was wrong, but I didn't get a chance to fix it. (Thanks for pointing that out, refractor, just take that as a lesson to never believe anything I say unless someone else confirms it :) )
Dan beat me to it when I was typing
'ä' should give the decimal value 132.

char a = 'ä';
// has a decimal value of -28

unsigned char b = (unsigned char) a;
//has a decimal value of 228

how do I get the correct ansii 132 decimal value?

Jim.

ps. Am I doing somefink real dumb?
That makes no sense. You compiled the exact code sample you posted? How are you obtaining b's value, via the debugger?

Dan East
'ä' should give the decimal value 132.


Errr... no it shouldn't (why do you think it should?).

On my character map, 'ä' is character (decimal) 228 (like it gives you). So, unsigned = 228.

Thus, signed = 1 1100100

So, we sum:
-128
+4
+32
+64
= -28

(or -128 + 100 = -28, same result)

Where's the problem? (or rather, where did 132 come from?)

BillCow:
As you probably know, 128 as a signed char comes out to -127.


No it doesn't. The top bit set flags the character as being -128, not -127. The rest of the bits then add to it.

Thus (binary) 1111 1111 =
-128 + (binary)111 1111
=-128 + 127
=-1

The range of a signed value is always one larger negatively (because the positives must include 0).

Cheers,

Refractor.
According to the ASCII chart in the MSDN library 'ä' is 132 (0x84). I looked on 4 different ASCII charts and 'ä' is 132 on all of them. What chart are you using, because it seems to match the Pocket PC devices. I think that's Jim's problem.

Dan East
Ah. All becomes clear.

You don't want ASCII - look at the ANSI character set instead.

"ANSI Character Codes Chart" in MSDN.

Cheers,

Refractor.
The mans right :)
Yep I was doing somefink dumb!
Why ANSI?

Know of a function to convert from ansi to ascii

Many thanks for everyones help :)

Jim.
http://www.microsoft.com/typography/unicode/cs.htm

This page probably answers the "why ANSI" part... but I don't know the conversion routine (maybe you'll have to go through ANSI->UNICODE->ASCII?) ... or just stick to Unicode in the first place if you can.

Cheers,

Refractor.