Convert UNICODE/ASCII problem

This is a discussion on Convert UNICODE/ASCII problem within the C++ Programming forums, part of the General Programming Boards category; Eh, I am trying to figure this out and nothing seems to work. I want to convert one simple string ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    10

    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));
    tmpStr.ReleaseBuffer(); 

    Code:
    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
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    '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
    Posts
    8,893
    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:
    Code:
    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 02:52 AM.
    All the buzzt!
    CornedBee

    "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, 07:57 AM
  2. Replies: 5
    Last Post: 11-07-2005, 10: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

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