Share via


Function for converting a char to a hex representing string?

Question

Thursday, March 15, 2007 4:06 PM

Hi!

Do anyone know if C++ contains an in-built function which can convert a char to a CString (not C, but C++ string) which contain the two characters that represent the char in hex?

...or have anyone such a function available for sharing?

Any help would be appreciated.

Best Regards Bjørn Liene Gundersen, Norway.

 

All replies (5)

Thursday, March 15, 2007 9:25 PM ✅Answered

The easiest way is probably to use the STL streams, such as

#include <string>
#include <stringstream>
#include <iomanip>

char mychar = 'c';
std::ostringstream os;
os << std::hex << mychar;
std::string mystring = os.str();

I've got one example use of such in my blog, if you're interested. It can be found at http://einaros.blogspot.com/2006/11/put-hex-on-that-dump.html.

 

 


Sunday, March 25, 2007 8:06 PM ✅Answered

 

To convert a char byte to a hex representing CString you can do as followed;

unsigned char Table[20];

CString SomethingDlg::MakeStrHexOfChar( Table )
{
  char buffer[3];

 _itoa( Table, buffer, 16 );
 Temp = buffer;

 return buffer;

}

You have to include <stdlib.h>.

Table contain the bytes you want to view as hex, and you pick the byte with the [] operator. Then u can add as many MakeStrHexOfChar( Table )
CString you want with + operator.

Bjørn Liene Gundersen


Friday, March 16, 2007 1:02 PM

Hei, jeg er ikke så vant med slik "avansert klassebruk" og programmering og fikk ikke til eksempelet ditt, så jeg laget denne funksjonen, som jeg får kompileringsfeil på.

I headerfila har jeg da definert følgende globale variabler

 en unsigned char med kjent verdi som brukes som argument og strengen CString ASCI_NIB[] = "0123456789ABCDEF";

 

 

CString Char_Hex_CString(unsigned char convert);
{
 unsigned char byte = convert;
 
 unsigned char Lbyte;
 unsigned char Hbyte;

 Lbyte = (byte & 0x0F);
 Hbyte = (byte & 0xF0)>>4;

 CString Lowbyte = ASCI_NIB[Lbyte];
 CString Highbyte = ASCI_NIB[Hbyte];
 
 CString Hexbyte = Lowbyte + Highbyte;

 return Hexbyte;
}

Men globale variabler eller ikke, like forbasket får jeg opp disse feilmeldingene. Jeg trodde globale variabler løste problemet med pekere, men der tok jeg visst feil?

Compiling...
UsbHidDemoCodeDlg.cpp
C:\Documents and Settings\Bjørn L Gundersen\Skrivebord\USB HID generic PC software for AT90USB or AT89C513x proven min\Generic_HID_15_11_06\UsbHidDemoCode\UsbHidDemoCodeDlg.cpp(666) : error C2664: 'Char_Hex_CString' : cannot convert parameter 1 from
 'unsigned char' to 'unsigned char *'
        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
C:\Documents and Settings\Bjørn L Gundersen\Skrivebord\USB HID generic PC software for AT90USB or AT89C513x proven min\Generic_HID_15_11_06\UsbHidDemoCode\UsbHidDemoCodeDlg.cpp(805) : error C2065: 'ASCI_NIB' : undeclared identifier
C:\Documents and Settings\Bjørn L Gundersen\Skrivebord\USB HID generic PC software for AT90USB or AT89C513x proven min\Generic_HID_15_11_06\UsbHidDemoCode\UsbHidDemoCodeDlg.cpp(805) : error C2109: subscript requires array or pointer type
C:\Documents and Settings\Bjørn L Gundersen\Skrivebord\USB HID generic PC software for AT90USB or AT89C513x proven min\Generic_HID_15_11_06\UsbHidDemoCode\UsbHidDemoCodeDlg.cpp(806) : error C2109: subscript requires array or pointer type
Error executing cl.exe.

UsbHidDemoCodeDlg.obj - 4 error(s), 0 warning(s)

All hjelp er hjertelig mottatt.

mvh. Bjørn Liene Gundersen, hovedprosjektstudent Høgskolen i Sørtrøndelag.


Friday, March 16, 2007 1:38 PM

If you're more comfortable with norwegian, you should ask the same question at http://www.diskusjon.no. This forum should be reserved for the english language :)

That being said, I'm afraid that the function doesn't make all too much sense. If your goal is to convert a single ascii character into its hex representation, the typical approach would be to take the ascii character code of the char, convert it from decimal to hex, and then write it back as a string padded to two bytes.

If you could come up with a clear specification of what the input of your conversion should be (a single byte? an array? a string?), as well as the wanted output (formatting?), I can write up a quick example of how to do it. :)


Thursday, March 22, 2007 3:33 PM

if I have 97 00 00 00 00 00 00 00 00 00 00 00 00 00 as an array of byte or char how do I convert to "{00000097-0000-0000-0000-000000000000}" or "{0x97, 0x0, 0x0, 0x0}
thank you in advance