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. conversion to _bstr_t - newbie

conversion to _bstr_t - newbie

Scheduled Pinned Locked Moved C / C++ / MFC
16 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.
  • T ThatsAlok

    antonaras wrote:

    integers and doubles to _bstr_t;

    //Integer  TCHAR szStr[100];  wsprintf(szStr,"%d",100);  _bstr_t bstText(szStr); //Float  TCHAR szStr[100];  wsprintf(szStr,"%f",100.10);  _bstr_t bstText(szStr);

    "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

    cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and You

    A Offline
    A Offline
    antonaras
    wrote on last edited by
    #5

    Thanks ThatsAlok I'm getting there is almost working how about doubles? Appreciate the help

    S T 2 Replies Last reply
    0
    • S Sarath C

      ThatsAlok wrote:

      TCHAR szStr[100]; wsprintf(szStr,"%d",100);

      I think type defined by TCHAR has dependency to the current build environement(unicode/ MBCS). The compiler will define char array instead of unicode character array (wchar_t)in an MBCS environment. so there is a chance for error. I think, when it is better to declare wchar_t array instead of declaring TCHAR array since we are using wsprintf for formatting. what do u think? SaRath.
      "Don't Do Different things... Do Things Differently..."

      T Offline
      T Offline
      ThatsAlok
      wrote on last edited by
      #6

      SaRath C wrote:

      I think type defined by TCHAR has dependency to the current build environement(unicode/ MBCS). The compiler will define char array instead of unicode character array (wchar_t)in an MBCS environment. so there is a chance for error.

      Actually TCHAR is defined as :-

      #ifdef UNICODE
       #define TCHAR wchar_t
       #else
       #define TCHAR char
       #endif

      so you can use in both Character set. plus little mistake from my side too, wsprintf should be written something like this to make it character set independent

      wsprintf(szChar,_T("%d"),100);

      "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

      cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and You

      1 Reply Last reply
      0
      • S Sarath C

        _bstr_t is a type which can hold unicode string. CString strText; // format the value to CString strText.Format("%d",nValue); call strText.AllocSysString(); to convert SaRath.
        "Don't Do Different things... Do Things Differently..."

        T Offline
        T Offline
        ThatsAlok
        wrote on last edited by
        #7

        SaRath C wrote:

        _bstr_t is a type which can hold unicode string.

        you getting very fast :)

        "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

        cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and You

        1 Reply Last reply
        0
        • A antonaras

          Thanks ThatsAlok I'm getting there is almost working how about doubles? Appreciate the help

          S Offline
          S Offline
          Sarath C
          wrote on last edited by
          #8

          You will get the expected string depends on the format specifier you are specifying http://msdn2.microsoft.com/en-us/library/75w45ekt.aspx[^] SaRath.
          "Don't Do Different things... Do Things Differently..."

          1 Reply Last reply
          0
          • A antonaras

            Thanks ThatsAlok I'm getting there is almost working how about doubles? Appreciate the help

            T Offline
            T Offline
            ThatsAlok
            wrote on last edited by
            #9

            antonaras wrote:

            how about doubles?

            use %e format

            "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

            cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Re

            A 1 Reply Last reply
            0
            • S Sarath C

              _bstr_t is a type which can hold unicode string. CString strText; // format the value to CString strText.Format("%d",nValue); call strText.AllocSysString(); to convert SaRath.
              "Don't Do Different things... Do Things Differently..."

              T Offline
              T Offline
              ThatsAlok
              wrote on last edited by
              #10

              SaRath C wrote:

              call strText.AllocSysString(); to convert

              there can Memory Leak problem here!

              "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

              cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and You

              1 Reply Last reply
              0
              • T ThatsAlok

                antonaras wrote:

                how about doubles?

                use %e format

                "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Re

                A Offline
                A Offline
                antonaras
                wrote on last edited by
                #11

                Hey ThatsAlok i tried your code and it works fine with integers but i have double valField4; TCHAR szStr2[100]; wsprintf(szStr2,"%e",valField4); _bstr_t bstText2(szStr2); printf("%s\n",(LPCSTR)bstText2); the otput for bstText2 that i get is e why does that happen thanks again

                1 Reply Last reply
                0
                • A antonaras

                  Hi guys can i convert integers and doubles to _bstr_t; if yes can you tell me how thanks a lot

                  V Offline
                  V Offline
                  Viorel
                  wrote on last edited by
                  #12

                  If you are familiar with STL, you can use one more method:

                  int i = 1234567;
                  double d = 123.4567;
                  _bstr_t s1, s2;
                  
                  // convert integer
                  {
                      std::ostrstream os;
                      os << i << std::ends;
                      s1 = os.str();
                  }
                  // convert double
                  {
                      std::ostrstream os;
                      os << d << std::ends;
                      s2 = os.str();
                  }
                  
                  A 1 Reply Last reply
                  0
                  • V Viorel

                    If you are familiar with STL, you can use one more method:

                    int i = 1234567;
                    double d = 123.4567;
                    _bstr_t s1, s2;
                    
                    // convert integer
                    {
                        std::ostrstream os;
                        os << i << std::ends;
                        s1 = os.str();
                    }
                    // convert double
                    {
                        std::ostrstream os;
                        os << d << std::ends;
                        s2 = os.str();
                    }
                    
                    A Offline
                    A Offline
                    antonaras
                    wrote on last edited by
                    #13

                    Thanks Viorel i tried your code but i get lots of compilation errors 'ostrstream' : undeclared identifier syntax error : missing ';' before identifier 'os' 'os' : undeclared identifier and so on what i'm doing wrong i have included string is there anything else i need to include thanks

                    V 1 Reply Last reply
                    0
                    • A antonaras

                      Thanks Viorel i tried your code but i get lots of compilation errors 'ostrstream' : undeclared identifier syntax error : missing ';' before identifier 'os' 'os' : undeclared identifier and so on what i'm doing wrong i have included string is there anything else i need to include thanks

                      V Offline
                      V Offline
                      Viorel
                      wrote on last edited by
                      #14

                      In order to use these STL features, you have to include a header file:

                      #include <strstream>
                      

                      Next, you may receive an "unresolved external symbol" error displayed by the linker. In this case, you have to make one more addition: specify "comsuppwd.lib" in the "Linker --> Input --> Additional Dependencies" configuration option of your project. (In release compilation mode, specify "comsuppw.lib"). You can always see which header file or library is required by analyzing the descriptions in MSDN.

                      A 1 Reply Last reply
                      0
                      • V Viorel

                        In order to use these STL features, you have to include a header file:

                        #include <strstream>
                        

                        Next, you may receive an "unresolved external symbol" error displayed by the linker. In this case, you have to make one more addition: specify "comsuppwd.lib" in the "Linker --> Input --> Additional Dependencies" configuration option of your project. (In release compilation mode, specify "comsuppw.lib"). You can always see which header file or library is required by analyzing the descriptions in MSDN.

                        A Offline
                        A Offline
                        antonaras
                        wrote on last edited by
                        #15

                        thanks Viorel is working fine now thanks for all the help

                        1 Reply Last reply
                        0
                        • A antonaras

                          Hi guys can i convert integers and doubles to _bstr_t; if yes can you tell me how thanks a lot

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

                          antonaras wrote:

                          can i convert integers and doubles to _bstr_t;

                          Just assign them:

                          double d;
                          int x;
                          _bstr_t str;
                          str = d;
                          str = (long) x;


                          "The largest fire starts but with the smallest spark." - David Crow

                          "Judge not by the eye but by the heart." - Native American Proverb

                          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