470,815 Members | 1,051 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

Encoding.ASCII.GetBytes

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!
Mar 1 '06 #1
1 11589
>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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Mar 1 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Tim Murray | last post: by
reply views Thread by Jason L James | last post: by
3 posts views Thread by devgrt | last post: by
4 posts views Thread by cok | last post: by
9 posts views Thread by =?Utf-8?B?RGFu?= | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.