Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Urgent Converting stuff

Urgent Converting stuff

Scheduled Pinned Locked Moved C / C++ / MFC
data-structureshelp
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    imanos
    wrote on last edited by
    #1

    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 :)

    _ S N V 4 Replies Last reply
    0
    • I imanos

      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 :)

      _ Offline
      _ Offline
      _AnsHUMAN_
      wrote on last edited by
      #2

      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_

      1 Reply Last reply
      0
      • I imanos

        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 :)

        S Offline
        S Offline
        Sam_c
        wrote on last edited by
        #3

        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 process void 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:

        1 Reply Last reply
        0
        • I imanos

          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 :)

          N Offline
          N Offline
          Nelek
          wrote on last edited by
          #4

          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?

          1 Reply Last reply
          0
          • I imanos

            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 :)

            V Offline
            V Offline
            Vikrant for VC
            wrote on last edited by
            #5

            FILE* vl_fp=fopen("Data.txt","w") fprintf(vl_fp,"%2X",stemp); fclose(vl_fp)

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups