Urgent Converting stuff
-
please i was confused a little i have array of unsigned char where if i print its value by printf("%2X",stemp); the value is hexadecimal and it is the desired answer now i want to write this value into file with its hexa format how is that possible please urgently help me
emma :)
-
please i was confused a little i have array of unsigned char where if i print its value by printf("%2X",stemp); the value is hexadecimal and it is the desired answer now i want to write this value into file with its hexa format how is that possible please urgently help me
emma :)
You can use sprintf to change the hexadecimal value into a string format and then write it to a file just like any other string. There could be some other way too, but this is what struck me at this moment.
Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_
-
please i was confused a little i have array of unsigned char where if i print its value by printf("%2X",stemp); the value is hexadecimal and it is the desired answer now i want to write this value into file with its hexa format how is that possible please urgently help me
emma :)
i think sprintf will convert it, others these might be of use to you just dug them out.
void CharStr2HexStr(unsigned char const* pucCharStr, char* pszHexStr, int nSize) { char szHex[3]; pszHexStr[0] = 0; for(int i=0; i<nSize; i++) { Char2Hex(pucCharStr[i], szHex); strcat(pszHexStr, szHex); } } //unsigned char to str of len 2 void Char2Hex(unsigned char uch, char* szHex) { unsigned char byte[2]; byte[0] = uch/16; byte[1] = uch%16; for(int i=0; i<2; i++) { if(byte[i] >= 0 && byte[i] <= 9) szHex[i] = '0' + byte[i]; else szHex[i] = 'A' + byte[i] - 10; } szHex[2] = 0; }
reverse the processvoid HexStr2CharStr(char const* pszHexStr, unsigned char* pucCharStr, int nSize) { unsigned char uch; for(int i=0; i < nSize; i++) { Hex2Char(pszHexStr+2*i, uch); pucCharStr[i] = uch; } } //convert str of len 2 to unsigned char void Hex2Char(char const* szHex, unsigned char& uch) { uch = 0; for(int i=0; i<2; i++) { if(*(szHex + i) >='0' && *(szHex + i) <= '9') uch = (uch << 4) + (*(szHex + i) - '0'); else if(*(szHex + i) >='A' && *(szHex + i) <= 'F') uch = (uch << 4) + (*(szHex + i) - 'A' + 10); else break; } }
-- modified at 4:50 Wednesday 9th May, 2007 had some format problems :doh: -
please i was confused a little i have array of unsigned char where if i print its value by printf("%2X",stemp); the value is hexadecimal and it is the desired answer now i want to write this value into file with its hexa format how is that possible please urgently help me
emma :)
Hi, If you want to write it in a Text-File then you can use the answer above or another option. CString szHex = ""; szHex.Format (_T("%x"), yourVariable); //With little Hexadecimal letters szHex.Format (_T("%X"), yourVariable); //With capital hexadecimal letters and then send it to the file as all other strings. If you want to write it in a Binary-File, I used this in my project. This from my view:
//Creation of the file, with overwriting if name matches
CFile f (pcFileName, CFile::modeCreate | CFile::modeWrite);//Writing the PCode itself
obj.WriteHeader (&f);
dwActualPos = f.SeekToEnd ();This from the CObject-derived where WriteHeader is:
void obj::WriteHeader (CFile* file)
{ Header[0] = 0x23957112
Header[1] = 0x00001011;
Header[2] = CalculateCodeLarge ();
Header[3] = CalculateDataLarge ();
Header[4] = CalculateNumberOfElements ();
Header[5] = 0x00000000;
Header[6] = (DWORD) GetInputsCount ();
Header[7] = (DWORD) GetOutputsCount ();
Header[8] = 0x00010000;DWORD\* pHeadBuf; pHeadBuf = &Header\[0\]; file->Write (pHeadBuf, sizeof (Header)); return;
}
So you can send the hex directly.
-------- M.D.V. If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
-
please i was confused a little i have array of unsigned char where if i print its value by printf("%2X",stemp); the value is hexadecimal and it is the desired answer now i want to write this value into file with its hexa format how is that possible please urgently help me
emma :)
FILE* vl_fp=fopen("Data.txt","w") fprintf(vl_fp,"%2X",stemp); fclose(vl_fp)