473,398 Members | 2,403 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,398 software developers and data experts.

Converting String into char *

I'm using the VC++ 2005 beta. I've seen several other suggestions on the
message boards, but none even compiled for me....here is my solution:

args[0] is a managed String, and the first argument passed into my program.
It is supposed to be a filename. I have a legacy library which is unmanaged
code that requires an old c-style char array.

System::Text::ASCIIEncoding ascii_enc;
int len = ascii_enc.GetByteCount(args[0]->ToCharArray(),0,args[0]->Length);
array<unsigned char,1> ^fname_array = gcnew array<unsigned char,1>(len+1);
ascii_enc.GetBytes(args[0],0,args[0]->Length,fname_array,0);
char *fname = new char[len+1];
for (int i=0;i<len;i++)
fname[i] = fname_array[i];
fname[len] = '\0';
old_library_call(fname);
delete fname;

1.) was there an easier way to do this?
2.) Is there anything inherently wrong with what I've done?
Nov 17 '05 #1
2 1482
Well...that was actually quite helpful. The marshalling functions work...you
know it helps if you use the right namespace :)

On a side note, I think the context sensitive help in VC++ 2005 needs a
little work...

Thanks.

"rmacias" wrote:
I'm not too familiar with the VC++2005 enhancements, but I would take a look
at the Marshal class as it has methods to copy strings into unmanaged memory.
Take a look at:

http://support.microsoft.com/default...b;EN-US;311259


Nov 17 '05 #2
I'm not too familiar with the VC++2005 enhancements, but I would take a look
at the Marshal class as it has methods to copy strings into unmanaged memory.
Take a look at:

http://support.microsoft.com/default...b;EN-US;311259

"tmr_net" wrote:
I'm using the VC++ 2005 beta. I've seen several other suggestions on the
message boards, but none even compiled for me....here is my solution:

args[0] is a managed String, and the first argument passed into my program.
It is supposed to be a filename. I have a legacy library which is unmanaged
code that requires an old c-style char array.

System::Text::ASCIIEncoding ascii_enc;
int len = ascii_enc.GetByteCount(args[0]->ToCharArray(),0,args[0]->Length);
array<unsigned char,1> ^fname_array = gcnew array<unsigned char,1>(len+1);
ascii_enc.GetBytes(args[0],0,args[0]->Length,fname_array,0);
char *fname = new char[len+1];
for (int i=0;i<len;i++)
fname[i] = fname_array[i];
fname[len] = '\0';
old_library_call(fname);
delete fname;

1.) was there an easier way to do this?
2.) Is there anything inherently wrong with what I've done?

Nov 17 '05 #3

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

Similar topics

5
by: matt melton | last post by:
Hi there, I am trying to write a method that accepts an array of any primitive type and will return the same array without copying memory as an array of bytes. ie. I'd like to be able to do...
4
by: Prabhu | last post by:
Hi, We are having problem in converting a byte array to string, The byte array has char(174), char(175), char(240), char(242) and char(247) as delimiters for the message. when we use...
18
by: Marcio Kleemann | last post by:
I need to force the first letter of each word in a line of text to uppercase. The text comes from a TextBox control in a Web Form. I'm new to ..NET and am having a problem. Since I can't modify...
3
by: fakeprogress | last post by:
How would I go about converting this C code to C++? /* LIBRARY is an array of structures */ /* This function compares 'tcode' with */ /* existing codes in the array. */ /* It...
2
by: pookiebearbottom | last post by:
Just looking for opinion on which of the 3 methods below people use in their code when they convert a 'const char *' to a 'const std::string &' came across #3 in someone's code and I had to...
0
by: anide | last post by:
Hi all I’ve some problem, I’m trying to converting a sorting algorithm from C++ to C#. In C++ I’ve compiled it using MSVC and its working properly, and in C# I’m using .NET Framework 2.0 (Visual...
2
by: CoreyWhite | last post by:
Problem: You have numbers in string format, but you need to convert them to a numeric type, such as an int or float. Solution: You can do this with the standard library functions. The...
5
by: Hans Mull | last post by:
Hi! How can I convert a string to a const unsigned char*? (string::c_str() converts the string to a signed char) Thanks in advance, Hans
1
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 =...
4
by: screamer81 | last post by:
Hello, I've a SDK functions description for a scanner and I try to convert unmanaged DLL C++ functions to c#. I converted all except one of them. This function is getting a struct parameter. ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
0
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...

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.