Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

425
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda