Jump to content
  • Advertisement
Sign in to follow this  
Psychopathetica

Hexadecimals

This topic is 2083 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
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)'.

Share this post


Link to post
Share on other sites

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;
}
Edited by Psychopathetica

Share this post


Link to post
Share on other sites
Psychopathetica, it crash because Text is an array of std::string when it should have been an array of char.

Share this post


Link to post
Share on other sites

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.

Edited by Psychopathetica

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!