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. HextoCString conversion

HextoCString conversion

Scheduled Pinned Locked Moved C / C++ / MFC
11 Posts 7 Posters 2 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.
  • S Offline
    S Offline
    S p k 521
    wrote on last edited by
    #1

    Hi All, Please let me know some hints to convert a hex value to a string.. Regards, Spk521

    S L N 3 Replies Last reply
    0
    • S S p k 521

      Hi All, Please let me know some hints to convert a hex value to a string.. Regards, Spk521

      S Offline
      S Offline
      ShilpiP
      wrote on last edited by
      #2

      Click here->[^]

      I believe in LOVE AT FIRST SITE... Bcoz I have loved my Mother... even since I opened my eyes...(ICAN)

      1 Reply Last reply
      0
      • S S p k 521

        Hi All, Please let me know some hints to convert a hex value to a string.. Regards, Spk521

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        It depends what you mean by convert. Do you want the hex value to appear as hex in the string (e.g. 0x3739 --> "0x3739") or do you want to display it as characters (e.g. 0x3739 --> "79")?

        I must get a clever new signature for 2011.

        S 1 Reply Last reply
        0
        • L Lost User

          It depends what you mean by convert. Do you want the hex value to appear as hex in the string (e.g. 0x3739 --> "0x3739") or do you want to display it as characters (e.g. 0x3739 --> "79")?

          I must get a clever new signature for 2011.

          S Offline
          S Offline
          S p k 521
          wrote on last edited by
          #4

          ys.. i need to convert hex to its corresponding ascii value...

          S C D 3 Replies Last reply
          0
          • S S p k 521

            ys.. i need to convert hex to its corresponding ascii value...

            S Offline
            S Offline
            Satheesh1546
            wrote on last edited by
            #5

            Hi, Please try this.. char str[] = "01-15-43-43-34-37-37-41"; char delims[]="-"; CString strAscii = _T(""); CString strModelN0 = _T(""); char *result= NULL; result = strtok(str,delims); while(result != NULL) { result = strtok(NULL, delims); if(NULL != result) { strAscii.Format(_T("%c"),hexToAscii(result)); strModelN0 += strAscii; } } char hexToAscii(char *Num) { char hex[10]= "0x"; strcat(hex,Num); return strtol(hex, NULL, 16); } Regards, Spidy

            S L 2 Replies Last reply
            0
            • S Satheesh1546

              Hi, Please try this.. char str[] = "01-15-43-43-34-37-37-41"; char delims[]="-"; CString strAscii = _T(""); CString strModelN0 = _T(""); char *result= NULL; result = strtok(str,delims); while(result != NULL) { result = strtok(NULL, delims); if(NULL != result) { strAscii.Format(_T("%c"),hexToAscii(result)); strModelN0 += strAscii; } } char hexToAscii(char *Num) { char hex[10]= "0x"; strcat(hex,Num); return strtol(hex, NULL, 16); } Regards, Spidy

              S Offline
              S Offline
              S p k 521
              wrote on last edited by
              #6

              Thanks laj its working....

              1 Reply Last reply
              0
              • S S p k 521

                Hi All, Please let me know some hints to convert a hex value to a string.. Regards, Spk521

                N Offline
                N Offline
                Nuri Ismail
                wrote on last edited by
                #7

                You can do this easily using std::ostringstream[^]. Here is a small example for you:

                #include <iostream>
                #include <string>
                #include <iomanip>
                #include <sstream>

                int main()
                {
                // Test numbers - num1 declared as HEX, num2 as DEC
                const unsigned int num1 = 0x1234, num2 = 1234;

                // We will use std::ostringstream for the conversion
                std::ostringstream oss;
                
                // First num to Hex
                    // "0x" is optional if you don't need it you can use "oss << std::hex << num1;"
                oss << "0x" << std::hex << num1; 
                std::string strFirstNumHex = oss.str();
                    // Clear the content of the string buffer for later use
                oss.str(std::string());
                
                // Second num to Hex
                    // "0x" is optional if you don't need it you can use "oss << std::hex << num2;"
                oss << "0x" << std::hex << num2;
                std::string strSecondNumHex = oss.str();
                    // Clear the content of the string buffer for later use
                oss.str(std::string());
                
                // First num to Dec
                oss << std::dec << num1;
                std::string strFirstNumDec = oss.str();
                    // Clear the content of the string buffer for later use
                oss.str(std::string());
                
                // Second num to Dec
                oss << std::dec << num2;
                std::string strSecondNumDec = oss.str();
                
                // This line prints: Numbers in HEX: 0x1234, 0x4d2       Numbers in DEC: 4660, 1234
                std::cout << "Numbers in HEX: " << strFirstNumHex << ", " << strSecondNumHex << "\\t"
                          << "Numbers in DEC: " << strFirstNumDec << ", " << strSecondNumDec;
                
                std::cin.get();
                return 0;
                

                }

                As you can see from the above example strFirstNumHex and strSecondNumHex contain hex string representations of the test integers (strFirstNumHex = 0x1234 and strSecondNumHex = 0x4d2). strFirstNumDec and strSecondNumDec contain decimal string representations of the test integers. If you don't need the "0x" prefix for your Hex strings you can remove this prefix from specified lines in above example. I hope this helps. :) NOTE: For clearing the content of the stream buffer I usually use: oss.str("");, but it messes the formatting of the code block and I changed it to oss.str(s

                S 1 Reply Last reply
                0
                • S S p k 521

                  ys.. i need to convert hex to its corresponding ascii value...

                  C Offline
                  C Offline
                  Cool_Dev
                  wrote on last edited by
                  #8

                  in addition to his above example, if your hex is in some int type variable, you may try this also,

                  int k = 0x3739;
                  CString out;
                  while(k > 0) {
                  out += (char)k % 0x100;
                  k /= 0x100;
                  }
                  strrev(out.GetBuffer(0));
                  cout <<(LPCTSTR)out <

                  1 Reply Last reply
                  0
                  • S Satheesh1546

                    Hi, Please try this.. char str[] = "01-15-43-43-34-37-37-41"; char delims[]="-"; CString strAscii = _T(""); CString strModelN0 = _T(""); char *result= NULL; result = strtok(str,delims); while(result != NULL) { result = strtok(NULL, delims); if(NULL != result) { strAscii.Format(_T("%c"),hexToAscii(result)); strModelN0 += strAscii; } } char hexToAscii(char *Num) { char hex[10]= "0x"; strcat(hex,Num); return strtol(hex, NULL, 16); } Regards, Spidy

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    You missed the first number.

                    I must get a clever new signature for 2011.

                    1 Reply Last reply
                    0
                    • N Nuri Ismail

                      You can do this easily using std::ostringstream[^]. Here is a small example for you:

                      #include <iostream>
                      #include <string>
                      #include <iomanip>
                      #include <sstream>

                      int main()
                      {
                      // Test numbers - num1 declared as HEX, num2 as DEC
                      const unsigned int num1 = 0x1234, num2 = 1234;

                      // We will use std::ostringstream for the conversion
                      std::ostringstream oss;
                      
                      // First num to Hex
                          // "0x" is optional if you don't need it you can use "oss << std::hex << num1;"
                      oss << "0x" << std::hex << num1; 
                      std::string strFirstNumHex = oss.str();
                          // Clear the content of the string buffer for later use
                      oss.str(std::string());
                      
                      // Second num to Hex
                          // "0x" is optional if you don't need it you can use "oss << std::hex << num2;"
                      oss << "0x" << std::hex << num2;
                      std::string strSecondNumHex = oss.str();
                          // Clear the content of the string buffer for later use
                      oss.str(std::string());
                      
                      // First num to Dec
                      oss << std::dec << num1;
                      std::string strFirstNumDec = oss.str();
                          // Clear the content of the string buffer for later use
                      oss.str(std::string());
                      
                      // Second num to Dec
                      oss << std::dec << num2;
                      std::string strSecondNumDec = oss.str();
                      
                      // This line prints: Numbers in HEX: 0x1234, 0x4d2       Numbers in DEC: 4660, 1234
                      std::cout << "Numbers in HEX: " << strFirstNumHex << ", " << strSecondNumHex << "\\t"
                                << "Numbers in DEC: " << strFirstNumDec << ", " << strSecondNumDec;
                      
                      std::cin.get();
                      return 0;
                      

                      }

                      As you can see from the above example strFirstNumHex and strSecondNumHex contain hex string representations of the test integers (strFirstNumHex = 0x1234 and strSecondNumHex = 0x4d2). strFirstNumDec and strSecondNumDec contain decimal string representations of the test integers. If you don't need the "0x" prefix for your Hex strings you can remove this prefix from specified lines in above example. I hope this helps. :) NOTE: For clearing the content of the stream buffer I usually use: oss.str("");, but it messes the formatting of the code block and I changed it to oss.str(s

                      S Offline
                      S Offline
                      S p k 521
                      wrote on last edited by
                      #10

                      thank you very much experts...

                      1 Reply Last reply
                      0
                      • S S p k 521

                        ys.. i need to convert hex to its corresponding ascii value...

                        D Offline
                        D Offline
                        David Crow
                        wrote on last edited by
                        #11

                        S p k 521 wrote:

                        ...convert hex to its corresponding ascii value...

                        This makes no sense? Are you wanting to convert from base-16 to base-10?

                        "One man's wage rise is another man's price increase." - Harold Wilson

                        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                        "Man who follows car will be exhausted." - Confucius

                        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