Character's ASCII Equivalent

New to XMOS and XCore? Get started here.
User avatar
Bianco
XCore Expert
Posts: 754
Joined: Thu Dec 10, 2009 6:56 pm

Re: Character's ASCII Equivalent

Postby Bianco » Wed Jan 30, 2013 11:07 am

JohnRobert wrote: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;
}
I wouldn't use that. You can just explicitly cast using (int)c if you want to.
yzoer
XCore Addict
Posts: 133
Joined: Tue Dec 15, 2009 10:23 pm

Postby yzoer » Wed Jan 30, 2013 3:43 pm

I think you're making this more complex than it really is :-) Using a cast is the preferred / most common way.

-Yvo
User avatar
segher
XCore Expert
Posts: 843
Joined: Sun Jul 11, 2010 1:31 am

Postby segher » Wed Jan 30, 2013 9:17 pm

Actually, 'c' already *has* type int. Also you do not need to explicitly cast
a return value to the return type of the function, ever.
JohnRobert
Active Member
Posts: 34
Joined: Fri Nov 02, 2012 8:10 am

Postby JohnRobert » Wed Jan 30, 2013 11:33 pm

G'day,

Well, okay, thanks for all the help, I'll use casting!

One last question though, could you cast a char array to an int array?

-John
User avatar
pstnotpd
XCore Addict
Posts: 161
Joined: Sun Jun 12, 2011 11:47 am

Postby pstnotpd » Thu Jan 31, 2013 7:23 am

JohnRobert wrote: ..., could you cast a char array to an int array?
-John
Haven't tried but would be surprised if that's possible.

The implicit conversion would be quite sophisticated then as a char array is usually made up of consecutive memory locations, 1 byte in case of ASCII, which has to be converted to consecutive 32 byte integers 1 by 1.

But then again I'm not that familiar with the XC implementation....
User avatar
pstnotpd
XCore Addict
Posts: 161
Joined: Sun Jun 12, 2011 11:47 am

Postby pstnotpd » Fri Feb 01, 2013 7:17 am

pstnotpd wrote:
JohnRobert wrote: .consecutive 32 byte integers 1 by 1.
That should be 32 bit integers of course.... :$

Who is online

Users browsing this forum: No registered users and 2 guests