Encoding.ASCII.GetBytes

D

devgrt

Checking if proper/best way to do this...
I have to pass a C# string to a C++ DLL. The function in the DLL
(gethostbyname) requires a null terminated string. It seems like when you do
byte [] var = Encoding.ASCII.GetBytes(string); that it only loads the byte
array with the chars and does not add a null at the end. I would like to
know if the below is proper way to deal with this:

byte [] var = Encoding.ASCII.GetBytes(string + '\0');

By using this when I pass var to C++ func char* I do get the correct string
and can call gethostbyname(var); and it works OK.

Thank you!
 
M

Mattias Sjögren

byte [] var = Encoding.ASCII.GetBytes(string + '\0');
By using this when I pass var to C++ func char* I do get the correct string
and can call gethostbyname(var); and it works OK.

That will work, but an easier way would be to change your
gethostbyname declaration to take a string parameter directly.


Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top