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

const char* to hex char

2 views
Skip to first unread message

Philliam Auriemma

unread,
Feb 2, 2010, 12:11:54 AM2/2/10
to
Hey guys,

Say I have a const char* that equals "10". How would I go about
turning that into '\x10'? I've tried itoa and ssprintf or something,
and have been at it for an entire day but I can't figure it out.

Jerry Coffin

unread,
Feb 2, 2010, 12:42:35 AM2/2/10
to
In article <904e9ffb-95dd-4324-b789-cf0fd0e1b5f8
@d27g2000yqn.googlegroups.com>, phil.a...@gmail.com says...

This will convert the string to an unsigned long, assuming the input
is in hexadecimal:

unsigned long value = strtoul("10", NULL, 16);

Printing that value back out in hexadecimal with a leading '0x' looks
something like:

std::cout << std::hex << std::showbase << value << "\n";

--
Later,
Jerry.

Philliam Auriemma

unread,
Feb 2, 2010, 10:36:39 AM2/2/10
to
On Feb 1, 9:42 pm, Jerry Coffin <jerryvcof...@yahoo.com> wrote:
> In article <904e9ffb-95dd-4324-b789-cf0fd0e1b5f8
> @d27g2000yqn.googlegroups.com>, phil.aurie...@gmail.com says...

>
>
>
> > Hey guys,
>
> > Say I have a const char* that equals "10". How would I go about
> > turning that into '\x10'? I've tried itoa and ssprintf or something,
> > and have been at it for an entire day but I can't figure it out.
>
> This will convert the string to an unsigned long, assuming the input
> is in hexadecimal:
>
> unsigned long value = strtoul("10", NULL, 16);
>
> Printing that value back out in hexadecimal with a leading '0x' looks
> something like:
>
> std::cout << std::hex << std::showbase << value << "\n";
>
> --
>     Later,
>     Jerry.

WOW THANK YOU, this is a life saver. Perfect.

0 new messages