# converting decimal to hex

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

## 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 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 on other sites
sorry, i really didn´t thought that way, damn
i always thought that it was a stupid question ;)

thanx

greetz
TheMatrixXXX