Integer to Hex string? - Mac Programming

Hi, Are there any functions to convert an integer in to a Hex string (any string type)? I know about using the std 'hex' routine to cout to the screen, but I need to store a string. Thanks Steve...

1. Integer to Hex string?

Hi,

Are there any functions to convert an integer in to a Hex string (any
string type)?

I know about using the std 'hex' routine to cout to the screen, but I
need to store a string.

Thanks

Steve

Steve Guest

2. Re: Integer to Hex string?

In article <bqs6lt\$ph8\$btinternet.com>,
Steve James <com> wrote:

The snprintf() function ('man snprintf') has more options than you will
ever need to do exactly that.
Michael Guest

3. Re: Integer to Hex string?

Thanks, of all the option, sprintf will suffice for now.
For anyone searching on this:

char *str = new char[8];
sprintf(str, "%lX", (unsigned long)(theNum));

(though as Michael pointed out, there's tons of options)

Michael Ash wrote:
>
>
> The snprintf() function ('man snprintf') has more options than you will
> ever need to do exactly that.[/ref]

Steve Guest

4. Re: Integer to Hex string?

In article <bqsip6\$ovn\$btinternet.com>,
Steve James <com> wrote:

You should really use snprintf instead of sprintf. sprintf isn't safe
unless you can be absolutely sure of the maximum length of the string,
because it will happily write past the end of your buffer and into
whatever's on the other end. It's ok in this case, but it's better to
get into the habit of using snprintf everywhere to avoid problems when
you can't know the maximum length.
Michael Guest

5. Re: Integer to Hex string?

Good point. A lot of code is cut/pasted from other old bits, so best

Michael Ash wrote:
>
>
> You should really use snprintf instead of sprintf. sprintf isn't safe
> unless you can be absolutely sure of the maximum length of the string,
> because it will happily write past the end of your buffer and into
> whatever's on the other end. It's ok in this case, but it's better to
> get into the habit of using snprintf everywhere to avoid problems when
> you can't know the maximum length.[/ref]

Steve Guest

6. Re: Integer to Hex string?

In article <bqs6lt\$ph8\$btinternet.com>,
Steve James <com> wrote:

The iostreams library allows you to use the std 'hex' routine to output
to a memory buffer. Simply use a stringstream as output stream instead
of std::cout:

#include <iostream>
#include <iomanip>
#include <sstream>

//(use "using namespace std" if the 'std::' prefixes annoy you)

int main( int argc, char *argv[])
{
std::ostringstream os;
os << std::hex << std::setw( 2) << 19;
std::string hexString = os.str();
std::cout << hexString << std::endl;
return EXIT_SUCCESS;
}

Reinder
Reinder Guest

7. Re: Integer to Hex string?

Thanks very much for that snippet. I'd been having trouble understanding
the bridge between std:cout and memory.

Steve

Reinder Verlinde wrote:
>
> The iostreams library allows you to use the std 'hex' routine to output
> to a memory buffer. Simply use a stringstream as output stream instead
> of std::cout:
>
> #include <iostream>
> #include <iomanip>
> #include <sstream>
>
> //(use "using namespace std" if the 'std::' prefixes annoy you)
>
> int main( int argc, char *argv[])
> {
> std::ostringstream os;
> os << std::hex << std::setw( 2) << 19;
> std::string hexString = os.str();
> std::cout << hexString << std::endl;
> return EXIT_SUCCESS;
> }
>
> Reinder[/ref]

Steve Guest

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•