Thread: Convert UNICODE/ASCII problem

  1. #1
    Registered User
    Join Date
    Apr 2007

    Convert UNICODE/ASCII problem

    Eh, I am trying to figure this out and nothing seems to work. I want to convert one simple string to unicode.

    cstring doesn't work at all

    PHP Code:
    #include <string>
    using namespace std;

    char fileNameA[] = "D:\\sample.txt";
    DWORD Pos 10;
    std::string tmpStr "";
    wchar_tfileNameW tmpStr.GetBufferSetLength ((Pos+1)*sizeof(wchar_t));
    MultiByteToWideChar(CP_ACP0fileNameAstrlen(fileNameA), fileNameW, (Pos+1)*sizeof(wchar_t));

    D:\nHide\nHide.cpp(36) : error C2039: 'GetBufferSetLength' : is not a member of 'basic_string<char,struct std::char_traits<char>,class std::allocator<char> >'
    D:\nHide\nHide.cpp(38) : error C2039: 'ReleaseBuffer' : is not a member of 'basic_string<char,struct std::char_traits<char>,class std::allocator<char> >'

  2. #2
    Join Date
    Oct 2007
    Inside my computer
    'Cause std::string != CString.
    CString is a MFC string class, std::string is the C++ library string class.
    Doesn't work that way.
    IF you change tmpStr to CString, it works, but if you use std::string, you need to do it another way. Probably use a C-style buffer and then assigning it to the std::string string.
    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
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Use a std::vector<wchar_t>.

    Oh, and even if std::string's buffer was mutable, it's still a narrow character string. std::wstring is the wide version.

    Something like this:
    int narrowlen = static_cast<int>(strlen(fileNameA));
    int widelen = MultiByteToWideChar(CP_ACP, 0, fileNameA, narrowlen, 0, 0);
    if(widelen <= 0) {
      // Handle error.
    std::vector<wchar_t> buffer(widelen);
    MultiByteToWideChar(CP_ACP, 0, fileNameA, narrowlen, &buffer[0], widelen);
    Last edited by CornedBee; 01-24-2008 at 03:52 AM.
    All the buzzt!

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem in Converting Unsigned long to String
    By cprogrammer_18 in forum C Programming
    Replies: 8
    Last Post: 01-14-2006, 08:57 AM
  2. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  3. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM