Solved

char ptr to wstring - Urgent!

Posted on 2003-12-01
12
1,131 Views
Last Modified: 2013-12-14
Hi all,

the following code doesn't worK:

      wchar_t *WcString = new wchar_t[20];
      
      size_t Number = sizeof(m_pcRegName);
      size_t NumberAffected = mbstowcs( WcString, m_pcRegName, Number );
      wstring wsFilePath = WcString;

It will compile, but WcString won't have the desired value.  m_pcRegName points to "C:\", which is translated into "???????..." by the mbstowcs function. What am I doing wrong? Is there any easy way of doing this conversion?

cheers,

henrik
0
Comment
Question by:henrikatwork
  • 5
  • 4
  • 3
12 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 9850679
I'd make it read

    wchar_t *WcString = new wchar_t[strlen(m_pcRegName) + 1];
   
     size_t Number = strlen(m_pcRegName); // <---------------------!!!!!
    size_t NumberAffected = mbstowcs( WcString, m_pcRegName, Number );
    wstring wsFilePath = WcString;

because 'sizeof(char*)' == 4;
0
 
LVL 1

Author Comment

by:henrikatwork
ID: 9850814
Thanks,

it works much better, BUT my WcString has a bunch of question marks in it!! (That are not in the m_pcRegName)

How come??

cheers,

henrik
0
 
LVL 86

Expert Comment

by:jkr
ID: 9850836
How are you trying to display/use the wide string?

wcout << wsFilePath.c_str();

should yield a correct result.
0
 
LVL 1

Author Comment

by:henrikatwork
ID: 9850912
I'm doing an append:


      wsFileName.append( wsFilePath );

wsFileName.append( "myFile.file" );

which turns out to "C:\???myFile.file"
:-(((

0
 
LVL 1

Author Comment

by:henrikatwork
ID: 9850924
I've tried using C: (without the backslash) , but then I get 4 '?' (????) instead of 3
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 9850947
mbstowcs assumes that m_pcRegName points to a multi-byte string.

Try:
--------8<--------
#include <iostream>
#include <string>

int main()
{
      const char m_pcRegName[] = "Hello, World";
      std::wstring wstr(m_pcRegName,m_pcRegName+strlen(m_pcRegName));
      std::wcout << wstr << L'\n';
}
--------8<--------

This assumes that m_pcRegName points to an ordinary narrow character string.

Worth a try, perhaps.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 17

Expert Comment

by:rstaveley
ID: 9850986
Should you be doing...

  wsFileName.append(L"myFile.file");
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 9850993
Hmm, try

   size_t Number = strlen(m_pcRegName) + 1;
   wchar_t *WcString = new wchar_t[Number];
   size_t NumberAffected = mbstowcs( WcString, m_pcRegName, Number );

   wcout << WcString << endl;

   wstring wsFilePath = WcString;

   wcout << wsFilePath.c_str();

If it still does not work, give


   size_t Number = strlen(m_pcRegName) + 1;
   wchar_t *WcString = new wchar_t[Number];
   wsprintfW ( WcString, "%S", m_pcRegName);

a try.

0
 
LVL 1

Author Comment

by:henrikatwork
ID: 9851017
Sorry,

but please post stuff that compile.

cheers,

henrik


0
 
LVL 86

Expert Comment

by:jkr
ID: 9852746
>>but please post stuff that compile.

???
0
 
LVL 1

Author Comment

by:henrikatwork
ID: 9857013
sorry, that comment was for rstavely, after having posted the code

nt main()
{
     const char m_pcRegName[] = "Hello, World";
     std::wstring wstr(m_pcRegName,m_pcRegName+strlen(m_pcRegName));
     std::wcout << wstr << L'\n';
}
--------8<--------

which isn't even close to correct...

Thanks for your help jkr!!
henrik
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 9857232
The excerpt compiles and executes on GCC3.2 and VC7.1, if you cut and copy between the indicated scissor marks, but I'm glad jkr was able to get you sorted out OK. My line of thought was that m_pcRegName might really not have been a multi-byte string (the wstr constructor is an iterator constructor), but clearly I was wrong, wrong, wrong...
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now