473,614 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

incorrect result when converting from basic string to System::String

Hi,
I am using a c# library in my c++ project. so I have to convert a
basic string to System::string to make use of a function that takes
System::String as argument. I did it like this:

System::String^ s = gcnew System::String( basic_s.c_str() ); //here
basic_s is a basic string in c++

But I found s contains less charactors than the original basic string,
some charactors are missing.
here, the basic string contians bytes that rang from -127 to 127,
which is the result of an encrypting function.

what's wrong with the converting? how to convert the string without
missing charactors?

Thanks!

Vivienne

Aug 20 '07 #1
1 1707
Vivienne <zh**********@g mail.comwrote:
I am using a c# library in my c++ project. so I have to convert a
basic string to System::string to make use of a function that takes
System::String as argument. I did it like this:

System::String^ s = gcnew System::String( basic_s.c_str() ); //here
basic_s is a basic string in c++

But I found s contains less charactors than the original basic string,
some charactors are missing.
here, the basic string contians bytes that rang from -127 to 127,
which is the result of an encrypting function.
And what do you expect the result to be? Which encoding to you expect
it to use?

As a general rule, it's a *really, really* bad idea to convert
arbitrary binary data (such as the result of encryption) to text
directly. Use Base64 instead.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 20 '07 #2

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

Similar topics

1
4196
by: Joe | last post by:
I'm trying to convert system.string to a com method that wants a BSTR*, and array of BSTR. Can anyone help?
4
1685
by: Yan Vinogradov | last post by:
There are two ways to convert a char* received from the unmanaged code into a managed System.String object that I am aware of. The first one is to simply construct String object passing char* as constructor parameter. The second one is to use Marshal::PtrToStringAnsi (or similar) method. Both seem to work just fine. Are there any catches that I am missing? Thanks, Yan
3
1829
by: Andy Sutorius via DotNetMonster.com | last post by:
Hi, I have a Sorted List that has 9 key/value pairs. I am trying to take one of the key/value pairs and store the value of that key into a string variable. However, the value that actually gets inserted into the variable is "System. String" and I don't understand what happened to my original value in the key/value pair. Thanks for your help,
2
1195
by: Bae,Hyun-jik | last post by:
Hi, My managed C++ library frequently takes LPCTSTR from managed exe. Due to the fact that my library doesn't modify string buffer if its parameter type is LPCTSTR, it won't be required to copy text data from System::String to another buffer memory, however, I couldn't find how to let my library access System::String text buffer directly. Please reply any answers. Thanks in advance.
2
1406
by: John Swan | last post by:
Hello. I cannot seem to find any documentation upon the STL string. I am simply trying to transfer the contents of a std::string to a System::String where I am coming into difficulties. Any help would be appreciated. Cheers John
24
17464
by: Marcus Kwok | last post by:
Hello, I am working on cleaning up some code that I inherited and was wondering if there is anything wrong with my function. I am fairly proficient in standard C++ but I am pretty new to the .NET managed C++. It seems to work fine, but everyone knows that programs with errors can still appear to "work fine" :) I am working with VS .NET 2003; I am unable to upgrade to 2005 at this time, so I cannot use the newer syntax or features. ...
6
7461
by: DaTurk | last post by:
Hi, I have several interfaces in CLI that I access via c#. My problem is, is that down in the unmanaged c++, which the CLI lies on top of, I have a lot of c_str() happening. But all of my methods in CLI return System::String^. I originally just gcnew'd System::String^ passing in the c_str(). But I can't really have as many gcnew's as I'm using for overhead and for fear of leaks. So my question is this, how can I get the char*...
1
3807
by: vcbytes | last post by:
I am having a problem with the following code: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { String^ texts = textBox1->Text; char *text = strToChars(texts); String^ shiftcs = domainUpDown1->Text; char *shiftc = strToChars(shiftcs); int shift = atoi(shiftc); char *cipher = encCCipher(text, shift); String^ ciphers = gcnew String(cipher); textBox2->Text = ciphers;
2
8348
by: Man4ish | last post by:
How can we convert std::string into basic string?
0
8130
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8627
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8579
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8433
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6088
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4127
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2568
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1747
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1425
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.