|
-
Jan 18th, 2003, 08:25 PM
#1
Thread Starter
Fanatic Member
char test = ???
I'm writing a program where I need to modify letters by using numbers. Like test = 79 to make test = 'a'.
How do I make test equal decimal letters 160 to 255? Like an up-side-down question mark for example. Doing
Code:
cout << (int)'¿' << endl;
gives me -65, then if I try to make a char = -65 it will not display a ¿...
Dec# for ¿ is 191
Never argue with fools, they will only drag you down to their level, and beat you with experience.
Q: How do you tell an experienced hacker from a novice?
A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer
-
Jan 18th, 2003, 08:50 PM
#2
Frenzied Member
A char can hold values -128..127. Make it unsigned.
Z.
-
Jan 18th, 2003, 09:06 PM
#3
Thread Starter
Fanatic Member
it still doesn't work...
cout << (int)'¿';
will display -65
unsigned char test = -65;
cout << test; will display some weird character...
Never argue with fools, they will only drag you down to their level, and beat you with experience.
Q: How do you tell an experienced hacker from a novice?
A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer
-
Jan 18th, 2003, 09:09 PM
#4
Frenzied Member
Originally posted by McCain
it still doesn't work...
cout << (int)'¿';
will display -65
unsigned char test = -65;
cout << test; will display some weird character...
cout << (int)(unsigned char)'¿';
Z.
-
Jan 18th, 2003, 09:20 PM
#5
Thread Starter
Fanatic Member
cout << (int)(unsigned char)'ä' << endl; --> 228
unsigned char test = 'ä';
cout << (int)'ä' << endl; --> -28
Someone please explain!
Thank you for your help so far Zaei
Never argue with fools, they will only drag you down to their level, and beat you with experience.
Q: How do you tell an experienced hacker from a novice?
A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer
-
Jan 20th, 2003, 04:38 AM
#6
That's because of the way negative numbers work. Search for "2's complement" and you'll find an explanation.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 20th, 2003, 04:29 PM
#7
Thread Starter
Fanatic Member
unsigned means it doesn't accept negative numbers, right?
Didn't find much on 2's complement but what I gathered is that the compiler will substitute the - for a 2, is this correct?
Please explain this:
cout << (int)'ä' << endl; --> -28
cout << (char)132 << endl; --> ä
Never argue with fools, they will only drag you down to their level, and beat you with experience.
Q: How do you tell an experienced hacker from a novice?
A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer
-
Jan 21st, 2003, 09:44 AM
#8
Ok, you get an explanation of 2's complement from me.
Every PC integer is made up of a series of bits (duh!). The computer doesn't know the - sign (bits can only represent 0 or 1), so it has to interpret some patterns as negative numbers (if you want it to, if you use unsigned variables then you don't want it to and all numbers are interpreted positive).
So, a regular 8-bit integer would look like:
00010011 = 19
or
00100100 = 36
Addition works:
00110111 = 55
Substraction works too, as does multiplication. After all, this is just a base-2 number system.
You now have several ways to mark a number as negative. The most primitive (and the one used for floating point numbers) is simply to declare one bit to be the sign bit. If it is 0 the number is positive, if it is 1 the number is negative. So
10010011 = -19
But this has two problems. The minor one: it's a waste
00000000 = 0
10000000 = 0
so you have a range from -127 to 127 (255 numbers). You could represent 2^8 = 256 numbers with 8 bits, so one number is wasted.
The second and larger problem is that of adding 1 to -1:
10000001 = -1 +
00000001 = 1 =
10000010 = -2
So you'd have to check every addition explicitly if it breaks the 0-barrier.
2's complement is a different idea. In 2's complement there is still the highest bit set in every negative number (so testing is easy), but the representation in the other bits works differently. You convert a positive number to a negative one by inverting all bits and adding 1:
00000001 = 1
invert:
11111110
and add 1:
11111111 = -1
ok, so we got a definition. Where's the advantage? Consider the previous example of -1 + 1:
11111111 = -1 +
00000001 = 1 =
100000000, but the highest bit is overflow and is ignored, so the result is
00000000, which is correct.
A little testing shows that all mathematics work correctly with this method. This is why it is used.
But a computer memory area doesn't know if it's an unsigned or a signed integer (or no integer at all). It depends on interpretation. That's the reason why the bit pattern
11100111
might be interpreted as -27 or as 231.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 21st, 2003, 11:32 AM
#9
Thread Starter
Fanatic Member
Thank you so much!
Now I finaly understand how this works, all thanks to you CornedBee
Never argue with fools, they will only drag you down to their level, and beat you with experience.
Q: How do you tell an experienced hacker from a novice?
A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|