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. Managed C++/CLI
  4. Unicode Strings and the L prefix

Unicode Strings and the L prefix

Scheduled Pinned Locked Moved Managed C++/CLI
c++question
6 Posts 3 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.
  • _ Offline
    _ Offline
    _Superman_
    wrote on last edited by
    #1

    String used in C++/CLI programs which are set to use Unicode character sets need not be prefixed with L. Is this an optional feature or is there something more to it? One thing that came to my notice is

    String^ fileName = "primary" + '.' + "extn"; // primary46extn

    and

    String^ fileName = "primary" + L'.' + "extn"; // primary.extn

    gave different results. So prefixing the dot with L, tells the compiler not to convert to integer.

    « Superman »

    C 1 Reply Last reply
    0
    • _ _Superman_

      String used in C++/CLI programs which are set to use Unicode character sets need not be prefixed with L. Is this an optional feature or is there something more to it? One thing that came to my notice is

      String^ fileName = "primary" + '.' + "extn"; // primary46extn

      and

      String^ fileName = "primary" + L'.' + "extn"; // primary.extn

      gave different results. So prefixing the dot with L, tells the compiler not to convert to integer.

      « Superman »

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Conversion from char to int is implicit in C++. In .NET, having a string and an int concatenated, automatically calls ToString on the int. so, C++ is turning your char to an int, and then .NET is trying to be helpful and turning that number into it's string representation.

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

      _ 1 Reply Last reply
      0
      • C Christian Graus

        Conversion from char to int is implicit in C++. In .NET, having a string and an int concatenated, automatically calls ToString on the int. so, C++ is turning your char to an int, and then .NET is trying to be helpful and turning that number into it's string representation.

        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

        _ Offline
        _ Offline
        _Superman_
        wrote on last edited by
        #3

        OK... Got that. For strings is the L prefix optional?

        « Superman »

        J 1 Reply Last reply
        0
        • _ _Superman_

          OK... Got that. For strings is the L prefix optional?

          « Superman »

          J Offline
          J Offline
          John R Shaw
          wrote on last edited by
          #4

          The ‘L’ prefix tells the complier to convert the [single byte] character sting into wide characters and is not optional, if the string type is expecting wide characters. Standard C++ examples: std::string = “char - string”; std::wstring = L”wchar_t – string”; // ‘L’ prefix required char* pStr = “char - string”; wchar_t* pStr = L”wchar_t – string”; // ‘L’ prefix required

          INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

          _ 1 Reply Last reply
          0
          • J John R Shaw

            The ‘L’ prefix tells the complier to convert the [single byte] character sting into wide characters and is not optional, if the string type is expecting wide characters. Standard C++ examples: std::string = “char - string”; std::wstring = L”wchar_t – string”; // ‘L’ prefix required char* pStr = “char - string”; wchar_t* pStr = L”wchar_t – string”; // ‘L’ prefix required

            INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

            _ Offline
            _ Offline
            _Superman_
            wrote on last edited by
            #5

            Thanks for the clarification. But I couldn't figure out how this works -

            MessageBox::Show("Contents", L"Title");

            The character set I'm using for the project is Unicode. You will notice that one parameter is with the 'L' prefix and the other is without it. So I'm guessing that the unicode version of Show is used and the 'L' prefix is optional.

            « Superman »

            J 1 Reply Last reply
            0
            • _ _Superman_

              Thanks for the clarification. But I couldn't figure out how this works -

              MessageBox::Show("Contents", L"Title");

              The character set I'm using for the project is Unicode. You will notice that one parameter is with the 'L' prefix and the other is without it. So I'm guessing that the unicode version of Show is used and the 'L' prefix is optional.

              « Superman »

              J Offline
              J Offline
              John R Shaw
              wrote on last edited by
              #6

              I have been away. From what little I have seen you appear to be using VC++ 2005, which I do not have. Therefore I can not test if this would even compile on that version of the compiler. The example you provided should generate errors, no matter what character set you are using. You can not mix Unicode and Multi-byte strings in a single function call, the function takes one or the other type, but not both. The only way this could work is if the compiler converted one of the strings, which it should not be doing, or the library provided functions that take mixed string argument types, which I have never seen before. In both these cases, MS would be doing something it should not be doing. Sorry I could not be of more help, all I know for sure is that the ‘L’ prefix is not optional and must be used to tell the compiler to convert the string to Unicode. Good Luck!

              INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

              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