-
May 31st, 2007, 03:46 PM
#1
Typecasting Problem
I am writing some data to a serial port. It looks like I have some problems when writing the character. For instance,
If I let x[] = {'a','b','c'}, no problem, x is declared as char
now, if I do something like that x[]={1,2,3}; I receive a lot bad characters
I try to send some character by doing something like that inside a loop
x[i] = (char)i; //but still not working, receive jaggy or control character
I wonder if there is a possibility to typecast
(char)i; in order to get something like that 'i'
in that case if i = 0, 1, 2, 3; x[i] will be {'0','1','2','3'} this is what the typecast above should be, but it does not work
-
May 31st, 2007, 03:50 PM
#2
Re: Typecasting Problem
You cant typecast. A number is a number, and an ASCII character is a representation of a character encoded as a number. See here
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
May 31st, 2007, 03:53 PM
#3
Re: Typecasting Problem
No, that is not possible.
A "normal" ASCII character like '0' is actually coded as the decimal number 48. So, if you want to send your data as characters you have to initialize your string with characters.
Edit: Good link TheCPUWizard
-
May 31st, 2007, 07:58 PM
#4
Re: Typecasting Problem
Originally Posted by vcstarter
I am writing some data to a serial port. It looks like I have some problems when writing the character. For instance,
If I let x[] = {'a','b','c'}, no problem, x is declared as char
now, if I do something like that x[]={1,2,3}; I receive a lot bad characters
I try to send some character by doing something like that inside a loop
x[i] = (char)i; //but still not working, receive jaggy or control character
I wonder if there is a possibility to typecast
(char)i; in order to get something like that 'i'
in that case if i = 0, 1, 2, 3; x[i] will be {'0','1','2','3'} this is what the typecast above should be, but it does not work
Well if you want to convert integer 1 to its equivalent character '1' you can just add '0' to the integer value.
Code:
for(i=1;i<=3;i++) {
x[i] = i + '0';
}
/** The only stupid question is the one you never ask. */
-
June 1st, 2007, 06:29 AM
#5
Re: Typecasting Problem
Hi.
Originally Posted by Sarevok
Well if you want to convert integer 1 to its equivalent character '1' you can just add '0' to the integer value.
Code:
for(i=1;i<=3;i++) {
x[i] = i + '0';
}
Yes. And a good practice when working with characters and integrals would be to use unsigned types.
_______________________
Is this post useful? If yes, I would appreciate if you rate it!
Leandro T. C. Melo
http://www.pazbrasil.org/
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|