Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

strtoul - string to int

Status
Not open for further replies.

deepsheep

Programmer
Sep 13, 2002
154
CA
I'm trying to use strtoul to convert a string to an int. However, I need to use the 'end' pointer and am having issues doing it properly. I have a series of 2 character numbers stored like: 11223344. Each is actually a separate number : 11 22 33 44. The code I have is:

strtoul(&time_array[0],&time_array[1],0);

And it returns a -1.
I've got another way to do it, but would like to know what I'm doing wrong. I'm having issues finding good resources on the internet.

Thanks!
 
The bugs

1) 2nd parameter should be a pointer to a char*
2) 3rd parameter should not be 0. If your string is hex, then it should be 16. If it is decimal, it should be 10. I've never tried anything beyond 16: you'll just have to play with it to find out what it will accept if you want something like base 19.

Quite surprised your code actually compiles. Anyway, here is an example
Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char* argv[])
{
   char teststr[] = "11 22 33 44";
   char *next, *curr;
   unsigned long val;
   
   curr = teststr;
   while (strlen(curr) != 0)
   {
      val = strtoul (curr, &next, 10);
      printf ("%d\n", val);
      curr = next;
   }
   return 0;
}
 
I figured it would be something like that.

My only question: Where are you setting the 'next' variable?

I need to start part-way in the array (there's junk before it), so this is how I tried and it didn't work (still getting -1 for return):

curr = &time_array[pos];
next = &time_array[pos+2];
data = strtoul (curr, &next, 10);
curr = next;

Thanks!
 
Is your string like this: "11 22 33 44" or this: "11223344"?
 
The second one. It's 6 2-digit numbers smushed together in a string.
 
next is set in
Code:
val = strtoul (curr, &next, 10);
You do not need to set it before you enter the routine. I've modified the example for starting mid way.
Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char* argv[])
{
   char teststr[] = "pairs11 22 33 44";
   char *next, *curr;
   unsigned long val;
   
   curr = &teststr[5];
   while (strlen(curr) != 0)
   {
      val = strtoul (curr, &next, 10);
      printf ("%d\n", val);
      curr = next;
   }
   return 0;
}
 
xwb: I don't think that will work since your example uses:
Code:
char teststr[] = "pairs11 22 33 44";
but deepsheep's string is "11223344".

What he really needs is some type of strntoul() function, which I've never seen before.

All I can think of is creating a temporary char[3] array and copying each group of 2 chars into it, then converting that temp string into a number.
 
> It's 6 2-digit numbers smushed together in a string.
If you know that, then just convert the whole 6 digits into a single number using strtol(), then use
[tt]x % 100[/tt]
and
[tt]x / 100[/tt]
to extract pairs of digits.


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top