Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Converting hex string to ASCII

0 views
Skip to first unread message

Xenzeo

unread,
Mar 15, 2004, 6:11:33 PM3/15/04
to
Hey all...

I'm trying to write a small program converting a hex string to ascii, I would like to know if there is a function like

cout << hex << 192;
// this prints out c0

that prints out the ascii chars of strings like:
char *hexToAscii = "48 45 4c 4c 4f 61 57 4f 52 4c 44";

I'm new at c++ programming, still in my learning process so please bear with me..

Any help would be appriciated, including links to resources..

regards

Xenzeo

Richard Heathfield

unread,
Mar 15, 2004, 6:38:10 PM3/15/04
to
Xenzeo wrote:

> Hey all...
>
> I'm trying to write a small program converting a hex string to ascii, I
> would like to know if there is a function like
>
> cout << hex << 192;
> // this prints out c0
>
> that prints out the ascii chars of strings like:
> char *hexToAscii = "48 45 4c 4c 4f 61 57 4f 52 4c 44";
>
> I'm new at c++ programming, still in my learning process so please bear
> with me..


If no C++ answer materialises, never forget that you could always do this:

char hexToAscii[] = "48 45 4c 4c 4f 61 57 4f 52 4c 44";
char *p = strtok(hexToAscii, " ");
while(p != NULL)
{
unsigned long n = strtoul(p, NULL, 16);
printf("%c", (int)n);
p = strtok(NULL, " ");
}


--
Richard Heathfield : bin...@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton

Xenzeo

unread,
Mar 15, 2004, 7:01:01 PM3/15/04
to
On Mon, 15 Mar 2004 23:38:10 +0000 (UTC)
Richard Heathfield <dont...@address.co.uk.invalid> wrote:
> If no C++ answer materialises, never forget that you could always do this:
>
> char hexToAscii[] = "48 45 4c 4c 4f 61 57 4f 52 4c 44";
> char *p = strtok(hexToAscii, " ");
> while(p != NULL)
> {
> unsigned long n = strtoul(p, NULL, 16);
> printf("%c", (int)n);
> p = strtok(NULL, " ");
> }
>

Thanks... ;-)

Mike Wahler

unread,
Mar 15, 2004, 7:03:25 PM3/15/04
to

"Xenzeo" <xenzeo-n...@blackhat.dk> wrote in message
news:20040316001133.2fbc08e0@xenzeo...

#include <algorithm>
#include <ios>
#include <iostream>
#include <string>

std::ostream& func(std::ostream& os, const std::string& s)
{
os.clear();
std::ios::fmtflags f(os.flags());
os << std::hex;
std::copy(s.begin(), s.end(), std::ostream_iterator<int>(os, " "));
os.flags(f);
return os;
}

int main()
{
func(std::cout, "Hello world");
std::cout.put('\n');
return 0;
}

-Mike


Xenzeo

unread,
Mar 15, 2004, 7:36:55 PM3/15/04
to
On Tue, 16 Mar 2004 00:03:25 GMT
"Mike Wahler" <mkwa...@mkwahler.net> wrote:
>
> #include <algorithm>
> #include <ios>
> #include <iostream>
> #include <string>
>
> std::ostream& func(std::ostream& os, const std::string& s)
> {
> os.clear();
> std::ios::fmtflags f(os.flags());
> os << std::hex;
> std::copy(s.begin(), s.end(), std::ostream_iterator<int>(os, " "));
> os.flags(f);
> return os;
> }
>
> int main()
> {
> func(std::cout, "Hello world");
> std::cout.put('\n');
> return 0;
> }
>

I'v got this compile error on this code:

translate2.c++: In function `std::ostream& func(std::ostream&, const
std::string&)':
translate2.c++:11: error: `ostream_iterator' undeclared in namespace `std'
translate2.c++:11: error: parse error before `>' token

I'm using g++ (GCC) 3.3.2 20031218 (Gentoo Linux 3.3.2-r5, propolice-3.3-7)


Another thing, doesnt this convert Ascii to Hex?? I need the oppesed.

-Xenzeo

Mike Wahler

unread,
Mar 15, 2004, 7:59:10 PM3/15/04
to

"Xenzeo" <xenzeo-n...@blackhat.dk> wrote in message
news:20040316013655.186f9ec7@xenzeo...

> On Tue, 16 Mar 2004 00:03:25 GMT
> "Mike Wahler" <mkwa...@mkwahler.net> wrote:
> >
> > #include <algorithm>
> > #include <ios>
> > #include <iostream>
> > #include <string>
> >
> > std::ostream& func(std::ostream& os, const std::string& s)
> > {
> > os.clear();
> > std::ios::fmtflags f(os.flags());
> > os << std::hex;
> > std::copy(s.begin(), s.end(), std::ostream_iterator<int>(os, " "));
> > os.flags(f);
> > return os;
> > }
> >
> > int main()
> > {
> > func(std::cout, "Hello world");
> > std::cout.put('\n');
> > return 0;
> > }
> >
>
> I'v got this compile error on this code:
>
> translate2.c++: In function `std::ostream& func(std::ostream&, const
> std::string&)':
> translate2.c++:11: error: `ostream_iterator' undeclared in namespace `std'
> translate2.c++:11: error: parse error before `>' token

I forgot to #include <iterator> Sorry.

>
> I'm using g++ (GCC) 3.3.2 20031218 (Gentoo Linux 3.3.2-r5,
propolice-3.3-7)
>
>
> Another thing, doesnt this convert Ascii to Hex?? I need the oppesed.

Sorry again. :-) I must have misunderstood your question.

#include <algorithm>
#include <ios>
#include <iostream>

#include <iterator>
#include <sstream>
#include <string>

std::ostream& func(std::ostream& os, const std::string& s)
{

std::istringstream iss(s);
iss >> std::hex;

std::copy(std::istream_iterator<int>(iss),
std::istream_iterator<int>(),
std::ostream_iterator<char>(os, ""));

return os;
}

int main()
{
func(std::cout, "48 45 4c 4c 4f 61 57 4f 52 4c 44");


std::cout.put('\n');
return 0;
}

-Mike


Xenzeo

unread,
Mar 15, 2004, 9:07:04 PM3/15/04
to
On Tue, 16 Mar 2004 00:59:10 GMT
"Mike Wahler" <mkwa...@mkwahler.net> wrote:

>
> I forgot to #include <iterator> Sorry.
>

np, i should have cought this my self..

>
> Sorry again. :-) I must have misunderstood your question.
>
> #include <algorithm>
> #include <ios>
> #include <iostream>
> #include <iterator>
> #include <sstream>
> #include <string>
>
> std::ostream& func(std::ostream& os, const std::string& s)
> {
> std::istringstream iss(s);
> iss >> std::hex;
>
> std::copy(std::istream_iterator<int>(iss),
> std::istream_iterator<int>(),
> std::ostream_iterator<char>(os, ""));
>
> return os;
> }
>
> int main()
> {
> func(std::cout, "48 45 4c 4c 4f 61 57 4f 52 4c 44");
> std::cout.put('\n');
> return 0;
> }
>

Thanks, mike..

54 68 61 6e 6b 73 20 61 67 61 69 6e 2c 20 6e 6f 77 20 69 27 76 20 67 6f 74 20 62 6f 74 68 20 70 72 6f 67 73 20 77 6f 72 6b 69 6e 67 2c 20 6d 75 63 68 20 61 70 70 72 69 63 69 61 74 65 64 ;-)

0 new messages