Thread: best way use WCHAR,strings

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    69

    Question best way use WCHAR,strings

    Hi
    I want easier way of use WCHAR definition and strings
    I have lot of string data in structs,I want to convert to WCHAR or its just to use (WCHAR)string1 on all places it needs to be used?
    or do I need to rewrite code that defines strings
    define a word or sentence with WCHAR is annoying compared to strings
    I want/need to define WCHAR variables the old ways in my program,not the new way where every definition is using loadstring,loadicon from resources
    How do you know when you are a Guru?
    When you can (code) C even blindfolded

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,408
    wstring - C++ Reference would seem to be the thing to use for the bulk of your code.

    Only use the c_str() method, and whatever "wide-string-to-encoding" routines specific to your OS when you have to.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2019
    Posts
    209
    It would help to see some example of your current exact use case requirements (some code), but generally this is why you find code using typedefs (used to be defines long ago) instead of very basic types.

    For example, in LOTS of rendering, physics and game engine code (one my frequent haunts), the notion of floating point values is often typedef'd as Real, making Real a type which can then easily mutate between float, double or whatever else might come up later (sometimes there are short floats, 16 bit floating point values).

    In your case, you'd do something like that for your string definition, so only one major refactor ends up being required now, and you can choose as you prefer moving forward.

  4. #4
    Registered User
    Join Date
    Apr 2019
    Posts
    69
    Using namesake std::
    Solved many errors
    Still OS calls only work correctly with WCHAR
    Trying with strings show up strange Chinese characters,calling the non unicode version of call
    Try change use of strings to wstrings defining with constants and add strings to end
    Code:
    question="what is ";
    //later in code create question
    question=question+otherstring;
    which is my way of replacing couts with creating strings to send to GUI
    Last edited by I C everything; 05-31-2019 at 09:23 AM.
    How do you know when you are a Guru?
    When you can (code) C even blindfolded

  5. #5
    Registered User
    Join Date
    May 2019
    Posts
    209
    Still OS calls only work correctly with WCHAR
    You'll discover that depends on the OS. Some operating systems use 32 bit unicode strings, some 16 bit unicode strings, others may recognize UTF-8 (which is the defacto (if not formalized) default of the Internet, and due to the world's open source correction UTF-8 is actually the most correct and reliable).

    Put another way, GUI has it's own "thing" to deal with, and that often conflicts with application level goals, so we very often see a "GUI" translation of strings where the application uses what's best for data I/O (either with the Internet, SQL, data in files, etc.). The GUI is only one of several conflicting demands on the subject.

    Conversion is just an unfortunate constant coming at the developer from various perspectives.

    All of us lean back in the chair, sigh or cry, curse or close our eyes, then lean forward resigned that there's no good answer and there was never a coordinated answer to the problem, and all attempts to fix it have managed only to make it worse.

    Objects give rise to the most effective solutions I've yet seen.
    Last edited by Niccolo; 05-31-2019 at 11:09 AM. Reason: Appending

  6. #6
    Registered User
    Join Date
    Apr 2019
    Posts
    69
    seems the logic way of doing things with OS,is put lots of data in resource wchar format text file and loadstring and make a string search to locate the separate strings and copy them to a smaller WCHAR variables for output
    How do you know when you are a Guru?
    When you can (code) C even blindfolded

  7. #7
    Registered User
    Join Date
    Apr 2019
    Posts
    69
    anyone know a better way? this worked:
    Code:
    for (j = 0; j < (int)answer1.length(); j++) {		wtmp[j] = (WCHAR)answer1[j];
    	}
    How do you know when you are a Guru?
    When you can (code) C even blindfolded

  8. #8
    Registered User
    Join Date
    Apr 2019
    Posts
    69
    wouldnt a solution with an array of pointers,reading in string from file line by line and save beginnings of words and sentences in array of pointers be smoother
    and just use pointer 41 and pointer 42 in output calls?and see to it each line is stored with zero termination
    How do you know when you are a Guru?
    When you can (code) C even blindfolded

  9. #9
    Registered User
    Join Date
    May 2019
    Posts
    209
    This issue is long standing, and involves a great deal more than strings from a flat file. This happens getting text from SQL, the Internet, string literals in code...everywhere there's a difference between source and destination requirements.

    Consider these links to some solutions, one from the STL in C++

    std::codecvt - cppreference.com

    Microsoft on the topic

    https://msdn.microsoft.com/en-us/magazine/mt763237.aspx

  10. #10
    Registered User
    Join Date
    Apr 2019
    Posts
    69
    Thanks Niccolo
    How do you know when you are a Guru?
    When you can (code) C even blindfolded

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Zeroed WCHAR
    By gadu in forum C Programming
    Replies: 3
    Last Post: 06-23-2008, 09:41 PM
  2. strstr on a wchar array
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 06-28-2006, 06:42 AM
  3. WChar to char
    By MK4554 in forum C++ Programming
    Replies: 19
    Last Post: 04-20-2006, 12:14 AM
  4. C Datatype WCHAR
    By GUI_XP in forum C Programming
    Replies: 2
    Last Post: 11-29-2002, 06:24 PM
  5. char to WCHAR
    By pinkcheese in forum Windows Programming
    Replies: 5
    Last Post: 07-28-2002, 06:50 PM

Tags for this Thread