Sign in to follow this  
TheMatrixXXX

converting decimal to hex

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this