473,397 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Conversion from/to String* to/from std::string

Hello,

I have the following methods I've written to convert
from String* to std::string and back:

static void ConvertFromStdString(std::string& inStr,
System:String* outStr)
{
outStr = new String(inStr.c_str());
}

static void ConvertToStdString(System::String* inStr,
std::string& outStr)
{
int len = inStr->Length();
char* charStr = __nogc new char[len + 1];
for (int i = 0; i < len; i++)
{
charStr[i] = (char)inStr->Chars[i];
}
charStr[len] = '\0';
outStr = charStr;
}

Can anyone tell me if there might be some hidden errors
in these functions (perhaps due to a mixing of managed
and unmanaged variables)?

TIA,

--
Akin

aknak at aksoto dot idps dot co dot uk
Nov 17 '05 #1
1 5029
Wild Wind wrote:

Hello,

I have the following methods I've written to convert
from String* to std::string and back:

static void ConvertFromStdString(std::string& inStr,
System:String* outStr)
{
outStr = new String(inStr.c_str());
}

static void ConvertToStdString(System::String* inStr,
std::string& outStr)
{
int len = inStr->Length();
char* charStr = __nogc new char[len + 1];
for (int i = 0; i < len; i++)
{
charStr[i] = (char)inStr->Chars[i];
}
charStr[len] = '\0';
outStr = charStr;
}

Can anyone tell me if there might be some hidden errors
in these functions (perhaps due to a mixing of managed
and unmanaged variables)?


The second fn will be pretty slow w/ the loop, and the cast to char is a
potential for somewhat undefined behavior for UNICODE characters (specifically,
chars above 8 bits).

You should take a look into Marshal::StringToHGlobalAnsi (and its counterpart
Marshal::FreeHGlobal) to get a copy of the string in one call, and assign that
to std::string.
Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Sims | last post by:
Hi, if i have a code const char * GetValue() { std::string szVectorValue = ...// get a std::string from the vector return szVectorValue.c_str(); }
24
by: Julie | last post by:
I'm re-evaluating the way that I convert from a std::string to char *. (Requirement: the source is a std::string, the usable contents are char *) Here is what I've come up with: #include...
3
by: Heiko Hund | last post by:
Hi, I do not understand the deeper reason for the following compiler error $ g++ test.cpp test.cpp: In function `int main()': test.cpp:41: error: `std::basic_string<char,...
5
by: Anton Pervukhin | last post by:
Hello! Imagine the situation you have a class that interprets the command line of the program you are executing. This class is written by third party and has a main function parse with arguments...
22
by: ineedyourluvin1 | last post by:
Hello all! I've been looking for a way to strip characters from strings such as a comma. This would be great for using a comma as a delimiter. I show you what I have right now. ...
1
by: gabriel.becedillas | last post by:
I have a lot of functions returning "const std::string&". Every time I wrap one of those I have to do it like this: class_.def("name", &someclass::bla,...
11
by: Jacek Dziedzic | last post by:
Hi! I need a routine like: std::string nth_word(const std::string &s, unsigned int n) { // return n-th word from the string, n is 0-based // if 's' contains too few words, return "" //...
5
by: rtrujillor | last post by:
int iAnno = 2005; int iMes = 7; int iDia = 23; std::stringstream ssAnno; ssAnno << iAnno; std::string anno = ssAnno.str(); std::stringstream ssMes; ssMes << iMes;
9
by: Eric | last post by:
I am working on a large, old code base and attempting to move it to GCC 4.2. Throughout the code, there is stuff like: char *aVar = "aString"; or void aFunc( char *aVar) { ... } aFunc(...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.