Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

424
Views
Convert hex string to unsigned long using strtoul()

I have this silly example program that takes an input value as hex and converts it to an unsigned long integer. After conversion I printf out the result.

#include <stdio.h>

int main (int argc, char *argv[])
{

    unsigned long int id = 0;

    id = strtoul(argv[1], NULL, 16);
    printf("value: %lx\n", id);

    return 0;
}

the problem is that running this program with an hex number larger than 31-bit causes the output value to be completely wrong. The upper 32 bits are all converted into (0xf). Shouldnt strtoul() be able to handle numbers up to 2^64? Why are bits >32 garbage? The machine is a X86_64 and actually, the result is also the same with strtoull().

$ ./ex 0x7fffffff
value: 7fffffff

$ ./ex 0x80000000
value: ffffffff80000000

$ ./ex 0x3cf180000000
value: ffffffff80000000
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Your solution works properly for me on my local machine. However, I believe you need to include:

#include <stdlib.h>

Strtoul is part of stdlib.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!