y3ss

Hi All,

Anybody can help me on converting an int or byte data to Hex in C++ I'm using VC++ 2005. What I'm doing is, i readbyte() from Serial Port, then i need to convert the read data (0-255) to Hex data (00-FF). I'm new to VC++. Really appreciate you guys help and many thanks in advance.

Thanks

THQ



Re: Visual C++ Language How to convert Byte/Int data to Hex

Ramkrishna Pawar

What do you mean by convert to hex, the data read from any location is always in HEX, it is us who see it as decimal (usually using printf etc.)...

So is your requirement to just see the data in hex on screen






Re: Visual C++ Language How to convert Byte/Int data to Hex

y3ss

Hi Ramkrishna,

You are right, the data we see will always be in dec, i would like to convert them to Hex, of course in string datatype. For eg, i read from Serial Port a data of 255, but i want to show this data is FF. Any idea Thanks for your reply.

Regards,

y3ss - THQ





Re: Visual C++ Language How to convert Byte/Int data to Hex

crescens2k

Well, this just depends on what you want to use to output. For example, if you are using printf then you use the %x format, if you look through the documentation then you will find these.

But what was said is true, the data in the computer is represented by hexadecimal. We convert it to decimal output because it is easier for us.






Re: Visual C++ Language How to convert Byte/Int data to Hex

y3ss

Well, I want to display it in the textbox, i'm doing window application. I use a readonly textbox to display (in hex) all the data read from serial port. Previously i did it in VB it was much easier, there is a Hex function to convert dec 0-255 to Hex. But i can't find any such function in C++.



Re: Visual C++ Language How to convert Byte/Int data to Hex

einaros

Have a look at http://einaros.blogspot.com/2006/11/put-hex-on-that-dump.html. While I don't think that's quite right for you, you may pick up on what you need to do to display hex encoded data.






Re: Visual C++ Language How to convert Byte/Int data to Hex

Ohadh

you can use this sample:

void MyClass::Hex_To_Char( char *in, char *out )

{

char sTmp[3]={'0'};

for(int i=0;i<256;i++)

{

sTmp[0] = in[i*2];

sTmp[1] = in[i*2+1];

sTmp[2] = '\0';

out[i] = (unsigned char)strtol(sTmp, NULL, 16);

}

}

Hope it is useful for you..





Re: Visual C++ Language How to convert Byte/Int data to Hex

einaros

Rather than leaving memory management up to the caller, I think it'd be wise to use the STL stream and string classes, such as

Code Snippet

#include <algorithm>

#include <string>

#include <sstream>

#include <iostream>

#include <iomanip>

using namespace std;

class StrToHex

{

public:

StrToHex(ostringstream& strOutput) : m_strOutput(strOutput) {}

void operator()(char ch)

{

m_strOutput << "0x" << hex << setw(2) << setfill('0') << static_cast<unsigned int>(ch) << " ";

}

private:

ostringstream& m_strOutput;

};

string convertToHex(const string& szInput)

{

ostringstream oss;

for_each(szInput.begin(), szInput.end(), StrToHex(oss));

return oss.str();

}

int main()

{

cout << convertToHex("my test string") << endl;

return 0;

}






Re: Visual C++ Language How to convert Byte/Int data to Hex

y3ss

Hi Einaros,

I'm getting confused with the code you gave. I need only to convert a byte in dec eg 255 to FF. Any idea Appreciate your help.

y3ss-THQ





Re: Visual C++ Language How to convert Byte/Int data to Hex

einaros

With streams (and roughly the same headers as my previous example), that would be:

Code Snippet

std::ostringstream oss;

oss << std::hex << someByte;



The hex output will then be available as a std:Tongue Tiedtring through oss.str();





Re: Visual C++ Language How to convert Byte/Int data to Hex

y3ss

Hi Einaros,

That is for console application right which i can do it by cout<<hex<<somebyte;

How if i want to display in textbox or assign to lets call dataHex which we declare as string

Thanks,

y3ss-THQ





Re: Visual C++ Language How to convert Byte/Int data to Hex

einaros

No, that's not specifically for a console program. The ostringstream exposes a string through its .str() member function. If you want to retrieve a character buffer for that, you could call oss.str().c_str(). That should make it fairly simple to display the text in a box.





Re: Visual C++ Language How to convert Byte/Int data to Hex

mrjimorg

I prefer:

printf("values = %x %x %x", 69, 100, 111);
values = 45 64 6f
or
printf("values = 0x%x 0x%x 0x%x", 69, 100, 111);
values = 0x45 0x64 0x6f
or
printf("values = 0x%X 0x%X 0x%X", 69, 100, 111);
values = 0x45 0x64 0x6F