J. J. Farrell said:
(e-mail address removed) wrote:
I have been trying to convert a 32 bit string like
"b2a4fdf15af7c4f215e1909be4707bdb"
What do you mean by a "32 bit string"? This looks like a
straightforward C string.
to convert to a string of special
chars like "²¤ýñZ÷Äòá?›äp{Û"
I have tried a lot of things .. but nothing works . I have no idea
how to go further .
Can anyone help me ?
We have no idea how to do this conversion. You have to tell us what the
algorithm is. What is the relationship between the initial string and
the final string? If you tell us what needs to be done to get from the
original string to the final one. we can help you implement it in C.
Thanks
This is a hexadecimal number .
[b2][a4][fd][f1][5a][f7][c4][f2][15][e1][90][9b][e4][70][7b][db]
and the special chars are the ISO-8859-1conversion of those hexadecimal
number .
Making the compiler output those bytes is not difficult; but ensuring
that they are interpreted as ISO-8859-1 is not possible in strictly
portable C code.
My hexbytes function below will convert your string containing
hexadecimal representations of bytes into a string containing the bytes
themselves.
The print_iso88591_glibc function achieves the task of interpreting the
bytes as ISO-8859-1, converting them to wide characters, and then
outputting them to the stdout stream. The particular locale name used is
one that exists on my system but may not exist on yours.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <wchar.h>
/* hexbytes function to interpret each two characters in src string
as a hexadecimal byte and store the resulting string into dst */
void hexbytes(char *dst, const char *src)
{
char tmp[3] = {0, 0, 0};
const char *p;
char *q;
if(strlen(src) % 2)
{
fprintf(stderr, "Odd length is invalid input\n");
exit(EXIT_FAILURE);
}
for(p = src, q = dst; *p; p += 2, q++)
{
tmp[0] = p[0];
tmp[1] = p[1];
*q = strtol(tmp, NULL, 16);
}
*q = 0;
}
void print_iso88591_glibc(const char *str)
{
wchar_t *dst = malloc((strlen(str) + 1) * sizeof *dst);
if(!dst)
{
fprintf(stderr, "Unable to allocate memory\n");
exit(EXIT_FAILURE);
}
if(!setlocale(LC_CTYPE, "en_US.ISO-8859-1"))
{
fprintf(stderr, "Unable to set ISO-8859-1 locale\n");
exit(EXIT_FAILURE);
}
mbstowcs(dst, str, strlen(str));
if(!setlocale(LC_CTYPE, ""))
{
fprintf(stderr, "Unable to set default locale\n");
exit(EXIT_FAILURE);
}
fputws(dst, stdout);
free(dst);
}
int main(void)
{
const char *src = "b2a4fdf15af7c4f215e1909be4707bdb";
char *dst = malloc(strlen(src) / 2 + 1);
if(!dst)
{
fprintf(stderr, "Unable to allocate memory\n");
exit(EXIT_FAILURE);
}
hexbytes(dst, src);
print_iso88591_glibc(dst);
free(dst);
putwchar(L'\n');
return 0;
}