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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. is this conversion from a char buf to a wchar correct?

is this conversion from a char buf to a wchar correct?

Scheduled Pinned Locked Moved C / C++ / MFC
questioncode-review
10 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.
  • G Offline
    G Offline
    gabbana
    wrote on last edited by
    #1

    hi, i want to ask a simple question related to my code here. I am using a MessageBox to show the user the estimated time of the battery. The start value is an ULONG which i put into my function as float seconds. The cond param is the returnstring of my function. Is the conversion (because the wsprintf function does not understand the %f param for formatting) right ? And what could i improve ? Thanks !

    void gettimestring(float seconds,wchar_t *input)
    {

    float minutes,hours;
    hours = floorf(seconds/3600);
    seconds -= hours\*3600;
    minutes = floorf(seconds/60);
    seconds -= minutes\*60;
    
    char buf\[20\];
    
    sprintf(buf,"%02.0f:%02.0f:%02.0f",hours,minutes,seconds);
    wsprintf(input,L"%S",buf);
    

    }

    bye, gabbana

    V CPalliniC T 3 Replies Last reply
    0
    • G gabbana

      hi, i want to ask a simple question related to my code here. I am using a MessageBox to show the user the estimated time of the battery. The start value is an ULONG which i put into my function as float seconds. The cond param is the returnstring of my function. Is the conversion (because the wsprintf function does not understand the %f param for formatting) right ? And what could i improve ? Thanks !

      void gettimestring(float seconds,wchar_t *input)
      {

      float minutes,hours;
      hours = floorf(seconds/3600);
      seconds -= hours\*3600;
      minutes = floorf(seconds/60);
      seconds -= minutes\*60;
      
      char buf\[20\];
      
      sprintf(buf,"%02.0f:%02.0f:%02.0f",hours,minutes,seconds);
      wsprintf(input,L"%S",buf);
      

      }

      bye, gabbana

      V Offline
      V Offline
      Varghese Paul M
      wrote on last edited by
      #2

      Try Win32 API "MultiByteToWideChar" For example

      int nSize = MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,buf,strlen(buf),input,sizeof(input));
      input[nSize] = 0;

      G 1 Reply Last reply
      0
      • G gabbana

        hi, i want to ask a simple question related to my code here. I am using a MessageBox to show the user the estimated time of the battery. The start value is an ULONG which i put into my function as float seconds. The cond param is the returnstring of my function. Is the conversion (because the wsprintf function does not understand the %f param for formatting) right ? And what could i improve ? Thanks !

        void gettimestring(float seconds,wchar_t *input)
        {

        float minutes,hours;
        hours = floorf(seconds/3600);
        seconds -= hours\*3600;
        minutes = floorf(seconds/60);
        seconds -= minutes\*60;
        
        char buf\[20\];
        
        sprintf(buf,"%02.0f:%02.0f:%02.0f",hours,minutes,seconds);
        wsprintf(input,L"%S",buf);
        

        }

        bye, gabbana

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        Why are you using floats for such a thing?

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        In testa che avete, signor di Ceprano?

        G 1 Reply Last reply
        0
        • CPalliniC CPallini

          Why are you using floats for such a thing?

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          G Offline
          G Offline
          gabbana
          wrote on last edited by
          #4

          The calcucaltion of the hours have decimal numbers. So I think i need floats at minimum or what do you mean exactly ?

          CPalliniC D 2 Replies Last reply
          0
          • V Varghese Paul M

            Try Win32 API "MultiByteToWideChar" For example

            int nSize = MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,buf,strlen(buf),input,sizeof(input));
            input[nSize] = 0;

            G Offline
            G Offline
            gabbana
            wrote on last edited by
            #5

            Okay thanks i will try it, i think i need first to look into the msdn what the params exactly mean.

            1 Reply Last reply
            0
            • G gabbana

              The calcucaltion of the hours have decimal numbers. So I think i need floats at minimum or what do you mean exactly ?

              CPalliniC Offline
              CPalliniC Offline
              CPallini
              wrote on last edited by
              #6

              I mean, for instance

              7305 seconds = 2 hours, 1 minute, 42 seconds

              for such a conversion floats are not needed:

              int iTotSecs = 7305;
              int iHours, iMins, iSecs;
              iHours = iTotSecs / 3600;
              iMins = (iTotSecs % 3600) / 60;
              iSecs = iTotSecs % 60;

              :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              In testa che avete, signor di Ceprano?

              G 1 Reply Last reply
              0
              • CPalliniC CPallini

                I mean, for instance

                7305 seconds = 2 hours, 1 minute, 42 seconds

                for such a conversion floats are not needed:

                int iTotSecs = 7305;
                int iHours, iMins, iSecs;
                iHours = iTotSecs / 3600;
                iMins = (iTotSecs % 3600) / 60;
                iSecs = iTotSecs % 60;

                :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                G Offline
                G Offline
                gabbana
                wrote on last edited by
                #7

                ohh yes that's right, but if i could remember the compiler put out some warnings and this was it i want to avoid.

                CPalliniC 1 Reply Last reply
                0
                • G gabbana

                  The calcucaltion of the hours have decimal numbers. So I think i need floats at minimum or what do you mean exactly ?

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

                  gabbana wrote:

                  The calcucaltion of the hours have decimal numbers.

                  So then use a double.

                  "Love people and use things, not love things and use people." - Unknown

                  "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                  1 Reply Last reply
                  0
                  • G gabbana

                    ohh yes that's right, but if i could remember the compiler put out some warnings and this was it i want to avoid.

                    CPalliniC Offline
                    CPalliniC Offline
                    CPallini
                    wrote on last edited by
                    #9

                    To eliminate all the warnings a simple cast on the original variable is enough. :)

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                    [My articles]

                    In testa che avete, signor di Ceprano?

                    1 Reply Last reply
                    0
                    • G gabbana

                      hi, i want to ask a simple question related to my code here. I am using a MessageBox to show the user the estimated time of the battery. The start value is an ULONG which i put into my function as float seconds. The cond param is the returnstring of my function. Is the conversion (because the wsprintf function does not understand the %f param for formatting) right ? And what could i improve ? Thanks !

                      void gettimestring(float seconds,wchar_t *input)
                      {

                      float minutes,hours;
                      hours = floorf(seconds/3600);
                      seconds -= hours\*3600;
                      minutes = floorf(seconds/60);
                      seconds -= minutes\*60;
                      
                      char buf\[20\];
                      
                      sprintf(buf,"%02.0f:%02.0f:%02.0f",hours,minutes,seconds);
                      wsprintf(input,L"%S",buf);
                      

                      }

                      bye, gabbana

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

                      gabbana wrote:

                      char buf[20]; sprintf(buf,"%02.0f:%02.0f:%02.0f",hours,minutes,seconds); wsprintf(input,L"%S",buf);

                      Instead of using char memory to variable buf, why don't you simply allocate the memory to wchar!

                      "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
                      Never mind - my own stupidity is the source of every "problem" - Mixture

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

                      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