Maker Pro
Maker Pro

Assembling splitted integer C

Dada Krauter

Feb 22, 2015
28
Joined
Feb 22, 2015
Messages
28
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
 

Supercap2F

Mar 22, 2014
550
Joined
Mar 22, 2014
Messages
550
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
 

Dada Krauter

Feb 22, 2015
28
Joined
Feb 22, 2015
Messages
28
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
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,700
Joined
Nov 17, 2011
Messages
13,700
@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
 

Dada Krauter

Feb 22, 2015
28
Joined
Feb 22, 2015
Messages
28
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
 

KrisBlueNZ

Sadly passed away in 2015
Nov 28, 2011
8,393
Joined
Nov 28, 2011
Messages
8,393
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.
 

Anoxynym

Mar 1, 2015
9
Joined
Mar 1, 2015
Messages
9
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:

gorgon

Jun 6, 2011
603
Joined
Jun 6, 2011
Messages
603
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.
 
Top