Converting Strings to Numerical Values

From Nutwiki
Revision as of 17:02, 27 October 2016 by Harald (Talk | contribs) (1 revision imported)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Test Environments

Hardware Comments Nut/OS
4.8.7
Ethernut 1.3 H OK
Binaries
Compiler: AVR-GCC 4.3.2
Ethernut 2.1 B OK
Binaries
Compiler: AVR-GCC 4.3.2
Ethernut 3.0 E OK
Binaries
Compiler: ARM-GCC 4.3.3

Overview

This example demonstrates a few methods for converting strings containing numbers to numerical values.

Source Code

<source lang="c">

  1. include <dev/board.h>
  2. include <stdio.h>
  3. include <io.h>

int main(void) {

   unsigned long baud = 115200;
   char input[256];
   int number = 0;
   NutRegisterDevice(&DEV_UART, 0, 0);
   freopen(DEV_UART_NAME, "w", stdout);
   freopen(DEV_UART_NAME, "r", stdin);
   _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
   puts("\nNut/OS String to Numerical Value Conversion Demo");
   puts("Please enter a number: (whitespaces and trailing characters allowed)");
   scanf("%s", input);
   printf("\nYour number converted to...\n"

"...int: %d\n"

          "...long: %ld\n\n", atoi(input), atol(input));		   
   sprintf(input, "hello 123 world");
   printf("A string contains the following value: %s\n"

"We are going to extract the number from it.\n", input);

   sscanf(input, "%*s %d %*s", &number);
   printf("The number was: %d", number);
   for (;;);
   return 0;

} </source>

Details

<source lang="c"> unsigned long baud = 115200; char input[256]; int number = 0;

NutRegisterDevice(&DEV_UART, 0, 0); freopen(DEV_UART_NAME, "w", stdout); freopen(DEV_UART_NAME, "r", stdin); _ioctl(_fileno(stdout), UART_SETSPEED, &baud);

puts("\nNut/OS String to Numerical Value Conversion Demo"); </source>

Since we are going to ask for some input for the example, we need the UART driver, not the debug one.

<source lang="c"> puts("Please enter a number: (whitespaces and trailing characters allowed)"); scanf("%s", input);

printf("\nYour number converted to...\n"

      "...int: %d\n"
      "...long: %ld\n\n", atoi(input), atol(input));		   

</source>

First we let the user enter a string. Strings can contain lots of different things but in this example we will try to obtain a numerical value from within it.

To do this we use the atol function which tries to parse a string into an integer value. It will first remove any whitespaces in front of the number and then attempt to convert the number into something that fits into its datatype or the highest possible value for the datatype. If there are any characters after the number, they are ignored.

The function atol is almost the same as atoi only that it doesn't return an integer but a long value. The difference is easily visible if you enter a number larger than an integer (for example 150000) as it will be too small when using atoi but display correctly using atol.

Which one to use depends on your application and data types.

<source lang="c"> sprintf(input, "hello 123 world"); printf("A string contains the following value: %s\n We are going to extract the number from it.\n", input);

sscanf(input, "%*s %d %*s", &number);

printf("The number was: %d", number); </source>

Besides that, there is another useful function: sscanf It allows us to break down a string into pieces as we need it. We'll use this handy function to obtain a number from the middle of a string. The first parameter specifies the string to operate on. The second parameter is a format string similar to printf or scanf, the following parameters are the variables we want to store our data in.

For this to work we also need the "*" format specifier, which does read the data accordingly but not store it anywhere. If you wanted to you could store any number of variables with this function.

Output

<source lang="text"> Nut/OS String to Numerical Value Conversion Demo Please enter a number: (whitespaces and trailing characters allowed)

 45bla

Your number converted to... ...int: 45 ...long: 45

A string contains the following value: hello 123 world We are going to extract the number from it. The number was: 123 </source>

See also