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

261
Vistas
How to convert Hexadecimal to Decimal?

I have different hexadecimal data are coming and stored into an integer type register.

When I use fprint I can see the following:

0x3076
0x307c
.
.
.

However, I would like to show a Decimal version of above-mention Hex data as follows.

12406
12412
.
.
.

In theory, let's say for the first value you do the following to convert it to decimal.

(6* 16^0)+(7 * 16^1)+(0*16^2)+(3*16^3)=6 + 112+ 0 + 12288 = 12406

So IF I have the character-based version of "0x3076" and also, IF I am able to get each single characters 6 - 7 - 0 - 3, I can calculate the decimal amount!

So, I decided to divide "3076" by 1000. I was expected to get 3, but I got TWO characters instead! however, if I was able to get 3 for the remainder of "307c" I am not able to get "C". If it was decimal this might work not hex!

Also, I tried "strtol" command. As I use Cygwin to compile my code, I am not sure where the error is! What did I do wrong?

So, I just need to find a way to get EACH SINGLE character out of the HEX data!

Any idea please?

P.S.

Here is my code in order to help you to give me an idea.

   int get_readings(int source, int phase, int max_tries)
{
    uint8_t buf[MAX_IEC1107_MSG_BODY];
    uint8_t inbuf[MAX_IEC1107_MSG_BODY];
    int inlen;
    uint8_t *s;
    int32_t value;
    int status;
    double voltage;
    double current;
    double active_power;
    double reactive_power;
    double apparent_power;
    double power_factor;
    double frequency;

    s = buf;
    *s++ = HOST_CMD_GET_READINGS_PHASE_1 + phase;
    *s++ = 0x00;
    if (max_tries != 1)
        meter_set_max_tries(&emeter[source].meter_msg, max_tries);
    if (meter_exchange(&emeter[source].meter_msg, buf, 2, inbuf, &inlen)
        &&
        inbuf[1] != 0xFF)
    {
        emeter[source].no_response = FALSE;
        s = inbuf;
        /* Get current readings */
        value = (s[3] << 8) | s[2];

        fprint(stderr, "value:" %p\n, value); // this give me HEX code

. . .

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

When reading in a string representing a number in hexadecimal, use strtol() to convert it to a long. Then if you want to print the number in decimal, use printf() with a %d format specifier.

char num[]="0x3076";
long n = strtol(num, NULL, 16);
printf("n=%ld\n", n);  // prints 12406

Once you read in the strings as longs using strtol and operate on the longs, the math should work itself out.

over 4 years ago · Santiago Trujillo Denunciar

0

unsigned n;
sscanf("0x3076", "%x", &n);
printf("%u\n", n);
over 4 years ago · Santiago Trujillo Denunciar

0

Only with the following line I could solve the issue!

fprintf(stderr, "Voltage: %.2f\n", value/100.00);

value was shown as HEX as I used to use %p! Then I changed to x! and as I need to have 124.06 not 12406, in other words 2 decimal points, I added .2f!

Thank you all!

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