# converting char into integer

Hi. I have such a question, how can I convert some char into int? Example: char a =23.50; and I need to get the same number 23.50 but integer form.

A char cannot contain a floating point value.

You can use a float to do what you ( may ) want.

``````float f = 23.5f;
int i = f;
``````

However the decimal portion is dropped and i = 23, not 23.50, or 23.00

Maybe you have a string ( of chars )?

``````int i = atoi( "23"  );
//Or
float f = atof( "23.50" );
``````

actually i get the char data from serial read as you can see from my code

``````void setup()
{
Serial.begin(9600); // Initialize serial port to send and receive at 9600 baud
}

void loop()
{
while (Serial.available())
{
Serial.print(angle);
``````

and the reading in serial monitor is like this -0.24 -0.25 -0.3 -0.3 -0.25 -0.24 -0.23 -0.23 -0.32 -0.37 -0.4 -0.65 -0.93 -1.21 -2.94 -4.8 -5.24

Several things: 1) data coming from the serial port is being read one byte at a time, so if “-0.24” is coming from a Serial.read(), all angle is going to get is “-”. 2) You can convert 23.50 into an int if you want, but all you’re going to get is 23. The int data type cannot have a fractional component. That is, everything after the decimal point is ignored.

What marks the end of one of your values coming in via Serial.read()? If it is terminated with a newline character (’\n’), you might try: Serial.readByteUntil(’\n’, buffer, 10), which says read all the bytes you find up to the newline character and save them in buffer[]. Note that buffer[] is defined as a character array with up to 10 elements:

``````char buffer[10];
``````

Then you could use dtostrf() to convert buffer[] into a floating point number.

than you so much it is very helpful information. but i have some confusing in how to write the code comparing with my code can you help me in this part? my regard

Try this…

``````void setup() {

Serial.begin(115200);
char buffer[] = "-23.5";  // Note this MUST be an array of characters...
int val;

val = atof(buffer);
Serial.println(buffer);
}

void loop() {

}
``````

Ok, but i don't want to convert only one value. i need to convert all values that received from serial read and need to located on void loop.