Character's ASCII Equivalent

New to XMOS and XCore? Get started here.
JohnRobert
Active Member
Posts: 34
Joined: Fri Nov 02, 2012 8:10 am

Character's ASCII Equivalent

Post by JohnRobert »

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


yzoer
XCore Addict
Posts: 133
Joined: Tue Dec 15, 2009 10:23 pm

Post by yzoer »

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
JohnRobert
Active Member
Posts: 34
Joined: Fri Nov 02, 2012 8:10 am

Post by JohnRobert »

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 :(
User avatar
franksanderdo
Experienced Member
Posts: 69
Joined: Sat Apr 30, 2011 6:19 pm

Post by franksanderdo »

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
JohnRobert
Active Member
Posts: 34
Joined: Fri Nov 02, 2012 8:10 am

Post by JohnRobert »

Cheers! I'll check this out.

-John
User avatar
Bianco
XCore Expert
Posts: 754
Joined: Thu Dec 10, 2009 6:56 pm

Post by Bianco »

If you do

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;
}
So you can use the char data type for assignment and comparison using both (8-bit) numbers or real characters enclosed in single quotes;
yzoer
XCore Addict
Posts: 133
Joined: Tue Dec 15, 2009 10:23 pm

Post by yzoer »

Wow. Major brain fart on my end :) Bianco's right. Don't know what the heck I was thinking!

Yvo
JohnRobert
Active Member
Posts: 34
Joined: Fri Nov 02, 2012 8:10 am

Post by JohnRobert »

@Bianco:

So, theoredically, I could cast char "c" to an int, and it'd return 72?

-John
yzoer
XCore Addict
Posts: 133
Joined: Tue Dec 15, 2009 10:23 pm

Post by yzoer »

I know I'm not Bianco but yes, that's pretty much all there's to it :-)

-Yvo
JohnRobert
Active Member
Posts: 34
Joined: Fri Nov 02, 2012 8:10 am

Post by JohnRobert »

I found something interesting in the "Programming XC on XMOS Devices.pdf"... and I quote..
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.
(page 14 of Programming XC on the XMOS Devices.pdf)

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;
}