Hex string to ascii

S

Salvatore Di Fazio

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
 
T

Tobias Gneist

Salvatore said:
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
 
S

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
 
M

Michal Nazarewicz

Salvatore Di Fazio said:
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-
 
R

rossum

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
 
B

Bo Yang

Salvatore Di Fazio :
Tobias Gneist ha scritto:


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 !
 
S

Salvatore Di Fazio

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 :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top