C Style strings to .NET strings.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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?
 
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)
 
David said:
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?

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
 
Back
Top