Can I put ASCII Values in an char array? If yes then how

Basically, I am trying to make a box. I made it up using the following way,
1
2
3
cout << char(218) << char(196) << char(196) << char(196) << char(191) << endl;
cout << char(179) << char(32) << char(32) << char(32) << char(179) << endl;
cout << char(192) << char(196) << char(196) << char(196) << char(217) << endl;


This works fine. However, I want it to be in shape of a 2D char array.. But when I put these (char(218),char(196) etc) in the char array indexes, and then outputs the char array, it gives me a completely different thing...
(Why I want it to be stored in char array?
Well, because I am developing kind of a game in which I am supposed to move tokens into the box. These tokens will move into these boxes and by my logic, If I use a char array then I can very easily replace the central char(32) (which is a space) with the token when I want to. Hence, it would be far more easier to make this program if I use char array instead of simple couts...)

Also, can anyone tell how can I shorten the size of the box? I need 3 spaces in between the box as I want the token to be in the center of box.. The perfect size would be with 2 spaces but then my token would never be central and in case of just 1 space, space between the boxes become too small. Thank you!
@redfury

You don't need to create an array of char, just an array of ints, but output the value as a char. Like so..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>

using std::cout;
using std::endl;

int main()
{
	int box[3][3]={
		{218,196,191},
		{179, 32,179},
		{192,196,217}
	};
	int x,y;
	cout << char(box[0][0]);
	for(x=0;x<3;x++)
		cout << char(box[0][1]);
	cout << char(box[0][2]);
	cout << endl;

	for(y=0;y<3;y++)
{
cout << char(box[1][0]);
	
	for(x=0;x<3;x++)
		cout << char(box[1][1]);
	cout << char(box[1][2]);
	
	cout << endl;
}

	cout << char(box[2][0]);
	for(x=0;x<3;x++)
		cout << char(box[2][1]);
	cout << char(box[2][2]);

	cout << endl << endl;
}
Last edited on
You can write the characters as hex values in a string.

1
2
3
4
5
6
7
8
9
10
#include <iostream>

int main() {
    char box[] = "\xda\xc4\xc4\xc4\xbf\n"
                 "\xb3   \xb3\n"
                 "\xc0\xc4\xc4\xc4\xd9\n";
    std::cout << box;
    box[8] = 'x';
    std::cout << box;
}


BTW, you should delete your double post.
Last edited on
there is nothing magic about char.
char c = 32; //c is now a space. don't need hex, casting, or any other weirdness. if you know ascii in hex, use hex, if you know it in decimal, use decimal.
if you put a zero as the last character, it will write as a c-string with cout:

cout << char(218) << char(196) << char(196) << char(196) << char(191) << endl;
becomes
char line1[] = {218,196,196,196,191,'\n',0}; //I even put the end of line in there.
cout << line1;

which is pretty much the same as above, except you already had it in decimal so I thought you might want this.
Last edited on
Topic archived. No new replies allowed.