I have a byte array that contains a whole bunch of information in it. It
gets generated from an embedded device and sent to me over ethernet.
Among the elements of information in that byte array is a C-style string.
In other words, there are 16 bytes. Each byte contains an ASCII character,
or 0. The string is null terminated.
I want to load those characters into a .NET string object. Is there a
single call that can do that?
I thought that ASCIIEncoding.ASCII.GetString(array, startindex, 16) would do
it for me. However, if the byte array contains the ascii character 'b',
followed by 15 zeros, what I want is the string "b", a string with length 1.
What I get is a string "b/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0", a string with
length 16, in which the first character is the unicode 'b', and the others
are the unicode null character.
After failing to find a function that would do it, I just wrote a function
that took such a string and stripped out the first null and anything after
it, and that worked. However, it seemed to me that this would be a common
enough operation that there is probably a function call that did it in one
call, which would result in better code. Is there such a function?
Lloyd Dupont - 29 Jul 2005 14:36 GMT
I'm not usre about the performance (although can't be that bad) but there is
such a method in the
System.Runtime.InteropServices.Marshal class
from the top of my hea dI would say
Marshal.ToAnsiString(ptr)

Signature
There are 10 kinds of people in this world. Those who understand binary and
those who don't.
>I have a byte array that contains a whole bunch of information in it. It
> gets generated from an embedded device and sent to me over ethernet.
[quoted text clipped - 21 lines]
> enough operation that there is probably a function call that did it in one
> call, which would result in better code. Is there such a function?
Chris - 29 Jul 2005 19:50 GMT
> I have a byte array that contains a whole bunch of information in it. It
> gets generated from an embedded device and sent to me over ethernet.
[quoted text clipped - 18 lines]
> enough operation that there is probably a function call that did it in one
> call, which would result in better code. Is there such a function?
Well if you think about it, what any function call is going to have to
do is basically the same loop you are doing. As long as your function
is effencient as possible, it'll be just as fast as any function call,
and maybe faster since you know what you are doing and any function you
call will probably be more generic.
PS. Any interop call would be slower than anything native..
Chris
Atul - 30 Jul 2005 06:56 GMT
Use the Marshal.PtrToStringAnsi function.
- Atul
Sky Software http://www.ssware.com/
Drop-In Windows Explorer-Like Shell Browsing UI for your apps.
>I have a byte array that contains a whole bunch of information in it. It
> gets generated from an embedded device and sent to me over ethernet.
[quoted text clipped - 21 lines]
> enough operation that there is probably a function call that did it in one
> call, which would result in better code. Is there such a function?
David - 01 Aug 2005 18:15 GMT
Thanks, everyone. (And special thanks to Lloyd and his sig. I liked it.)
> I have a byte array that contains a whole bunch of information in it. It
> gets generated from an embedded device and sent to me over ethernet.
[quoted text clipped - 18 lines]
> enough operation that there is probably a function call that did it in one
> call, which would result in better code. Is there such a function?