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. wcstombs is not working

wcstombs is not working

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
14 Posts 6 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.
  • N n1pabs

    Which character set are you trying to convert from? and a wchar is unicode char is ansi :)

    R Offline
    R Offline
    Rakesh5
    wrote on last edited by
    #3

    unicode characters.. i use chinese,japanese,arabic,french.. thanks, rakesh

    1 Reply Last reply
    0
    • R Rakesh5

      hi all, i am using wcstombs function to convert my const wchar* value to char*.... but its showing ?? marks instead of unicode characters.. i tried using widechartomultibyte like this... [(WideCharToMultiByte(CP_ACP, 0, Text, 0, Chartext, nSize, NULL, NULL); where int nSize =0;] but its also not converting the value.... can anyone help me where am i going wrong... Thanks, Rakesh.

      S Offline
      S Offline
      Souldrift
      wrote on last edited by
      #4

      Hm, just looking at your code line and reading this in the documentation "If cchWideChar is set to 0, the function fails." I´d say use -1 as your cchWideChar (fourth parameter). Souldrift

      R 1 Reply Last reply
      0
      • S Souldrift

        Hm, just looking at your code line and reading this in the documentation "If cchWideChar is set to 0, the function fails." I´d say use -1 as your cchWideChar (fourth parameter). Souldrift

        R Offline
        R Offline
        Rakesh5
        wrote on last edited by
        #5

        actually i tried with -1 first... it dint work..hence i tried with 0... Thanks, Rakesh

        S 1 Reply Last reply
        0
        • R Rakesh5

          actually i tried with -1 first... it dint work..hence i tried with 0... Thanks, Rakesh

          S Offline
          S Offline
          Souldrift
          wrote on last edited by
          #6

          Could you post your actual piece of code?

          R 1 Reply Last reply
          0
          • R Rakesh5

            hi all, i am using wcstombs function to convert my const wchar* value to char*.... but its showing ?? marks instead of unicode characters.. i tried using widechartomultibyte like this... [(WideCharToMultiByte(CP_ACP, 0, Text, 0, Chartext, nSize, NULL, NULL); where int nSize =0;] but its also not converting the value.... can anyone help me where am i going wrong... Thanks, Rakesh.

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

            If the wide char character cannot be represented in the choosen codepage (in you case CP_ACP, i.e. The system default Windows ANSI codepage), then it is replaced by a default one, see WideCharToMultiByte documentation [^]. :)

            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?

            N 1 Reply Last reply
            0
            • S Souldrift

              Could you post your actual piece of code?

              R Offline
              R Offline
              Rakesh5
              wrote on last edited by
              #8

              Hi, This is my piece of code.. const WCHAR* pText = "hello"; char * pCharText; WideCharToMultiByte(CP_ACP, 0, pText, -1,(LPSTR) pCharText, nSize, NULL, NULL); Thanks, Rakesh.

              S 1 Reply Last reply
              0
              • R Rakesh5

                Hi, This is my piece of code.. const WCHAR* pText = "hello"; char * pCharText; WideCharToMultiByte(CP_ACP, 0, pText, -1,(LPSTR) pCharText, nSize, NULL, NULL); Thanks, Rakesh.

                S Offline
                S Offline
                Souldrift
                wrote on last edited by
                #9

                That cannot be all your code. What´s nSize? And pCharText wasn´t initialized -> this should be a runtime error. Anyway, try this

                int erg=WideCharToMultiByte(CP_ACP, 0, pText, -1, NULL, 0, 0, 0); // first we ask for the memory needed
                char* result = new char[erg];
                erg=WideCharToMultiByte(CP_ACP, 0, pText, -1, result, erg, 0, 0); // then we convert

                Souldrift

                R 1 Reply Last reply
                0
                • S Souldrift

                  That cannot be all your code. What´s nSize? And pCharText wasn´t initialized -> this should be a runtime error. Anyway, try this

                  int erg=WideCharToMultiByte(CP_ACP, 0, pText, -1, NULL, 0, 0, 0); // first we ask for the memory needed
                  char* result = new char[erg];
                  erg=WideCharToMultiByte(CP_ACP, 0, pText, -1, result, erg, 0, 0); // then we convert

                  Souldrift

                  R Offline
                  R Offline
                  Rakesh5
                  wrote on last edited by
                  #10

                  Hi SoulDrift, I tried your code..its still showing the same "??" marks rather japanese texts.. please tell me where am going wrong.. am giving the code once again for your perusal.. const WCHAR* = L"sss"; int erg=WideCharToMultiByte(CP_ACP, 0, pText, -1, NULL, 0, 0, 0); // first we ask for the memory needed char* charText = new char[erg]; erg = WideCharToMultiByte(CP_ACP, 0, pText, -1, charText, erg, 0, 0); // then we convert Thanks, Rakesh

                  S 1 Reply Last reply
                  0
                  • R Rakesh5

                    Hi SoulDrift, I tried your code..its still showing the same "??" marks rather japanese texts.. please tell me where am going wrong.. am giving the code once again for your perusal.. const WCHAR* = L"sss"; int erg=WideCharToMultiByte(CP_ACP, 0, pText, -1, NULL, 0, 0, 0); // first we ask for the memory needed char* charText = new char[erg]; erg = WideCharToMultiByte(CP_ACP, 0, pText, -1, charText, erg, 0, 0); // then we convert Thanks, Rakesh

                    S Offline
                    S Offline
                    Souldrift
                    wrote on last edited by
                    #11

                    Your const WCHAR* doesn`t have a label. Does your compiler not have a problem with that? I tried your code

                    const WCHAR* pText = L"sss";

                    int erg=WideCharToMultiByte(CP_ACP, 0, pText, -1, NULL, 0, 0, 0); // first we ask for the memory needed
                    char* charText = new char[erg];
                    erg = WideCharToMultiByte(CP_ACP, 0, pText, -1, charText, erg, 0, 0); // then we convert

                    I works very nicely. Souldrift

                    1 Reply Last reply
                    0
                    • R Rakesh5

                      hi all, i am using wcstombs function to convert my const wchar* value to char*.... but its showing ?? marks instead of unicode characters.. i tried using widechartomultibyte like this... [(WideCharToMultiByte(CP_ACP, 0, Text, 0, Chartext, nSize, NULL, NULL); where int nSize =0;] but its also not converting the value.... can anyone help me where am i going wrong... Thanks, Rakesh.

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

                      Rakesh5 wrote:

                      ...but its showing ?? marks instead of unicode characters..

                      What is?

                      "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                      "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

                      1 Reply Last reply
                      0
                      • CPalliniC CPallini

                        If the wide char character cannot be represented in the choosen codepage (in you case CP_ACP, i.e. The system default Windows ANSI codepage), then it is replaced by a default one, see WideCharToMultiByte documentation [^]. :)

                        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]

                        N Offline
                        N Offline
                        n1pabs
                        wrote on last edited by
                        #13

                        I have to agree, if your developing on an American/english installed OS then CP_ACP will be Windows 1252, you want to change that to the Japanese/country specific page. ? is I remember right is 0x20, could be wrong. I wrote a little app to convert UNICODE to Multibyte supporting the codepages we need, and if the wrong code page was chosen the character would be displayed as ?? basically I replaced CP_ACP with either 1250 or 1251 or 1252 etc.. Seemed to do the trick.

                        1 Reply Last reply
                        0
                        • R Rakesh5

                          hi all, i am using wcstombs function to convert my const wchar* value to char*.... but its showing ?? marks instead of unicode characters.. i tried using widechartomultibyte like this... [(WideCharToMultiByte(CP_ACP, 0, Text, 0, Chartext, nSize, NULL, NULL); where int nSize =0;] but its also not converting the value.... can anyone help me where am i going wrong... Thanks, Rakesh.

                          N Offline
                          N Offline
                          Nemanja Trifunovic
                          wrote on last edited by
                          #14

                          First of all, using CP_ACP means you don't really have a control of the target code page - it depends on the users's settings. Sometimes it is exactly what you want, sometimes it is not. Anyway, if we assume that your system locale is Windows CP 1252 and you have some Greek characters in Text, your code will try to convert Greek characters to CP1252 and because there is no mapping between the two scripts, you are going to get replacement characters (?) instead. To convert Greek text from const wchar* value to char value to char*, you'll need to use CP1253 (not sure if I spelled the constant correctly) instead of CP_AP. I hope it make sense.

                          Programming Blog utf8-cpp

                          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