Hallo, Is it possible, that I can add the strings '1' '.' and '2'. I want to add this 3 strings: 1.2 and convert it into a float value and than I want add a float value.
Usually, the appropriate library functions can be found in string.h (strcat() ) and stdlib.h (atof() ).
No. I only can cut, copy or compare strings but I can not add them. d = a + b + c; e = (float) d; The compiler convert all char into ASCII numbers. I need e = 1.5 and no ASCII nuber.
I think you need to go back to basics, and think about what a string actually is.
A string could be something like "apples" or "oranges" - so how could you meaningfully compute a sum of those?!
The nearest thing would be to concatenate them; eg,
string1 := "apples" string2 := "oranges" string3 := string1 + string2 -- gives "applesoranges"
If your strings represent numbers, then you first need to convert them into numerical values.
Look-up the standard 'C' library, or write your own custom function to convert between strings and numbers...
My problem is: I get form the MDU 2 components. One component before and on after the comma. I must add them to a float value. Example: before: 5 after: 2 The value of my division is 5.2. I need this value. Idea: I convert these two values and add them with a comma to a string value. That works but if I want to convert this string back to a float value, the compiler converts the comma into an ASCII sign.
Have you even looked at the descriptions of the two functions I mentioned ?
You cannot "add" strings (at least in C. Maybe in BASIC or something). You concatenate them. That's what strcat() (from string.h) does.
atof() converts a string to a floating point number.
"My problem is"
Your problem is that you have still not understood the nature of a string, and the difference between strings and numerical values.
"I get form the MDU"
What is an "MDU"?
A string is, by definition, for text - in C251, that means ASCII-coded characters.
To do arithmetic, you need numerical values such as int or float.
Standard library functions exist to convert between strings & numerical values - look them up! Alternatively, write your own.
I don't know what a MDU is. Does it return int's or strings? If they're strings you should use Andy's approach, converting the numbers to strings, concatenate them with a comma/point in between and converting the result back to a float.
If you get numbers from your "MDU", you might be able to do this? (float) (5 + (0.1 * 2)) i.e. (float) (MDU.component1 + (0.1 * MDU.component2))
As I see it, you want to convert the second MDU component to a fraction?
Regards, Joost Leeuwesteijn
"If they're strings you should use Andy's approach, converting the numbers to strings,"
No!
He wants to do arithmetic. Doing arithmetic requires numerical values.
Therefore, if this "MDU" thing returns strings, he needs to convert those strings to numerical values!
Then he can do his arithmetic with those numerical values.
He might then need to convert the resulting numerical value to a string for display, transmission, or whatever...
You're right. I mistyped two solutions into one.
If he gets strings: - convert to numbers and do arithmetic - concatenate with point inbetween, then convert to float and do arithmetic
If he gets numbers: - do arithemetic - or less efficient way: convert numbers to strings, concatenate with point inbetween, convert back to float; but this doesn't make much sense but I believe that's what he (OP) had in mind.
-- Joost Leeuwesteijn
Sounds more like a class assignment. Given two numbers, which happen to be represented as strings, add them together.
It is possible to do this without converting the strings to integers and back. (Though, using existing libraries likely would be the easiest and fastest way!). You could write code to loop over the strings, from the end to the beginning (least significant characters first), adding each individual pair of characters, and keeping the result as a character. That is, the same algorithm you were probably taught to use when doing the arithmetic by hand.
Skipping all sorts of error and bounds checking, this should do: sprintf (outStr, "%d", atoi(inStr1) + atoi(inStr2));
You said: "I get form the MDU 2 components. One component before and on after the comma. I must add them to a float value. Example: before: 5 after: 2 The value of my division is 5.2."
If read this as: "I get from the MDU this string "5,2" and I wish to convert them to a number." then I would say you float a; char MDU[4]; strcpy(MDU,"5,2"); MDU[1]='.'; a = atof(MDU); a will equal 5.2 In this case I replace the "," with a "." since that is what my atof expects. Maybe yours can just handle the "," as though it were the radix. Some places use "." for the radix and some use ",".
If you read it as "I get from the MDU this string "5,2" and I wish to convert them to a number."
Then surely the simplest approach is:
float float_val; float_val = MDU[0]-'0'; // Numerical value of the 1st character; // ie, the units digit float_val += (MDU[2]-'0')/10; // Numerical value of the 3rd character; // ie, the tenths digit
Simple it is. But also, unfortunately, wrong. One would have to divide by 10.0 instead of 10, or multiply by 0.1 to get the desired effect.
And that's before considering that floating-point arithmetic on an embedded CPU like the 251 is generally a dubious choice --- putting it mildly. It's best to either avoid it completely, or delegate that part of the job to a tools suited for it: a DSP.
"Simple it is. But also, unfortunately, wrong. One would have to divide by 10.0 instead of 10"
Oops - so one would!
"floating-point arithmetic on an embedded CPU like the 251 is generally a dubious choice"
That's my excuse - I never touch the stuff, so missed the obvious detail... ;-)
Adds another side the "Sledgehammer to crack nut" title, though!
One way to avoid floating-point is to choose one's units appropriately; eg, instead of using "5.2 whatsits", use "52 centi-whatsits" or 520 milli-whatsits" - and then use integer arithmetic.
eg, in this case:
int num_val; num_val = (MDU[0]-'0')*10; // Numerical value of the 1st character; // ie, the tens digit in centi-whatsits num_val += (MDU[2]-'0'); ; // Numerical value of the 3rd character; // ie, the units digit in centi-whatsits
or
int num_val; num_val = (MDU[0]-'0')*100; // Numerical value of the 1st character; // ie, the hundreds digit in milli-whatsits num_val += (MDU[2]-'0')*10; // Numerical value of the 3rd character; // ie, the tens digit in milli-whatsits
Erm, at the risk of being a pest, I'm afraid your blow missed the nut by an order of magnitude, which is bad even for a sledgehammer: 5.2 whatsits is 520 centi-whatsits.
But I'm willing to forgive people educated on "imperial" units for mixing up their SI prefixes --- as long as they extend similar courtesy to people baffled by the relation between inches and pints. ;-)