Unix Programming - Decimal to Hex in g++

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > February 2004 > Decimal to Hex in g++





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Decimal to Hex in g++
Josh Parker

2004-02-23, 7:34 am

How do i convert a Decimal number to Hex in g++, I know there is not
an itoa function so what is the best way to do this?

would it be:

char *hexstring[10];
int number = 30;
sprintf(hexstring, "%x", number);

I will be passing this number to another program which will change the
hex to binary. Any help would really be appreciated. Thank You
Jens.Toerring@physik.fu-berlin.de

2004-02-23, 7:34 am

Josh Parker <jcp1217@mail.ecu.edu> wrote:
> How do i convert a Decimal number to Hex in g++, I know there is not
> an itoa function so what is the best way to do this?


> would it be:


> char *hexstring[10];


Drop the '*' in front of hexstring, or you get an array of 10
uninitialized char pointers instead of an array with enough space
for 9 characters.

> int number = 30;
> sprintf(hexstring, "%x", number);


Otherwise that looks completely reasonable, at least as long as you're
on a typical 32-bit machine. To make sure it works with whatever size
an integer has on a machine you could use instead

char hexstring[ ( CHAR_BIT * sizeof( int ) ) / 4 + 2 ];

CHAR_BIT is the number of bits in the smallest unit on your machine
(a char per definitionem), which is typically 8, but could be any
other larger value in principle (don't forget to include <climits>
or <limits.h> when you're using it). The additional 2 is one for
the '\0' at the end of the string and another one for cases where
CHAR_BIT can't be divided by 4.
Regards, Jens
--
\ Jens Thoms Toerring ___ Jens.Toerring@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Jens.Toerring@physik.fu-berlin.de

2004-02-23, 8:34 am

Jens.Toerring@physik.fu-berlin.de wrote:[color=blue]
> Josh Parker <jcp1217@mail.ecu.edu> wrote:
[color=blue]

One thing I forgot: At least in C (don't know about C++ but I would guess
it's the same) the "%x" is only allowed for _unsigned_ ints.

Regards, Jens
--
\ Jens Thoms Toerring ___ Jens.Toerring@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Dragan Cvetkovic

2004-02-23, 8:34 am

Jens.Toerring@physik.fu-berlin.de writes:

> Jens.Toerring@physik.fu-berlin.de wrote:
>
>
> One thing I forgot: At least in C (don't know about C++ but I would guess
> it's the same) the "%x" is only allowed for _unsigned_ ints.
>


And better use snprintf() if your system has one instead of sprintf().

Bye, Dragan

--
Dragan Cvetkovic,

To be or not to be is true. G. Boole No it isn't. L. E. J. Brouwer

!!! Sender/From address is bogus. Use reply-to one !!!
Greg Martin

2004-02-24, 12:34 am

Josh Parker wrote:

> How do i convert a Decimal number to Hex in g++, I know there is not
> an itoa function so what is the best way to do this?
>
> would it be:
>
> char *hexstring[10];
> int number = 30;
> sprintf(hexstring, "%x", number);
>
> I will be passing this number to another program which will change the
> hex to binary. Any help would really be appreciated. Thank You

#include <iostream>
#include <sstream>

int main()
{
std::ostringstream ss;

int num = 30;

ss << std::hex << num;

std::cout << ss.str() << "\n";

return 0;
}

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com