473,383 Members | 1,798 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,383 software developers and data experts.

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 23596
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: wkodie | last post by:
I'm having trouble encrypting/decrypting a simple string using the System.Security.Cryptography.TripleDESCryptoServiceProvider, etc... The encryption works, but the decryption does not properly...
11
by: Kai Bohli | last post by:
Hi all ! I need to translate a string to Ascii and return a string again. The code below dosen't work for Ascii (Superset) codes above 127. Any help are greatly appreciated. protected...
4
by: Dennis Myrén | last post by:
Hi. Is there a way to utilize the great primitive data type formatting routines available in .NET without working with strings? I want a byte directly rather than a string. I think it is...
18
by: Ger | last post by:
I have not been able to find a simple, straight forward Unicode to ASCII string conversion function in VB.Net. Is that because such a function does not exists or do I overlook it? I found...
4
by: http://www.visual-basic-data-mining.net/forum | last post by:
Hi Does anyone know how to stay connected to the server and at the same time i can pass the string to and from the module to the form..... What I want: I put the connection at the...
1
by: olsongt | last post by:
I was going to submit to sourceforge, but my unicode skills are weak. I was trying to strip characters from a string that contained values outside of ASCII. I though I could just encode as 'ascii'...
18
by: John | last post by:
Hi, I'm a beginner is using C# and .net. I have big legacy files that stores various values (ints, bytes, strings) and want to read them into a C# programme so that I can store them in a...
2
by: John Nagle | last post by:
I'm trying to clean up a bad ASCII string, one read from a web page that is supposedly in the ASCII character set but has some characters above 127. And I get this: File...
13
by: Eps | last post by:
Hi there, I believe all strings in .net are unicode by default, I am looking for a way to remove all non ascii characters from a string (or optionally replace them). There is an article on...
19
by: est | last post by:
From python manual str( ) Return a string containing a nicely printable representation of an object. For strings, this returns the string itself. The difference with repr(object) is that...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.