How do I use extended ASCII inside an array....

Started by
8 comments, last by Zahlman 17 years, 7 months ago
I am trying to figure out how to use extended ascii inside my 2-D array like so...
unsigned char maze[ROWMAX][COLMAX] =
{
{'B','B','B','B','B','B','B','B','B','B','B','B','B','B','B','B'},
{'B','M',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','B'},
{'B','B','B','B','B','B','B','B','B','B',' ','B','B','B',' ','B'},
{'B','W',' ',' ',' ',' ',' ',' ',' ','B',' ','B',' ','B',' ','B'},
{'B','B','B','B','B','B','B','B',' ','B','B','B',' ','B',' ','B'},
{'B',' ',' ',' ',' ',' ',' ','B',' ',' ',' ',' ',' ','B',' ','B'},
{'B','B','B','B','B','B','B','B',' ','B','B','B','B','B',' ','B'},
{'B',' ',' ',' ','B',' ',' ',' ',' ','B',' ',' ',' ',' ',' ','B'},
{'B',' ',' ',' ',' ',' ',' ',' ','B','B','B','B',' ','B','B','B'},
{'B',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','B'},
{'B','B','B','B','B','B','B','B','B','B','B','B','B','B','B','B'}
};

I would like to input the decmial representaion of the extended ASCII character.
Advertisement
You have 3 options:

* use an integer value (e.g. 255, 0xFF or 0377 for character #255)
* use an hexadecimal character literal (e.g. '\xFF' for character #255)
* use an octal character literal (e.g. '\0377' for character #255)


The actual character it maps to graphically depends on the code page you are using.

"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
I change my array to an intger and then typecasted it to a char when I went to display it. Does this sound resonable or is there a better way of doing this.

EDIT: this did not work for me...
Quote:Original post by Fruny
You have 3 options:

* use an integer value (e.g. 255, 0xFF or 0377 for character #255)
* use an hexadecimal character literal (e.g. '\xFF' for character #255)
* use an octal character literal (e.g. '\0377' for character #255)


The actual character it maps to graphically depends on the code page you are using.


Can you explain what you mean by code page?
unsigned char array[] = { 255, 0xFF, 0377, '\xFF', '\0337' };

No cast required.

Trying to directly use 'ÿ' in your code would be a Bad Idea™, since its value will depend on the code page the file is saved in: 0xFF if you are using Windows 1252, but 0x98 with IBM 850.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote:Original post by chadsxe
Can you explain what you mean by code page?


There is no such thing as "Extended ASCII". ASCI only defines code points 0 to 127. The interpretation of char values 128 to 255 depends on the code page you are using, 0x8C could be î (IBM 434, IBM 850) or Œ (Windows 1252) or even a control code (ISO 8859-1).
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote:Original post by Fruny
unsigned char array[] = { 255, 0xFF, 0377, '\xFF', '\0337' };

No cast required.

Trying to directly use 'ÿ' in your code would be a Bad Idea™, since its value will depend on the code page the file is saved in.


Duh...I was doing this '255'. Sorry and thanks for the info.
Quote:Original post by chadsxe
Quote:Original post by Fruny
unsigned char array[] = { 255, 0xFF, 0377, '\xFF', '\0337' };

No cast required.

Trying to directly use 'ÿ' in your code would be a Bad Idea™, since its value will depend on the code page the file is saved in.


Duh...I was doing this '255'. Sorry and thanks for the info.


Is it just me or should that not even compile? Single quotes (') are for chars... '255' is a char array and should have to be in double quotes ("). I'm confused as to how that didn't throw an error.
[TheUnbeliever]
Quote:Original post by TheUnbeliever
Is it just me or should that not even compile? Single quotes (') are for chars... '255' is a char array and should have to be in double quotes ("). I'm confused as to how that didn't throw an error.

MSVC would have compiled it but should have given him a warning about constant trunctation. Basically it evaluates to 0x00323535. I don't know if that syntax is a MSVC specific thing or not.
-Mike
They are legal, but the value is implementation-defined. (Most likely, it will be as if it were a base-256 value with the ASCII value used for each digit; but that's not guaranteed.)

Quote:
An ordinary character literal that contains a single c-char has type char, with value equal to the numerical value of the encoding of the c-char in the execution character set. An ordinary character literal that contains more than one c-char is a multicharacter literal. A multicharacter literal has type int and implementation-defined value.

This topic is closed to new replies.

Advertisement