Advertisement Jump to content
Sign in to follow this  

converting decimal to hex

This topic is 4976 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
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, you agree to our community Guidelines, Terms of Use, and Privacy Policy. 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!