#include “mbed.h”
#include
#include
#include
#include
using namespace std;
int ascii_to_int_conv(char ascii);
double ascii_to_double_conv(char ascii);
int main(){
printf("\033[2J\033[1;1H"); // clear terminal
//string that needs to be converted
string str = "54.1"; // input
char charArray[str.length() + 1]; // +1 for null terminator
//fill char array
strcpy(charArray, str.c_str()); // fills the char with the sting
// output the copy
cout << "Result: " << charArray << endl; // output to check the char converstion has been done
double result;
// standard lib convertion
result = atof(str.c_str());
// OUTPUT: result 536895960
result = ascii_to_double_conv(charArray); // convert the array to double
result = ascii_to_int_conv(charArray); // convert the array to double
printf("result %d\n" , result);
// cout << "Result: " << result << endl; //output the result
}
int ascii_to_int_conv(char ascii)
{
//cout << ascii << endl;
int idx = (sizeof(ascii)) - 2;
int value = 0;
// Fill each element of the value array individually
for (int i = 0; i < idx; ++i) {
value = value * 10 + (ascii[i] - 48);
}
// Output the entire concatenated number
//cout << "The concatenated number is: " << value << endl;
printf("the concatenated number is %i \n", value );
return value;
}
double ascii_to_double_conv(char ascii) {
int idx = sizeof(ascii);
double value = 0.0;
double decimalMultiplier = 1.0; // To handle decimal places
int decimalPointIndex = -1;
// Fill each element of the value array individually
for (int i = 0; i < idx; ++i) {
if(ascii[i] == '.')
{
decimalPointIndex = i;
}
if (decimalPointIndex != -1)
{
value = value * 10 + (ascii[i] -48);
decimalMultiplier *= 0.1;
}
else
{
value = value * 10 + (ascii[i] - 48);
}
}
// Adjust value for decimal places
if (decimalPointIndex != -1) {
value *= decimalMultiplier;
}
printf("The concatenated number is: %d\n", value);
return value;
}