By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,696 Members | 2,256 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,696 IT Pros & Developers. It's quick & easy.

byte[] of data -> multiple strings

P: n/a
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?

Nov 16 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a

"Maury Markowitz" <Ma************@discussions.microsoft.com> wrote in
message news:21**********************************@microsof t.com...
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
Nov 16 '05 #2

P: n/a
Maybe convert to a single string and then use the Split method to split with
the null?

Thomas P. Skinner [MVP]

"Maury Markowitz" <Ma************@discussions.microsoft.com> wrote in
message news:21**********************************@microsof t.com...
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?

Nov 16 '05 #3

P: n/a
Maury Markowitz <Ma************@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

P: n/a
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
Nov 16 '05 #5

P: n/a
"Robert Jordan" wrote:
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[].
Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.