Hexadecimals

Started by
18 comments, last by Trienco 10 years, 2 months ago

This may sound strange but I know how to display hexadecimals in C with the printf() using \x and in C++ using cout << hex << your_int_here, but what if I wanted to store that hexadecimal in a string? And I'm not talking about using the char data type but rather the string data type from the library #include <string>. Is it possible without having to design my own hex to string conversion function?

Advertisement
There is a built in class that mixes strings with streams. It has the hopefully unsurprising name of stringstream.

You can treat it like a stream, using the stream extraction and stream insertion methods. You can dump the content's with the .str() method.

There are quite a few useful string-based conversions you can do with it, including your example question. There are many lexical-style conversions that it can directly and indirectly perform, the Boost libraries use it as the basis for their lexical_cast and lexical_convert functionality. It is quite powerful.

Google can find many good tutorials on useful ways to use the class, such as this one.

In C, there is also itoa().

I just want to mention that itoa() is not a standard function. The standard way to do this in C99 is `snprintf(buffer, buffer_length, "%x", number)'.

But the thing is that all those methods still involve the console window. If you were wanting to save it to file, or a listbox, label, or messagebox, couldn't you use sprintf() to put the result in a string and display it using any of those?

[EDIT]

Seems like Im right:


#include <windows.h>
#include <string>
using namespace std;

int main()
{
	int number = 11;
	string Text[255];
	sprintf((char *)Text, "The hexadecimal value of number is: %x", number);
	MessageBox(NULL, (char *)Text, "typedef", MB_OK);

	return 0;
}

And although it works on VS 2012, I tried it on VS 2013, and I got a warning saying it might be unsafe, use sprintf_s instead. Very strange. And even then the console window crashes afterwards no matter which sprintf I use.

As an experiment I even tried not using a Messagebox, and displayed it in the console window. It works until I close the console window, then crashes. Its only if I comment out the sprintf_s that it doesnt crash:


#include <windows.h>
#include <string>
#include <iostream>
using namespace std;

int main()
{
	int number = 11;
	string Text[255];
	sprintf_s((char *)Text, sizeof(Text),"The hexadecimal value of number is: %x", number);
	//MessageBox(NULL, (char *)Text, "typedef", MB_OK);
	cout << (char *)Text << endl;
	system("pause");
	return 0;
}

But the thing is that all those methods still involve the console window.

Err, no! Each of the above mentioned

* stringstream

* itoa()

* snprintf() ((which is just a variant of sprintf() so that the amount of characters written can be limited))

use a string or char[] as target; none of them involve the console window (I assume you mean the implicit use of stdout / stderr here).

And although it works on VS 2012, I tried it on VS 2013, and I got a warning saying it might be unsafe, use sprintf_s instead. Very strange. And even then the console window crashes afterwards no matter which sprintf I use.

As an experiment I even tried not using a Messagebox, and displayed it in the console window. It works until I close the console window, then crashes. Its only if I comment out the sprintf_s that it doesnt crash:

You are creating an array of 255 string objects, and your call to printf overwrites the string objects with, as far as the string objects are concerned, garbage data. When the array is destroyed, some of the 255 the string objects have been trashed and teh destructor fails.

If you want to use the printf-family of functions, then either create an array of 255 characters, or one string object that contains room for 255 characters (and be sure to get a pointer to the internal string data, don't overwrite the string object). But 255 string objects that you overwrite with character data is totally wrong.

Psychopathetica, it crash because Text is an array of std::string when it should have been an array of char.

Setting char *Text instead of string Text[255] still crashes the console window. But this works at least:


#include <windows.h>
#include <string>
#include <iostream>
using namespace std;

int main()
{
	int number = 11;
	char Text[255];
	sprintf_s(Text, sizeof(Text), "The hexadecimal value of number is: %x", number);
	MessageBox(NULL, Text, "typedef", MB_OK);
	cout << (char *)Text << endl;
	system("pause");
	return 0;
}

However I still wanna be able to store the data in a string rather than a char.

Setting char *Text instead of string Text[255] still crashes the console window.

For the case that your console application does not load the correct libraries: Have you considered to use one of the suggested standardized functions (sprintf, snprintf) instead?

However I still wanna be able to store the data in a string rather than a char.

The very first answer above already told the solution. In case that your favorite search engine has a defect … something like the following should do (but is written down untested):


stringstream myHexString;
myHexString << hex << number;
string str = myHexString.str();

This topic is closed to new replies.

Advertisement