String to byte *

  • Thread starter Thread starter Tobias Svensson
  • Start date Start date
T

Tobias Svensson

Hi,
How can I convert a String to a byte * pointer? I am using a third-party
component that has functions that take byte * parameters.

regards Tobias
 
private unsafe void button26_Click(object sender, System.EventArgs e)
{
string s = "Hello";
fixed ( char* pc = s )
{
byte * pb = (byte*)pc;
char c1 = *pc;
Console.WriteLine("Char0:"+c1);
char c2 = pc[1];
Console.WriteLine("Char1:"+c2);
Console.WriteLine("byte0:"+pb[0]);
Console.WriteLine("byte1:"+pb[1]);
Console.WriteLine("byte2:"+pb[2]);
Console.WriteLine("byte3:"+pb[3]);
}
}
 
I can't be sure without some more information about the what the dll expects
but you should probably try out the System.Text.Encoding class. Create the
encoding appropriate to the application (Unicode, UTF-8, default, etc.) and
then call the GetBytes method. Declare the function prototype to take a byte
array and you should be fine.
 
Back
Top