blutuu
blutuu

Reputation: 590

Char array is returning incorrect values

I have a char array, chars[] with values {'#', '$', '1'} contained within it. I want to remove the 1 and place it into another variable, val, but when I do it gives me a 49 (idk why). I tried debugging it and the info shows that the elements of chars are as follows:

char[0] = 35 '#'
char[1] = 36 '$'
char[2] = 49 '1'

Which in turn makes

int val = char[2];

become

val = 49

I'm not sure why this is, but it's throwing my plans off. Does anyone know what the problem is and what I can do to fix it?

Upvotes: 1

Views: 1983

Answers (3)

WareN The GaMeR
WareN The GaMeR

Reputation: 76

Just go for: charArray[x].ToString();

This will convert the ASCII representation to an actual Character.

Upvotes: 0

acsadam0404
acsadam0404

Reputation: 2831

You should use char val = char[2];

With int, you are getting the ASCII representation of the char as an integer. see also http://hu.wikipedia.org/wiki/ASCII

Upvotes: 2

Kevin DiTraglia
Kevin DiTraglia

Reputation: 26048

49 is the ASCII representation for the char '1'

link to ASCII table

Upvotes: 1

Related Questions