Convert hexadecimal string to C-readable output

Asked

Viewed 3,401 times

2

I would like to save a text in hexadecimal, and calling a function, the hexadecimal string is converted to a readable char string, i.e., decoding the hexadecimal, for example 0x6f0x69 for oi.

I tried the following:

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <string.h>

int main(void) {

    char hex[3];
    hex[0]="0x69";
    hex[1]="0x20";
    hex[2]="0x31";

    char text[100];
    int i = 0;
    char decimalNumber[100];
    while(i<3;i++){
        sscanf(hex,"%x",&decimalNumber);
        text+=decimalNumber;
    }
    printf("The value of decimalNumber is %s\n",text);
    return 0;
}

If possible, in some way that allows me to save different strings and calling only one function for their conversion, example:

char hex1 = ...
char hex2 = ...
char srt[50] = converter(hex);

1 answer

3


Using your example.

Now putting the complete program, tested on ideone.


#include <stdio.h>
#include <string.h> //strlen

int main()
{
    // para loop "for"
    int i;

    // area de entrada
    char hex[] = "6f69";
    int sizeHex = strlen(hex);

    // area de saida
    char text[10];

    // variaveis de conveniencia
    char ch, high, low;

    // trata todos os caracteres da area de entrada
    for (i = 0; i < sizeHex; i += 2)
    {
       high = hex[i];
       high -= 0x30;
       if (high > 9) high -= 7;
       high <<= 4;

       low = hex[i+1];
       low -= 0x30;
       if (low > 9) low -= 7;

       ch = high | low;

       // transfere para area de saida            
       text[i/2] = ch;
    }

    text[i/2] = 0; // para delimitar string

    printf("string hexadecimal: %s\n",text);
}

Recital "0x":

#include <stdio.h>
#include <string.h> //strlen

int main()
{
    // para loop "for"
    int i;

    // area de entrada
    char hex[] = "0x6F0x69";
    int sizeHex = strlen(hex);

    // area de saida
    char text[10];

    // ponteiro para area de saida
    char* pText = text;

    // area de trabalho, vai ser usada com scanf --> vai 0x6F, depois 0x69, etc
    char tmp[5];

    // variavel de conveniencia
    int ch;

    tmp[4] = 0; // paa delimitar string de trabalho

    // trata todos os caracteres da area de entrada
    // anda de 4 em 4 porque cada caracter esta' codificado como "0xNN"
    for (i = 0; i < sizeHex; i += 4)
    {
       memcpy(tmp, hex+i, 4);
       sscanf(tmp, "%x", &ch);

       // transfere para area de saida, e avanca ponteiro na area de saida
       *pText++ = (char)ch;
    }

    *pText = 0; // para delimitar string

    printf("string convertida: %s\n",text);
}

It’s not what was asked, but I find it interesting too.

#include <stdio.h>

int main()
{
    // para loop "for"
    int i;

    // area de entrada
    char hex[3];

    // area de saida
    char text[10];

    // variaveis de conveniencia
    char ch, high, low;

    // constante caracter usas aspas simples (apostrofo), e '\xNN'
    hex[0] = '\x69';
    hex[1] = '\x20';
    hex[2] = '\x31';

    // trata todos os caracteres da area de entrada
    for (i = 0; i < 3; i++)
    {
       // pega um caracter da area de entrada
       ch = hex[i];

       // converte meio byte 'a esquerda para caracter hexa visivel
       high = ch >> 4;        // desloca 4 bits 'a direita
       high &= 0x0F;          // zera os 4 bits da esquerda
       if (high > 9) high += 7; // se for maior que 9, ajusta o valor para resultar numa letra
       high = high + 0x30; // transforma em caracter visivel

       // repete para meio byte 'a direita (nao precisa deslocar 4 bits)
       low = ch & 0x0F;
       if (low > 9) low += 7;
       low = low + 0x30;

       // transfere para area de saida            
       text[2*i] = high;
       text[2*i+1] = low;
    }

    text[2*i] = 0; // para delimitar string

    printf("string convertida: %s\n",text);
}

  • I tested in the ideone and it didn’t work. =/

  • I corrected the syntax errors. http://ideone.com/AScZPe

  • I think you didn’t understand or I didn’t make it very clear, this should convert the Hex string into char, but decoding its value, example 6f69 for oi.

  • 1

    The structure is the same as the previous one. http://ideone.com/UWNRvc

  • Perfect, thank you for updating, even though I didn’t make the post clear enough at the beginning. }

  • 1

    Another example, now with the "0x" 'in front of the hexa character code. http://ideone.com/nLYjgk#stdin

  • You could include the content of the two links in the answer so that more people can find it, as external links may disappear. This would influence to better the quality of your response.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.