char to unicode

This is a discussion on char to unicode within the Windows Programming forums, part of the Platform Specific Boards category; anyone know of a quick way to convert the data in a char array to a unicode array? I have ...

  1. #1
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189

    char to unicode

    anyone know of a quick way to convert the data in a char array to a unicode array? I have to support an existing application that will only pass the filename as a char array.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,620
    Sure. This should get you started:
    Code:
    	Strings::CStringExW AnsiToUTF16(const Strings::CStringExA& strFrom)
    	{
    		Strings::CStringExW strTo;
    		int nLen = MultiByteToWideChar(CP_ACP, 0, strFrom, -1, NULL, NULL);
    		MultiByteToWideChar(CP_ACP, 0, strFrom, -1, strTo.GetBuffer(nLen + 1), nLen);
    		strTo.ReleaseBuffer();
    		return strTo;
    	}
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,671
    If you aren't using MBCS (which you probably aren't) then just use a for loop and assign each character.

    The values for ASCII characters (0x00 - 0x7F) have the same values under Unicode.

    If you are using MBCS, or some ansi code page with "special" character symbols being used (>0x7F) then you can use mbstowcs() or MultiByteToWideChar().

    gg

  4. #4
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Thanks code, once again you are the man
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,671
    On a side note...

    >> MultiByteToWideChar(CP_ACP, 0, strFrom, -1, strTo.GetBuffer(nLen + 1), nLen);
    Check that return code!

    Is it safe to run the following with your string class?
    Code:
    strTo.GetBuffer(20);
    strTo.ReleaseBuffer();
    On a regular MFC CString, I don't think this is safe since the release will do some sort of strlen() on the buffer, which may of been freshly allocated and not null-terminated.

    Plus, who knows if MB2WC could bomb after writting characters but not the null-terminator.

    gg

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,620
    Yes, you're right. Much of the code lacks error handling and error checking.
    And you're right that it isn't safe since it emulates CString behavior.

    But thanks for the heads up. I'm going to look into the issue somewhat.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  2. Conversion Char To Char * Problem
    By ltanusaputra in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2008, 01:06 PM
  3. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 11:36 AM
  4. I'm having a problem with data files.
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 05-14-2003, 09:40 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21