Results 1 to 9 of 9

Thread: char test = ???

  1. #1

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802

    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

  2. #2
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    A char can hold values -128..127. Make it unsigned.

    Z.

  3. #3

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    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

  4. #4
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    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.

  5. #5

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    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

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  7. #7

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    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

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  9. #9

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    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
  •  



Click Here to Expand Forum to Full Width