Questions tagged [strtol]

strtol() is the C runtime library function for converting the text representation of a number to a long integer. This SO tag also applies to strtoll(), strtoul(), and strtoull() which perform the same conversion to types "long long", "unsigned long", and "unsigned long long".

Use this tag on for all questions about the use of the strtol() family of functions or where such a function does not seem to be working correctly.

Closely related are:

  • for converting text to int (includes atol(), atoll(), and atoq())
  • atol() for long
  • for double
  • for double with unambiguous error indication
  • for converting one or more values at a time directed by a format specification

SYNPOSIS

#include <stdlib.h>

              long int strtol   (const char *nptr, char **endptr, int base);
         long long int strtoll  (const char *nptr, char **endptr, int base);
     unsigned long int strtoul  (const char *nptr, char **endptr, int base);
unsigned long long int strtoull (const char *nptr, char **endptr, int base);

BSD also has

                quad_t strtoq   (const char *nptr, char **endptr, int base);
              u_quad_t strtouq  (const char *nptr, char **endptr, int base);

strtol() converts from text in any base from 2 through 36. Or it can choose a base automatically (if base is specified as zero) same as the C compiler does depending on how the number is written: a leading 0 chooses octal, a leading 0x or 0X chooses hexadecimal, else decimal. Leading whitespace is skipped.

If endptr is not NULL, the pointer is set to the next character not converted. A conversion error is indicated by errno set to EINVAL (but set errno to zero before calling).

strtol() returns the result of the conversion, unless the value would underflow or overflow. It returns LONG_MIN if an underflow occurs, and LONG_MAX in case of an overflow. In both cases, errno is set to ERANGE. Similar is the case for strtoll() (LLONG_MIN and LLONG_MAX instead of LONG_MIN and LONG_MAX respectively).

175 questions
0
votes
2 answers

Split a string and convert individual pieces to hexadecimal

I have a string like "03FE" holding hexadecimal values. I need to split this string in two parts, and convert each individual part to the equivalent hexadecimal. That is, I need 0x03 in one variable and 0xFE in another variable. For example, if I…
Randomly Named User
  • 1,889
  • 7
  • 27
  • 47
0
votes
0 answers

C 2D array of strings,strtol or atoi for returning int and histogram from that

This is function for show how many times are some numbers in string,but 2D array dont work for me string is like a Fift Evenue 45/01 and in hist_arr must be on possition 0-1 1-1 2- 3- 4-1 5-1 6- 7- 8- 9- void funkcia_h(char **arr,int rows) { …
Da No
  • 69
  • 2
  • 10
0
votes
3 answers

C, how to put number split over an array, into int

Lets say I have char array[10] and [0] = '1', [1] = '2', [2] = '3', etc. How would i go about creating (int) 123 from these indexes, using C? I wish to implement this on an arduino board which is limited to just under 2kb of SRAM. so…
Simon.
  • 1,886
  • 5
  • 29
  • 62
0
votes
5 answers

Casting issues using argument input from C

I am trying to write a C program, where a user must input two arguments into the command line. First, they must provide a name of a text file of values to be read. Second, they must provide a value of 0 or 1, which will be saved as an integer to be…
user4148072
0
votes
3 answers

Using strtol to get long double in c++

I am trying to get the long double out of an array. long double num; char * pEnd; char line[] = {5,0,2,5,2,2,5,4,5,.,5,6,6}; num = strtold(line1, &pEnd); For some reason the num i am getting is rounded to 502522545.6 I am quite new to C++…
olv1do
  • 13
  • 1
  • 5
0
votes
1 answer

token to unsigned printing odd values

As the title suggested I am getting some weird returns from strtoul, I have tested just by using strcpy to store the data as a string and it is giving the correct value, but as soon as I try and change it to an unsigned int by using strtoul() I am…
Rory Thoman
  • 69
  • 12
0
votes
2 answers

Using strtol to filter bad command prompts and print error messages (C)

So I compile the program and run it like ./beetle int int, basically the executable and 2 ints follow, I want to make a test for if theres more than 2 arguments after the executable, if there is an argument that is not an int (like 300b) and if…
FullCombatBeard
  • 323
  • 1
  • 11
0
votes
2 answers

Finding a C# equivalent (that specifies a base parameter) to strtol

I need to convert a string to a long in c#. I'm porting a C++ program that currently uses strtol to do this. Because MSDN defines a long data type as a "signed 64 bit integer", I am using the following line of code to do the conversion in C#: long…
Eric after dark
  • 1,768
  • 4
  • 31
  • 79
0
votes
1 answer

Why strtol returns wrong value?

char xs[7] = {'0','0','0','1','0','1','0'}; long ss = strtol(xs, NULL, 2); after 2nd line ss is 2147483647, any ideas? following code is in the loop (outside it works fine)
user123454321
  • 1,028
  • 8
  • 26
0
votes
2 answers

strtol giving same answer for two different hex strings

So I have two hex strings - "3b101c091d53320c000910" and "071d154502010a04000419". When I use strtol() on them I get same value for both strings. I tried the following code- string t1="3b101c091d53320c000910"; long int…
Karlee
  • 9
  • 1
0
votes
1 answer

Strtol and Glib Buffers causing appcrash

I'm trying to take data read from a TCP socket with the g_io_channel_read_chars method and convert it into a long integer. I've tried using strtol, atoi, not casting ScanLine as a gchar pointer, accessing the first variable of ScanLine with…
Scott James Walter
  • 427
  • 1
  • 6
  • 20
0
votes
1 answer

Error checking for strtol() when parsing mac address and moving temp. pointer up

I have a function that converts a mac address from the forma00:11:22:33:44:55 to a 6 Byte array using strtol(). Now my function basically works fine but I'm wondering how I do proper error checking past the first conversion. thus far, my code looks…
stdcerr
  • 13,725
  • 25
  • 71
  • 128
0
votes
1 answer

converting from string to int using strtol() : \0' or '\n'?

I have a doubt with conversion from string to int. I got a string through function fgets then I used strtol function to convert it to int. This is the code : #include #include #include #include #include…
aeroxr1
  • 1,014
  • 1
  • 14
  • 36
0
votes
1 answer

hex number (str) interpretation with strtol

I would like to interpret a mac string formatted like 00:11:22:33:44:55 as 6 binary bytes i.e. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55. I've attemped to accomplish this with following code: #include #include #include void…
stdcerr
  • 13,725
  • 25
  • 71
  • 128
0
votes
1 answer

Flow handling in interrupt driven UART - ATMEGA328P

I'm trying to implement interrupt driven UART communication with an ATMEGA328P. What I need to achieve is sending commands (char arrays) over UART in order to extract values to my variables in the main routine so that I can program a behaviour in my…
Joum
  • 3,189
  • 3
  • 33
  • 64