470,636 Members | 1,582 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,636 developers. It's quick & easy.

Hex string to ascii

Hi guys,
how can I convert a hex string in a sequence of chars?

I've thought, if I get a hex string like that:

54657374

I need to get number one for time and convert it in decimal in the
following way:

5x16^7
4x16^6
6x16^5
5x16^4
7x16^3
3x16^2
7x16
4
= number_tot

and now

char buf[256]
sprintf(buf, "%s", number_tot);

is it correct?

Is there a better way?
tnx

Dec 13 '06 #1
8 23398
Salvatore Di Fazio wrote:
how can I convert a hex string in a sequence of chars?
Take two consecutive hex digits and convert them into a char using
either sscanf or istringstream. repeat until you run out of hex digits.
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 14 '06 #2
Salvatore Di Fazio wrote:
how can I convert a hex string in a sequence of chars?

char buf[256]
sprintf(buf, "%s", number_tot);

is it correct?

Is there a better way?
you can try something like this:

#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>

using namespace std;

int main()
{
string val("54657374");
stringstream ss(val);
long l;
ss >hex >l;
cout << "val ='" << val
<< "', l = " << l
<< ", hex(l) ='" << hex << l << "'"
<< endl;
return 0;
}

Tobias Gneist
Dec 14 '06 #3
Tobias Gneist ha scritto:
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>

using namespace std;

int main()
{
string val("54657374");
stringstream ss(val);
long l;
ss >hex >l;
cout << "val ='" << val
<< "', l = " << l
<< ", hex(l) ='" << hex << l << "'"
<< endl;
return 0;
}
Hi Tobias,
I tried but in this way I don't get the ascii string value but a
number.
Tnx

Dec 14 '06 #4
"Salvatore Di Fazio" <sa***************@gmail.comwrites:
how can I convert a hex string in a sequence of chars?
A string is a sequence of chars.
>
I've thought, if I get a hex string like that:

54657374

I need to get number one for time and convert it in decimal in the
following way:

5x16^7
4x16^6
6x16^5
5x16^4
7x16^3
3x16^2
7x16
4
= number_tot

and now

char buf[256]
sprintf(buf, "%s", number_tot);

is it correct?
I guess not - number_tot seems to be of an integer type not char*.

If you want to convert hex string into a number use strtol(), ie:

#v+
#include <cstdlib>
#include <cstdio>

using namespace std;

int main(int argc, char **argv) {
if (argc==1) {
fptus("need an argument\n", stderr);
return 1;
}

while (--argc) {
char *end;
long num = strtol(*++argv, &end, 16);
if (*end) { /* you could also add errno checking */
fprintf(stderr, "%s: invalid number\n", *argv);
} else {
printf("%d\n", num);
}
}
return 0;
}
#v-

If you want to convert digits "in pairs" do:

#v+
#include <cstdlib>
#include <cstdio>

using namespace std;

int main(int argc, char **argv) {
if (argc==1) {
fptus("need an argument\n", stderr);
return 1;
}

while (--argc) {
char buf[3] = { 0, 0, 0 }, *end;
for (const char *arg = *++argv; arg[0] && arg[1]; arg += 2) {
buf[0] = arg[0];
buf[1] = arg[1];
long num = strtol(buf, &end, 16);
if (*end) {
fprintf(stderr, "%s: invalid number\n", buf);
} else {
printf("%3d ", num);
}
}
putchar('\n');
}
return 0;
}
#v-

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>---<jid:mina86*chrome.pl>--ooO--(_)--Ooo--
Dec 14 '06 #5
why don't you use the table-driven method?

"Salvatore Di Fazio" <sa***************@gmail.com写入消息新闻:11******* ***************@t46g2000cwa.googlegroups.com...
Hi guys,
how can I convert a hex string in a sequence of chars?

I've thought, if I get a hex string like that:

54657374

I need to get number one for time and convert it in decimal in the
following way:

5x16^7
4x16^6
6x16^5
5x16^4
7x16^3
3x16^2
7x16
4
= number_tot

and now

char buf[256]
sprintf(buf, "%s", number_tot);

is it correct?

Is there a better way?
tnx

Dec 14 '06 #6
On 13 Dec 2006 11:25:03 -0800, "Salvatore Di Fazio"
<sa***************@gmail.comwrote:
>Hi guys,
how can I convert a hex string in a sequence of chars?

I've thought, if I get a hex string like that:

54657374

I need to get number one for time and convert it in decimal in the
following way:

5x16^7
4x16^6
6x16^5
5x16^4
7x16^3
3x16^2
7x16
4
= number_tot

and now

char buf[256]
sprintf(buf, "%s", number_tot);

is it correct?

Is there a better way?
tnx
From your description you appear to have a string representing a
number in hexadecimal notation and want to convert it to a string
representing the same number in decimal notation. So for an input of
"1AB" you want an output of "427" since 0x1FF = 427.

One way is using stringstreams:

#include <iostream>
#include <cstdlib>
#include <string>
#include <sstream>

std::string hex2DecString(std::string hexStr) {
std::stringstream ss;

// Read hexadecimal number into stream
if (!(ss << std::hex << hexStr)) {
throw "hex2DecString: Conversion 1 error.";
}

// Read integer from stream
int intVal;
if (!(ss >intVal)) {
throw "hex2DecString: Conversion 2 error.";
}

// Read integer back into stream as decimal
ss.clear(); // **See note below**
ss.str("");
if (!(ss << std::dec << intVal)) {
throw "hex2DecString: Conversion 3 error.";
}

// Extract decimal string of number
return ss.str();
}

int main() {
std::string hexStr = "1AB";
std::string decStr = hex2DecString(hexStr);
std::cout << "Hex = " << hexStr << "\tDecimal = " << decStr <<
std::endl;
return EXIT_SUCCESS;
}

I am not sure why ss.clear() is needed, if it is not present then the
Conversion 3 error throws.

rossum
Dec 14 '06 #7
Salvatore Di Fazio :
Tobias Gneist ha scritto:
>#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>

using namespace std;

int main()
{
string val("54657374");
stringstream ss(val);
long l;
ss >hex >l;
cout << "val ='" << val
<< "', l = " << l
<< ", hex(l) ='" << hex << l << "'"
<< endl;
return 0;
}

Hi Tobias,
I tried but in this way I don't get the ascii string value but a
number.
Tnx
You can use ss << hex << l to get the hex string of the value l .
I think !
Dec 14 '06 #8
Okkey,
I think I've explained in a wrong way my problem my "problem". :)

I receive a hex string that means a ascii string that conteins number
and char.

So I need just to convert this hex string to a ascii char.

Example

Hex string in: 0x5465737449
Converted string: TEST1

I've googled but I've not found something.
Tomorrow I will have time to try to make it to myself.

Anyway thank to all :)

Dec 14 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by wkodie | last post: by
4 posts views Thread by Dennis Myr閚 | last post: by
4 posts views Thread by http://www.visual-basic-data-mining.net/forum | last post: by
1 post views Thread by olsongt | last post: by
18 posts views Thread by John | last post: by
1 post views Thread by Korara | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.