473,408 Members | 2,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Hex digits to special chars

Hi ,

I have been trying to convert a 32 bit string like
"b2a4fdf15af7c4f215e1909be4707bdb" 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 ?

Thanks in advance.

Aug 1 '06 #1
8 2889
ab*****@gmail.com wrote:
I have been trying to convert a 32 bit string like
"b2a4fdf15af7c4f215e1909be4707bdb" to convert to a string of special
chars like "=B2=A4=FD=F1Z=F7=C4=F2=E1?=9B=E4p{=DB"
I have tried a lot of things .. but nothing works . I have no idea
how to go further .=20
Can anyone help me ?
Yes; undoubtedly the teacher who gave you this homework can, if you ask
him civilly and show your work upto now.

Richard
Aug 1 '06 #2

Richard Bos wrote:
ab*****@gmail.com wrote:
I have been trying to convert a 32 bit string like
"b2a4fdf15af7c4f215e1909be4707bdb" to convert to a string of special
chars like "=B2=A4=FD=F1Z=F7=C4=F2=E1?=9B=E4p{=DB"
I have tried a lot of things .. but nothing works . I have no idea
how to go further .=20
Can anyone help me ?

Yes; undoubtedly the teacher who gave you this homework can, if you ask
him civilly and show your work upto now.

Richard
I am not sure if the special chars got printed correctly . The string
is
" Zᐛp{" .
Is it really that simple? if so ,,, then can you please tell me .. coz
my teacher also doesn't know !

Aug 1 '06 #3

ab*****@gmail.com 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.

Aug 1 '06 #4

J. J. Farrell wrote:
ab*****@gmail.com 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 .

Aug 1 '06 #5
I guess he means that it's a string of hex chars... Like
b2=178
a4=164
etc.
So he wants to print the ascii value of 178, the ascii
value of 164 etc ...

So, how anybody can do this ? Well, i'm not sure if there's
an easier way, but i'm thinking that a loop has to be written,
to get characters in pairs as the above from the string and
then convert each pair to an integer and print it's ascii value...

This one may be helpful for that conversion
http://www.thecodeproject.com/string/hexstrtoint.asp

--
Papastefanos Serafeim

? "J. J. Farrell" <jj*@bcs.org.uk?????? ??? ??????
news:11**********************@b28g2000cwb.googlegr oups.com...

ab*****@gmail.com 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.
Aug 1 '06 #6
ab*****@gmail.com wrote:
J. J. Farrell wrote:
>ab*****@gmail.com 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;
}

--
Simon.
Aug 1 '06 #7
Simon Biber wrote:
ab*****@gmail.com wrote:
J. J. Farrell wrote:
ab*****@gmail.com 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;
}

--
Simon.
Hi Simon ,

I ran your code on my solaris machine , where when i used to
run
this code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <wchar.h>

#define BUF_SIZE 100

int main(int argc, char* argv[])
{
int i;

unsigned char
mbBuf[BUF_SIZE]="\xb2\xa4\xfd\xf1\x5a\xf7\xc4\xf2\x15\xe1\x90\x9b \xe4\x70\x7b\xdb";
printf(mbBuf);

}

I used to get the following o/p =Zp{

After running the code which had the system call
setlocale(LC_CTYPE,"iso_8859_1")
The character code of the machines seems to have changed .And it's not
ISO-8859-1
And i am unable to revert it back because I don't know what the
previous
settings were .
Becuase now the same program shows the o/p as =>
Z-=ɢSp{

As you might have noticed ther earlier o/p were ascii equivalent of
the hex digits . But now i don't even know what this current encoding
is .

The locale of my system shows :
LANG=
LC_CTYPE="C"
LC_NUMERIC="C"
LC_TIME="C"
LC_COLLATE="C"
LC_MONETARY="C"
LC_MESSAGES="C"
LC_ALL=

Can you please help me as to what those previous settings should be to
get
the ascii equivalents ??

Aug 3 '06 #8
On Thu, 3 Aug 2006 05:44:05 UTC, ab*****@gmail.com wrote:
Simon Biber wrote:
ab*****@gmail.com wrote:
J. J. Farrell wrote:
>ab*****@gmail.com 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 .

unsigned char
mbBuf[BUF_SIZE]="\xb2\xa4\xfd\xf1\x5a\xf7\xc4\xf2\x15\xe1\x90\x9b \xe4\x70\x7b\xdb";
printf(mbBuf);

}

I used to get the following o/p =Zp{
You tries to print some binary data. mbBuf contains a set of binary
data but no printable chars. Anything that printf prints seems ok on
that content. You would have to convert each digit to a printable char
and print that.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
Aug 4 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: tertius | last post by:
Is there a better way to append certain chars in a string with a backslash that the example below? chr = "#$%^&_{}" # special chars to look out for str = "123 45^ & 00 0_" # string to...
6
by: Jonas Meurer | last post by:
hello, my script selects a comment saved as VARCHAR in MySQL and displays it inside an html page. the problem is, that the comment contains several special characters, as mysterious utf-8...
2
by: Thorsten Viel | last post by:
Hi List, im trying to use special chars with the std::string (like S) but when i cout<< the string the special chars have disappeared. What's wrong here? Thanks in advance
4
by: dutch disCo | last post by:
Hi, I'm trying to use GET method to pass variables through my script files. Unfortunately, when a special char (&, ?, +) is being posted the variable gets trimmed in a very strange way. I'm...
3
by: VMI | last post by:
When I execute a ReadLine from an ascii file with special chars (ie. the '' in "NUEZ PEREZ"), it automatically deletes this character. So "NUEZ PEREZ" becomes "NUEZ PEREZ". How can this be...
0
by: hoenes1 | last post by:
Hi all, I have a standard html form containing several textboxes. Since this is a german application, the boxes are likely to contain special characters like , , , etc. The form is passed to...
10
by: Niklas Engfelt | last post by:
Does anyone have a nice algorithm/method to increase all digits one step, e.g. 123123 to 234234 and 4444 to 5555 and 999999 to 000000? Best regards Niklas
14
by: abhi147 | last post by:
Hi , I have a program where I want to print a string "457e31b2db200dc125f3e00886ff57de" like "\x45\x7e\x31\xb2\xdb\x20\x0d\xc1\x25\xf3\xe0\x08\x86\xff\x57\xde" .. But in each and every case it...
5
by: Alex | last post by:
Hi all - Is there a standard way to handle special chars in strings in dotnet? I'm using csharp and having two seperate problems... 1. Passing in args... I need to pass in an arg that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.