[ACCEPTED]-C#: Converting String to Sbyte*-unmanaged
Hmmm how about something like this:
(didnt test it, dont give me -1 if it doesnt work, I just believe that it should) :))
string str = "The quick brown fox jumped over the gentleman.";
byte[] bytes = Encoding.ASCII.GetBytes(str);
unsafe
{
fixed (byte* p = bytes)
{
sbyte* sp = (sbyte*)p;
//SP is now what you want
}
}
0
You can do that way:
sbyte[] sbytes = Array.ConvertAll(bytes, q => Convert.ToSByte(q));
0
sbyte[] and sbyte* are almost the same thing 16 (almost)
sbyte[] str1; sbyte* str2;
&str1[0] is 15 a pointer to the first element of the array 14 of chars str2 is a pointer to a char, which 13 presumably has a bunch of consecutive chars 12 following it, followed by a null terminator
if 11 you use str1 as an array, you know the length 10 without a null terminator. but if you want 9 to pass str1 to a string api that requires 8 sbyte*, you use &str1[0] to turn it 7 into a sbyte*, and then you stripped away 6 array length information, so you have to 5 make sur eyou're null terminated.
Cipi's 4 answer shows you how to convert an array 3 to a pointer, C# makes it hard to use pointers, on 2 purpose. in C or C++ arrays and pointers 1 are similar.
http://www.lysator.liu.se/c/c-faq/c-2.html
http://pw1.netcom.com/~tjensen/ptr/pointers.htm
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.