G'day,
I'm curious as to how one might get the ASCII equivalent of a character. For example, I wanted the ASCII equivalent of the character "H", which is 72 - but how can I get my code to find out that "H" equals 72?
Thanks,
John
Character's ASCII Equivalent
-
- Active Member
- Posts: 34
- Joined: Fri Nov 02, 2012 8:10 am
-
- XCore Addict
- Posts: 133
- Joined: Tue Dec 15, 2009 10:23 pm
Easiest way is to create a table holding the ASCII values and index into that using your character. It'll cost you 256 bytes though...
-Yvo
-Yvo
-
- Active Member
- Posts: 34
- Joined: Fri Nov 02, 2012 8:10 am
I know it's a bit of a stupid question... But how would one make tables in C? I know in Python, Java, etc... But not C :(
-
- Experienced Member
- Posts: 69
- Joined: Sat Apr 30, 2011 6:19 pm
Hi John,
Even though I can't test it right now (I am not at home) I would do:
char_value = (int) your_character;
The Compiler will do the rest for you. If I remember the old times back, there is a lookup table within the compiler doing this. If so, the function internally might look like this:
int lookuptable[256] = { 0x01, 0x02, 0x03 /* etc */ };
//defines your table with a bit of typing work.
int char_value = lookuptable[your_character];
//Gives you the value.
See you around
Frank
Even though I can't test it right now (I am not at home) I would do:
char_value = (int) your_character;
The Compiler will do the rest for you. If I remember the old times back, there is a lookup table within the compiler doing this. If so, the function internally might look like this:
int lookuptable[256] = { 0x01, 0x02, 0x03 /* etc */ };
//defines your table with a bit of typing work.
int char_value = lookuptable[your_character];
//Gives you the value.
See you around
Frank
-
- Active Member
- Posts: 34
- Joined: Fri Nov 02, 2012 8:10 am
Cheers! I'll check this out.
-John
-John
-
- XCore Expert
- Posts: 754
- Joined: Thu Dec 10, 2009 6:56 pm
If you do
char c = 'H';
it will hold the value of 72.
try this:
So you can use the char data type for assignment and comparison using both (8-bit) numbers or real characters enclosed in single quotes;
char c = 'H';
it will hold the value of 72.
try this:
Code: Select all
#include <stdio.h>
int main(void) {
char c = 'H';
printf("my char value (1): %d (%c)\n", c, c);
c = 72;
printf("my char value (2): %d (%c)\n", c, c);
if (c == 'H') printf("c == 'H'\n");
return 0;
}
-
- XCore Addict
- Posts: 133
- Joined: Tue Dec 15, 2009 10:23 pm
Wow. Major brain fart on my end :) Bianco's right. Don't know what the heck I was thinking!
Yvo
Yvo
-
- Active Member
- Posts: 34
- Joined: Fri Nov 02, 2012 8:10 am
@Bianco:
So, theoredically, I could cast char "c" to an int, and it'd return 72?
-John
So, theoredically, I could cast char "c" to an int, and it'd return 72?
-John
-
- XCore Addict
- Posts: 133
- Joined: Tue Dec 15, 2009 10:23 pm
I know I'm not Bianco but yes, that's pretty much all there's to it :-)
-Yvo
-Yvo
-
- Active Member
- Posts: 34
- Joined: Fri Nov 02, 2012 8:10 am
I found something interesting in the "Programming XC on XMOS Devices.pdf"... and I quote..
So, if this is the case, perhaps an effective, and quick way to convert it would be..
(page 14 of Programming XC on the XMOS Devices.pdf)If an operator has operands of different types, the operands are converted to a common type. In general, the "lower" type is promoted to the "higher" type before the operation proceeds; the result is of the higher type. For example, in the expression
'c' + 1
The binary operator + takes a char and an int operand. The char operand is promoted to an int, and the result of the expression is an int.
So, if this is the case, perhaps an effective, and quick way to convert it would be..
Code: Select all
int charToASCII(char c){
return c+0;
}