byte[] of data -> multiple strings

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

Guest

I have a byte[] returned from a DLL that contains n c-style strings inside
it. Any suggestions on how to easily pull them out into a string[]?

Encoding helps with a single byte array (although easily enough), but
doesn't parse out runs of them. Looping over the bytes looking for x00's
strikes me as too 1960s.

Is there some helper that does this?
 
Maury Markowitz said:
I have a byte[] returned from a DLL that contains n c-style strings inside
it. Any suggestions on how to easily pull them out into a string[]?

Encoding helps with a single byte array (although easily enough), but
doesn't parse out runs of them. Looping over the bytes looking for x00's
strikes me as too 1960s.

Is there some helper that does this?

How do you know how many null terminated strings there are?

David
 
Maybe convert to a single string and then use the Split method to split with
the null?

Thomas P. Skinner [MVP]
 
Maury Markowitz said:
I have a byte[] returned from a DLL that contains n c-style strings inside
it. Any suggestions on how to easily pull them out into a string[]?

Encoding helps with a single byte array (although easily enough), but
doesn't parse out runs of them. Looping over the bytes looking for x00's
strikes me as too 1960s.

Is there some helper that does this?

Well *something's* got to go through the bytes looking for 0s - I don't
believe there's anything in the framework which does it.

Admittedly you *could* just convert the whole thing to a string, and
use String.Split ('\0') but I'm not sure it's the best way of doing
things.
 
Hi Jon,
Well *something's* got to go through the bytes looking for 0s - I don't
believe there's anything in the framework which does it.

Admittedly you *could* just convert the whole thing to a string, and
use String.Split ('\0') but I'm not sure it's the best way of doing
things.

It definitely works. It's an usual interop trick: marshal the
string as byte[], convert the byte[] to string, split on '\0'.

bye
Rob
 
Robert Jordan said:
It definitely works. It's an usual interop trick: marshal the
string as byte[], convert the byte[] to string, split on '\0'.

I can understand how this would work for a known-length byte[], but the
length in this case is variant (0..n strings of 0..m length) and I'm not
allocating it, in the struct it's simply byte[]. If the strings were fixed
length (as they are on the call-side that results in this data) then there's
some solutions, as would be the case if it were an array of
pointers-to-strings.

FYI I tried a number of solutions to "directly cast" the data into the
string array before using the byte[] trick, but it seems that the runtime
doesn't allow the obvious ones. For instance, the nicest solution would be to
do it this way...

[MarshalAs(UnmanagedType.ByValArray, ArraySubType=UnmanagedType.LPStr,
SizeConst=numFlds)]
string[] data

but that's apparently illegal and returns the "you can't marshal this
struct" error. I guess that makes sense, it needs to know the size of the
overall struct when doing the mapping back into the managed side. Various
attempts at char[] failed for the same reason.

I was able to debug it a little by saying it was an IntPtr (as a
placeholder) and then using PtrToString on that address. Then I could just
loop on PtrToString by making a new IntPtr + strLen. This almost works, I can
get the first 32 bits (4 chars) back OK but from that point on the data is
mushed and says that the string is always 11 chars long (which it isn't). If
I can figure that out I think I'll go this route, and get rid of the byte[].
 
Back
Top