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. =/
– Florida
I corrected the syntax errors. http://ideone.com/AScZPe
– zentrunix
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
foroi
.– Florida
The structure is the same as the previous one. http://ideone.com/UWNRvc
– zentrunix
Perfect, thank you for updating, even though I didn’t make the post clear enough at the beginning. }
– Florida
Another example, now with the "0x" 'in front of the hexa character code. http://ideone.com/nLYjgk#stdin
– zentrunix
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.
– Florida