Z
Z
Zakhar Alekseenko2014-02-05 06:03:47
.NET
Zakhar Alekseenko, 2014-02-05 06:03:47

Correct conversion of char[] to System::String

const int len=117;
char str[len];
memcpy(str,somechararray,len);
System::String^ clistr = gcnew System::String(str);

Random characters appear in the clistr string after the text from somechararray.
What is the correct way to convert a fixed-length string to System::String without squiggles at the end of the string?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2014-02-05
@Zaher220

char str[len + 1];
memcpy(str,somechararray,len);
str[len] = 0;

This is if you really want to copy the substring.
In general, String has a constructor that takes a pointer, position, and length. Why don't you just write like this:
System::String^ clistr = gcnew System::String(somechararray, 0, len);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question