Connect with us

Assembling splitted integer C

Discussion in 'Microcontrollers, Programming and IoT' started by Dada Krauter, Feb 26, 2015.

Scroll to continue with content
  1. Dada Krauter

    Dada Krauter

    28
    0
    Feb 22, 2015
    Hi everyone ;)

    I have this code to take desired segment of data from long string :)
    The command prints separately each char and I want to save it as one integer.

    So: Transmitter sends data like that %S,123,A,456,B,789,C. Code reads only numbers and prints on LCD. Let's take 123: It prints 1 then 2 and then 3 on LCD but I want to save it as one integer 1 2 3 = 123. How can I do that?

    Fragment of this code(Printing on LCD)
    /////
    t=0;
    while(data[t]!=',')
    {
    print(data[t]);
    t++;
    }
    /////

    Thanks a lot ;)

    DADA
     
  2. Supercap2F

    Supercap2F

    550
    151
    Mar 22, 2014
    If it's a parallel display with a HD44780 controller, you can only put one character on the screen at a time because of the way data is sent. If you want to print one number, then add it to the hexadecimal code 0x30, and send the result (because the data is sent via ASCII). If you want to print on the display a number like 100, then your going to have to use the modulo operator to find the numbers out of each decimal place, and then print them one by one.
    Dan
     
  3. Dada Krauter

    Dada Krauter

    28
    0
    Feb 22, 2015
    Hi Supercap2F :)
    Thank you for your fast reply,

    on LCD data prints separately e.g. 1 then 2 then 3 (123) but I want to use printed numbers for another function inside a MCU. I want it to be 123 not 1 2 3, Like that: int data = 123; Can you tell me how to save each digit in to one integer with C code?

    Thanks again ;)

    DADA
     
  4. Harald Kapp

    Harald Kapp Moderator Moderator

    12,465
    2,987
    Nov 17, 2011
    @Supercap: I guess that's not what Dada wants. He receives an integer in the form of an ASCII string.separated by commmas and wants to extract the Integer from the ASCII string. So "123" becomes 123dec.

    One way to do it is like this:
    Code:
    t=0;
    int result =0;
    while(data[t]!=',')
    {
    result=10*result+(int) (data[t]-'0') //without error check for permitted characters, assuming ACII code
    t++;
    } 
    This works as follows:
    • data[t]-'0' computes the integer value fo the ASCII character in the buffer. BY subtracting the ASCII-value of '0' an ASCII '0' becomes the number 0.
    • The typecast (int) (data[t]-'0') converts the result into an integer between 0...9, depending on the ASSCII character in data[t].
    • This is added to 10*result. Why 10*? This is to shift any partial number that is already stored in the variable result one decimal place to the left.

    For ASCII "123" result will develop going through the loop as follows:
    result=0
    result=10*0+1=1
    result=10*1+2=12
    result=10*12+3=123​

    Cheers
    Harald
     
    Arouse1973 and Supercap2F like this.
  5. Dada Krauter

    Dada Krauter

    28
    0
    Feb 22, 2015
    Thank you Harald Kapp you helped me :D

    It worked. It's that what I wanted ;)

    I have one more question is there any library for UART data correction(If received data is incorrect)?? or if not how can I write that in C code? :)

    Thanks again :)

    DADA
     
  6. KrisBlueNZ

    KrisBlueNZ Sadly passed away in 2015

    8,393
    1,272
    Nov 28, 2011
    If you're receiving plain text, you can't tell whether it was "received correctly" or not. The UART can detect some kinds of errors - framing errors, and parity errors if parity is used, and it can report overrun errors, and you may be able to test for invalid received characters or incorrect string structure, but if data gets corrupted and this doesn't trigger any of those errors, there is no way for the receiver to know.
     
  7. Dada Krauter

    Dada Krauter

    28
    0
    Feb 22, 2015
    Hi @KrisBlueNZ :)

    OK I understand that, can you help me to write those functions with C code?

    Thanks for all ;)

    DADA
     
  8. Anoxynym

    Anoxynym

    9
    7
    Mar 1, 2015
    Look at the string functions in the standard I/O library. These are documented on the internet. I found them using search string "C library functions"

    #include <stdio>
    ...
    int sscanf(constchar*str,constchar*format,...)

    converts string str to values (including numbers) as specified in the string format and puts them into variables whose names are passed as pointers in the parameters following the format string.
     
    Last edited: Mar 2, 2015
  9. gorgon

    gorgon

    603
    24
    Jun 6, 2011
    If you need to transfer data securely, I would use a CRC on the data, so you know when they are wrong, and can ask for a retransmission.
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-