how can i compare characters from the Extended ASCII Codes that are stored in a character array?

71 Views Asked by At

I'm using the ASCII code 219 to print out "█" in a character array. however I would like to check the value of that cell later on in the program using a if statment. this is what i currently have to try to check the value: if(gameboard[playery][playerx] == 219){}. however is vs code it will give a message stating that the comparison is allways faulse due to range limitations for the data type. is there a diffrent way of doing this?

i've tried single and double quotes aswell as using the box as plan text in the program but none of that works. i also tried using the octal value from this suggestion in a diffrent post however the message still appered.

2

There are 2 best solutions below

1
Thomas Hedden On

I'm not sure about whether the char is signed or not, but I think that @teapost418 is correct that you just have to type cast 219 as a char:

if(gameboard[playery][playerx] == (char) 219){}

I think that otherwise 219 would be interpreted as an int.

0
chux - Reinstate Monica On

First, there is no ASCII code 219 as ASCII is 7-bit.

The are various extended ASCII codings with a value of 219.


<string.h> compare routines like strcmp() examine the several characters as if they were unsigned char even when char is signed.

For all functions in this subclause, each character shall be interpreted as if it had the type unsigned char (and therefore every possible object representation is valid and has a different value). C23dr § 7.26.1 4

fgetc() reads a character and returns, as an int, an unsigned char value or EOF.

I recommend to compare individual char in the same fashion to provide consistent functionality when strings are used.

// if(gameboard[playery][playerx] == 219){}
if((unsigned char)gameboard[playery][playerx] == 219){}

... or perhaps make gameboard[][] an array of unsigned char. Yet need to see overall code to fully endorse this approach.