CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Typecasting Problem

  1. #1
    Join Date
    Jun 2002
    Posts
    936

    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

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,116

    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

  3. #3
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    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

  4. #4
    Join Date
    Aug 2005
    Location
    The Matrix
    Posts
    159

    Re: Typecasting Problem

    Quote 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. */

  5. #5
    Join Date
    Jan 2006
    Location
    Belo Horizonte, Brazil
    Posts
    405

    Re: Typecasting Problem

    Hi.

    Quote 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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)