Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

single dimensional array data in a single variable in c

Status
Not open for further replies.

hirenn

Full Member level 1
Full Member level 1
Joined
Jul 12, 2014
Messages
96
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
703
Hello Everyone !!!!

I'm writing code to get data from sensor having ascii Value as input , i have get it from UART to buffer (buffer is single dimensional array having 4)..

im using C.

now i want to get this array data into a single variable.
how could i do this .. please help me :)
 

It depends on the format of the four bytes. For example if the ASCII bytes are '1234' and they represent that numeric value in decimal, you could use:
Code:
int Total;
Total = (data[3] - '0') * 1000;
Total += (data[2] - '0') * 100;
Total += (data[1] - '0') * 10;
Total += (data[0] - '0');

Subtracting '0' (character zero) converts the ascii value to it's decimal value, for example '7' = D'37' so subtracting '0' makes it 7.
If the values are in a different radix, choose appropriate multipliers instead of decimal.

Brian.
 
  • Like
Reactions: hirenn

    hirenn

    Points: 2
    Helpful Answer Positive Rating
hello !!
Thanks for you kind reply , but there is problem

what data is i.m getting is varied from 0 to 1200 in decimal

how could i get with this in single variable from array
 

Hi,

Where EXACTELY do you see a problem?

From what I see now Brian´s solution is correct.

****

"decimal"...there is no dedicated "decimal" data format for computers, it´s more a human format.
* ASCII--> Brian
* binary? signed, unsigned?
* BCD?
* float?

***
1234 in decimal can be represented as 4 bytes:
* as ASCII string 0x 31 32 33 34 = "1234"
* as binary (signed and unsigned) = 0b 00000000 000000000 00000100 11010010
* as HEX (signed and unsigned) = 0x00 00 04 D2
* as BCD: 0b 00000000 00000000 00010010 00110100 = 0x 00 00 12 34
* as float: 0b 01000100 10011010 01000000 00000000 = 0x 44 9a 40 00
(I added some spaces to divide the bytes)

Klaus
 

varied from 0 to 1200 in decimal

Does this mean you can receive 1 or 2 or 3 or 4 ascii values? Then you might need to wait until all data arrive, before you start to calculate a total value.

Is there a keyword you can call in your programming language, which converts a string to a numeric variable? That could be an easy way to do it.

Example: x = VAL ( a$)
 

Hi,

Independent of how many bytes are received....the array is 4 bytes wide.

Then the question is what is the initial value of the array?

Klaus
 

Hello !!
how to get array ascii data like 1000.100 in single value??
can i use same method betwixt >???
 

Hi,

first define your dataformat. Else we have to guess.

"1000.100" does that mean ALWAYS 8 bytes?
* or is "1000.1" allowed, too?
* is "0" allowed?
* is "12345678" allowed?
* is "123.4567" allowed?

Generally Brian´s method is good, but you need to take care of all (allowed) special cases.

Klaus

*****
You want us to help. So please be so kind to answer our questions, or at least give some feedback.
 

hello !!

actually, i'm getting weight from weight scale who gives values in format of "3 digits . 2 digits"
could i use Brian's method ???
Thanks a lot
 

Hi,

First there were 4 bytes, ..then we requested the data format...
then there were 4 bytes integer and 3 bytes fractional
now there are 3 bytes integer and 2 bytes fractional

Are you kidding us?

Klaus
 

my first post and last one are different ...

the last post is ascii format which one is just example
 

You can use the same method but as everyone pointed out, you need to know the format of the ASCII data first. If the decimal place is fixed, it wouldn't normally be sent over a serial link, there would be no point if it never changed but then you would have to be careful how you segregated the integer and fraction parts. You can use fractional multipliers like this for example: (based on my previous code)
Code:
float Total;
Total = (data[5] - '0') * 1000;
Total += (data[4] - '0') * 100;
Total += (data[3] - '0') * 10;
Total += (data[2] - '0');
Total += (data[1] - '0') * 0.1;
Total += (data[0] - '0') *0.01;

Note the final result 'Total' has to be a float type rather than integer. In that code "123456" would convert to 1234.56 in decimal.

Brian.
 
  • Like
Reactions: hirenn

    hirenn

    Points: 2
    Helpful Answer Positive Rating
Hello Brian
thanks for reply,
what to do if the point "." is variable in data for example,.. 123.56 or 1234. 56 or 12.56

Thanks !!
 

what to do if the point "." is variable in data for example,.. 123.56 or 1234. 56 or 12.56
Your code becomes variable, too. E.g. using an iteration loop and if () statements.
 

Hi,

what to do if the point "." is variable in data for example,.. 123.56 or 1234. 56 or 12.56
Maybe we forgot to say:

Therefore a software engineer needs a specification of the data format...before starting to write code.
Giving such informations afterwards and piece by piece is annoying and demotivating.

Klaus
 

Agreed, but there is a different method if the character may contain a decimal point and it's position might vary according to the value. Instead of treating the array as individual digits, treat it as a whole ASCII string and use scanf functions to do the conversion. Note that adding scanf functions might significantly increase program size because they cater for several different formats.

The method is to ensure there is a NULL (binary zero) character at the end of the array so you might have to make it one location bigger if there isn't enough space. Then use:

Code:
float Total;
sscanf(data,"%f",&Total);

Brian.
 
  • Like
Reactions: hirenn

    hirenn

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top