Jump to content
  • Advertisement
Sign in to follow this  
TheMatrixXXX

converting decimal to hex

This topic is 4816 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

Hi anyone, i have a little problem with converting a decimal to a hexadecimal number. i know how it works in theory. i´ve written some code and it doesn´t work and i have searched the net for some example, but i haven´t found something useful. (i´m at work at the moment so i don´t can post the code now) so my question: is there a function in c/c++ to convert a decimal to hex (i have an example with printf, but it didn´t work) or do you have some piece of code to do it? thanx in advance. greetz TheMatrixXXX

Share this post


Link to post
Share on other sites
Advertisement
Keep in mind that both "decimal" and "hex" are really display interpretations of the same underlying data: what's contained in memory is a sequence of bits (the number in binary), which won't be affected by "changing bases".

What you can do is tell the output stream to use hex rather than decimal when it encodes numeric values for output. This is done with a stream manipulator in C++, e.g.:


#include <iostream>
using namespace std;

int main() {
cout << hex << 123 << endl; // output: 7b
}



Here std::hex is the necessary stream manipulator.

In C, you can try playing with the %x or %p (IIRC) format specifiers for printf (and related functions).

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!