9 Replies Latest reply on Apr 3, 2015 2:09 PM by JiGi_284761

    Char to Int

       I am building a project on psoc 5LP. By using easy to use cc2500 module I am able to receive the string I wanted to psoc and display it in the lcd screen. But I want to analyse the number and make the decisions accordingly. I am receiving it as character. How do I convert it to int and do mathematical operations on it? c  syntaxes I use in seems to be not working with .For eg I want to access the MSB of the string I receive but string[0] is not letting me access it. It is showing error. How do I solve this problem? Where can I get a extensive list of basic functions and operations used in creator?

        • 1. Re: Char to Int

          There is a C function scanf() which could do the job, but it is rather sensitive to errors. A simple conversion could be programmed easily. When you post your actual project here, we all can have  a look at all of your settings? To do so, use
          Creator->File->Create Workspace Bundle (minimal)
          and attach the resulting file.

          A rough sketch of a conversion is


          Result = 0;


          Index = 0;


          while(String[Index] && (String[Index] is a Number))




              Result *= 10


              Result += String[Index++] - '0';







          • 2. Re: Char to Int

             The following is the c file. The variable 'message' is string.For eg; It will be of the form 1345. I have to extract the data node 1 , sensor 1 value 45 from it. Please help me how to read this. I mean I have to divide 1345 with 1000 and find quotient and  the remainder. Then understand it is node 1 from quotient and againg divide 345 by 100 to identify it is sensor 3. Then read the value 45 and display if it is high or low. And then continue with other modules. But I am not able to access the string 'message' divide it by 1000 and proceed. 




            #include <device.h>


            #define LCD_NUM_COLUMNS 16


            void main()




                char8 messsage;      


                uint8 count=0;


                uint8 pos = 0;












                 messsage = UART_1_GetChar();


                 if(messsage > 0)






                        if(count % LCD_NUM_COLUMNS == 0) 






                            pos = 0; 




















             NB: I just printed the string in LCD to detect if I am really receiving the data. I could not get it printed in hyperterminal. 

            • 3. Re: Char to Int

              You are talking about a "string" but I only can see that you are handling characters.


              Again: can you please post the complete project as I instructed? Without the help of the IDE it is some more work to tell you what to do.


              Another question: I suppose your sensor is not sending continously, is there a mimic to "request" data?





              • 4. Re: Char to Int

                 I have attatched the module i did. This displays the seet of characters i want in LCD screen. And the transmitter is continuously sending data. Every 3s (I can set this time) There will be new set of characters (like 1345 I explained earlier ) will be received.


                I am a little confused about terminology. I am receiving characters which has 4 numbers at a strech and it is this set I want to analyse.

                • 5. Re: Char to Int

                  You should be using Creator 3.1 SP1.




                  You should receive your number, digit by digit, as chars from the UART. If it


                  is a long, 4 chars, a float 4 chars, an int 2 chars, a byte one char. Stated another


                  way you sent the number, or the module, by converting it to a string of chars,


                  and then sending the string byte for byte via UART.




                  On receive side you place the chars into a string buffer, for example




                  char MyNumber[ 10 ];




                  So you place each UART char received into that buffer, and place one more char,


                  a NULL, '\0' into the location after the last char, that terminates the string. Then you


                  use C conversion functions to convert.










                  Regards, Dana.

                  • 6. Re: Char to Int

                    Make sure you set up the scanf nano library per following -








                    Regards, Dana.

                    • 7. Re: Char to Int

                      For the terminology:


                      Characters are usually printable (ASCII) values.


                      Bytes are values representing 0x00 to 0xff which include ASCIIs and non-printable characters.


                      Strings are a sequence of non-zero bytes followed by a single terminating zero (0x00 or '\0') byte.


                      A character constant: 'A'


                      A string constant "Hello World". Even "A" is a string. consisting of the characters 'A' and '\0'


                      So it is very important what you receive from the UART, a sequence of printable characters that you can send directly to LCD or a sequence of bytes which you can make visible with LCD_PrintInt8() as a two-digit hex value.





                      • 8. Re: Char to Int

                        To add to Bob's comments, when you use strings the array you hold them in


                        must allways be 1 element larger than the max size string, thats to hold the


                        NULL character, '\0', various string f()'s use this to know where the end of the


                        string is.




                        When you do formatting for sprintf, scanf, on strings and numerics this attachment


                        may help, a useful reference.




                        Regards,. Dana.

                        • 9. Re: Char to Int

                          Creator dosent have any lists of functions. It does however initialize and create functions for the components. User Code is up to you.


                          First of all forget everything you read in the Cypress Uart datasheets. Useless for real world situations.


                          Your CyDelay(2000) will corrupt everything. Whatever you sent will be long gone covered  by errant noise bytes by the time the delay is over.


                          First, you need a structure to store your data. You will have a lot of data and need somewhere to store it.


                          Something like this:


                          typedef sruct MyUartFrame{


                          uint8 Id;


                          uint8 ValidMsg;


                          uint8 StartDelimiter;


                          uint8 EndDelimiter;


                          uint8 FunctionCode:


                          uint8 DataInString[100];


                          uint8 DataOutString[100];


                          uint8 CRCHigh;


                          uint8 CRCLow;


                          uint16 CRC;


                          uint8 Data16_1;


                          uint8 Data16_2;


                          uint8 Data32_1;


                          uint8 Data32_2;


                          uint8 Data32_3;


                          uint8 Data32_4;


                          uint16 DataInt16;


                          float32 ConvertedFloatData[10];




                          Then you need to make functions that will fill the structure;


                          Remember to always disable the UartRx interrupt when you access the Uart.Then enable it after you fetch data.  Otherwise you will be sorry. It picks up fish farting in  antartica..... :D


                          You need a array to put your data from the uart. (uint8 DataInString[100];)There are many ways to do this but if you know your frame size it is eaisiest.


                          You need to know what protocol the data is in and adjust your structure to accept it. If you are writing your own program Id suggest using a existing protocol of some sort.


                          There is always a start delimiter of some sort to tell you when the message starts. There is not always a end delimiter. You will need a timer for this or a counter.


                          The entire uart frame will go byte to byte into the DataIn Array.


                          Then you process the array using CRC and if it is a valid message, you look for the start delimiter (Might be IP address or MAC address or Modbus address or just a specific character or group of characters.


                          From this point you will fill your structure with the data. Remember the uart data is always 8 bit. Anything larger than 8 bit will need to be reassembled and converted into its proper data type.


                          You need to determine where the data starts in your message and capture the correct bytes in the correct order then insert them into a data type of the correct type. (Perhaps 2 bytes into a 16 bit register  or 4 bytes into a 32 bit register) This will involve bit  shifting and "anding" or "oring" depending on how you do it. Then you will need to convert these results to the proper data type (perhaps float). This usually involves a union.




                          I would like to put sample code for you but this forum sucks.


                          Hopefully the new forum will take care of some of the problems.