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
1 answer

Why do I get wrong conversion from hex to decimal with strtoul function in Visual Studio compiler?

I am converting a string from hex to decimal. The problem is that in Visual Studio compiler the conversion returns a wrong value. However when I compile the same code in a Mac at the terminal using the g++ compiler, the value is returned…
0
votes
2 answers

using strtol on bytes stored in char array

I'm trying to extract 2nd and 3rd byte from a char array and interpret it's value as an integer. Here in this case, want to extract 0x01 and 0x18 and interpret its value as 0x118 or 280 (decimal) using strtol. But the output act len returns 0. int…
user967850
  • 31
  • 6
0
votes
0 answers

Strtol implementation different behaviour on 32 and 64 bit machine

#include #include #include #include #include #include #include #define NEGATIVE -1 #define POSITIVE 1 #define OCTAL 8 #define DECIMAL 10 #define HEXADECIMAL 16 #define…
0
votes
1 answer

Reading text files with strtol in C line by line

OK so I've got this function which finds the average of all numbers in a file: float averageOfNumbers(FILE *fp_in) { int n=0,S=0; char red[1024];char *ptr; int p_a_h; float sr; while(!feof(fp_in)){ …
glavata
  • 45
  • 4
0
votes
5 answers

Extract numbers from string doesnt work! c

i would remove the "-" from the ISBN String. But my code dont print me the value out. Where is the fault? char *ISBN[20]; //example: 3-423-62167-2 *p = ISBN; strcpy(ISBN, ptr); //Copy from a Buffer printf("\nISBN Array: %s", ISBN); //This…
Tommy
  • 15
  • 4
0
votes
2 answers

Translate string to number

I am looking for a way to take a string and check 3 possibilities. Digit and thus converts it to a signed int (not a long) Is a symbolic representation previously defined at runtime, and converts it to a signed int Neither The "symbolic…
0
votes
1 answer

strtoul to convert from String date ("03/10/2013 14:01:00") to time_t

I don't understand, why this doesn't work? PS: I found this piece of code from some google! Problem: I don't know why it should work? Does this consider timezone as well?! 1 #include 2 #include 3 #include 4…
code muncher
  • 1,592
  • 2
  • 27
  • 46
0
votes
1 answer

converting from binary string to int using strtoll?

I have to read from a text file that contains a bunch of binary numbers and then convert them to integers and store these in an array. I have made a function to do this, however, the function is only returning one number. It seems to not be going…
Andy
  • 281
  • 2
  • 7
  • 23
0
votes
3 answers

How to return a hex value after converting it from a string

I am trying to return a hex value inside this method. not sure where I'm going wrong. not sure how put the value into hex without using cout. have not been able to find a solution. Input value is always going to be 32 bits long its like i want to…
cmonnats23
  • 15
  • 1
  • 6
0
votes
2 answers

How to do a true strtoul? Wont intake an actual string

Having an issue inputting an ACTUAL string to strtuol. The input string SHOULD be an unsigned binary value of 32 bits long. Obviously, there is in issue with InputString = apple; but I'm not sure how to resolve the issue. any thoughts? This shouldnt…
cmonnats23
  • 15
  • 1
  • 6
0
votes
2 answers

How to convert from a binary string of 32 characters to hex?

I know there are tons of tutorials online about how to convert from a string to hex. Well, I am having an issue with that. My code (see below) works up to 31 characters and I cant for the life of me figure out why. Whenever there are 32 character it…
cmonnats23
  • 15
  • 1
  • 6
0
votes
1 answer

strtol c string function

I'm using input via redirection. I'm trying to store numbers from a string to an integer. The first number is 1989... however only '1' gets stored. Am I using strtol wrong? table is an array of structs value is an array of integers. //(*row, i, j)…
ShadyBears
  • 3,955
  • 13
  • 44
  • 66
0
votes
3 answers

Using strtol() to Parse Long Values out of Lines of a File

When I use strtol function to parse long values out of such lines of a file, ffffffff8105b4a5 t send_signal it just returns ffffffff instead of ffffffff8105b4a5!!! Here's the code: uint64_t value1; uint64_t value2; char *temp = (char *)…
Peggy
  • 639
  • 9
  • 28
0
votes
1 answer

Why are atoi and strtol only returning the first number from my string most of the time?

I'm trying to get ints from a c file that has input like this: (0 3 200 3) (0 9 500 3) (98 20 500 3) (100 1 100 3) (100 100 500 3) atoi and s work fine for the first number after the parenthesis (I use a while loop and strcat numbers larger than…
TomN
  • 5
  • 1
  • 4
0
votes
2 answers

Convert a segment of an Hex string to a double

I have a device that returns data as a Hex value, for example 00 00 00 56 00 00 01 00. I place this into an NSString. I am only interested in converting the 3rd and 4th segment to a double. ie only 00 56. On sites such as…
Ste Prescott
  • 1,789
  • 2
  • 22
  • 43