I wouldn't use that. You can just explicitly cast using (int)c if you want to.JohnRobert wrote:I found something interesting in the "Programming XC on XMOS Devices.pdf"... and I quote..
(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; }
Character's ASCII Equivalent
-
- XCore Expert
- Posts: 754
- Joined: Thu Dec 10, 2009 6:56 pm
-
- XCore Addict
- Posts: 133
- Joined: Tue Dec 15, 2009 10:23 pm
I think you're making this more complex than it really is :-) Using a cast is the preferred / most common way.
-Yvo
-Yvo
-
- XCore Expert
- Posts: 844
- Joined: Sun Jul 11, 2010 1:31 am
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.
a return value to the return type of the function, ever.
-
- Active Member
- Posts: 34
- Joined: Fri Nov 02, 2012 8:10 am
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
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
-
- XCore Addict
- Posts: 161
- Joined: Sun Jun 12, 2011 11:47 am
Haven't tried but would be surprised if that's possible.JohnRobert wrote: ..., could you cast a char array to an int array?
-John
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....
-
- XCore Addict
- Posts: 161
- Joined: Sun Jun 12, 2011 11:47 am
That should be 32 bit integers of course.... :$pstnotpd wrote:JohnRobert wrote: .consecutive 32 byte integers 1 by 1.