Converting char* to String^

I have an odd question...

I am creating a terminal like program that will be used send SCPI commands to a micro controller over USB using the USBTMC class...

So here's my problem:

1
2
3
4
5
6
7
8
9
10
char *BulkOUT = new char[commandSize];

//Bunch of stuff to create the header

String ^ commandStr = gcnew String(BulkOUT, 0, commandSize);

if(this->serialPort->IsOpen)
    this->serialPort->WriteLine(commandStr);
else
    //report error 


I created a command header that I will need to send out, and I saved that command header in a char array BulkOUT. To use the WriteLine function, I need to save the command header as a String^, so I used the String constructor. My problem is that I DO NOT get the correct string! Why? Well I noticed that it only copies up to BulkOUT[2] when the command header (for testing purposes) is 24 characters long! I realized it does this because BulkOUT[3] = 0x00 {for the USBTMC class spec, the fourth byte is reserved and must be 0x00}.

So my problem is that I cant copy my char array because I have multiple instances of 0x00 at different indexes (not just index 3)...so what should I do?? How can I create a String^ if my character array has some 0x00 values? Is there another way to send a command through the comport that does not require the writing variable to be String^??

Any help would be greatly appreciated...
sounds like the string BulkOUT is a unicode string.

what happens if you convert it to an ascii string first?

There is a managed String constructor that takes an array and an encoding. You could try the ASCII encoding as a parameter.
http://msdn.microsoft.com/en-us/library/9d876whe%28v=vs.110%29.aspx?cs-save-lang=1&cs-lang=cpp#code-snippet-1
closed account (10X9216C)
Don't use C++ CLR. If you want to use .Net just use C#. It just goes so much against what C++ does, if you are going to use mostly .net anyway then save yourself the trouble.
Don't use C++ CLR. If you want to use .Net just use C#.


this is true most times but in cases where you have native modules that need to be executed as well as managed ones, then C# is not a good choice as it offers no such means to call native and managed code within the same process.

even VB .NET can't do that only c++/cli can.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <string>
#include <iostream>
#include <cstdlib>

int main()
{
    const std::size_t nchars = 24 ;

    {
        // work with wide characters
        const wchar_t wstr[ nchars + 1 ] = L"How\000Now\000Brown\000Cow!!!!!!!" ;

        auto cli_str = gcnew System::String( wstr, 0, nchars );

        for( int i = 0 ; i < cli_str->Length ; ++i )
        {
            if( cli_str[ i ] != 0 ) System::Console::Write( cli_str[ i ] ) ;
            else System::Console::Write( "<null>" ) ;
        }
        System::Console::WriteLine() ;
    }

    {
        char cstr[ nchars + 1 ] = "How\000Now\000Brown\000Cow!!!!!!!" ; 
        
        // or convert array of char to array of wide char
        wchar_t wstr[ nchars + 1 ] = {0} ;
        for( std::size_t i = 0 ; i < nchars ; ++i )
            if( cstr[ i ] ) std::mbtowc( wstr + i, cstr + i, 1 ) ; // assumes that there are no multibyte chars
        
        auto cli_str = gcnew System::String( wstr, 0, nchars );

        for( int i = 0 ; i < cli_str->Length ; ++i )
        {
            if( cli_str[ i ] != 0 ) System::Console::Write( cli_str[ i ] ) ;
            else System::Console::Write( "<null>" ) ;
        }
    }
    System::Console::WriteLine() ;
}

http://rextester.com/YWEL88116
Thanks JLBorges, I used the second solution and got everything to work

I havent tried booradley60 solution, using the String constructor with ASCII encoding...but it sounds simple enough, maybe I'll try it at a different time
closed account (10X9216C)
C# is not a good choice as it offers no such means to call native and managed code within the same process.

You can... Maybe not C++ but it can definitely call C functions. Do you mean you can't write native code in C#?
You can... Maybe not C++ but it can definitely call C functions.


my mistake you are correct - I only considered cases where you have native c++ libraries you need to call from your managed code and want as little rewriting or creating of c wrappers ...

Topic archived. No new replies allowed.