Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

427
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda