converting string to byte array

T

tbh

for historical reasons i need to be able to call, from C# under DotNet 2, as
COM+ DLL function that returns a "string" which is really an array of
seemingly arbitrary bytes (presumably non-zero). however, C# treats this as
a string and the only ways i have found to convert it to a byte array lose
data (apparently in those cases when the "characters" are not legal UTF
values).

in this case (a program i only need to run once) i would be willing to
tolerate an unsafe solution, but i haven't been able to find one, despite
fairly extensive searching.

is this not possible or have i just not found the right trick yet?

Tim Hanson
 
M

Morten Wennevik

Hi Tim,

Well, UTF8 won't work due to characters being both 1 and 2 bytes long.
You could try using Encoding.Default, your default ANSI code table, which
should convert each byte in the string to a separate byte in an array.
 
T

tbh

thanks, Morten, but i don't want any encoding. i want C# to treat the string
as if it were a byte array. (it is, the COM+ library pretends it is a
string.)
 
W

Walter Wang [MSFT]

Hi Tim,

Would you please tell me how you return the "string" from COM+ DLL
function? Is it correctly including the string length in the header? Both
COM and .NET internally use Unicode to represent string, unless there're
some encoding conversion during the calling, there should be no loss of
data. Please feel free to post here if I've misunderstood anything.

Also, have you tried to use String.CopyTo() to copy the returned string to
a char array? Then you can convert each char to an integer and get the two
bytes.

Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Morten Wennevik

As long as the string is untouched I don't think it matters which encoding
you get the bytes with, as long as the encoding is 8-bit. I may be wrong
though.
 
C

Christof Nordiek

Hi Tim,

since .NET represents string internally as UTF-16 this would be the right
encoding.
Try Encoding.Unicode (It's how .NET says UTF-16) and look if the result is
right.
Then try too insert some false surrogates and noncharacter to test if they
will be treated
right. If that works it should work for all.
 
W

Walter Wang [MSFT]

Hi Tim,

Would you please reply here so that we can know the status of this post?
Thank you.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
T

tbh

hmmm, the MS Outlook Newsreader has trouble seeing all the items in this
thread, so sorry for not replying where i should.

thanks for all the ideas. i'm swamped at the moment or i would have
responded earlier and won't manage a careful, complete example of what i
mean this week, but i can sketch one that people could flesh out if they
find time.

i think the problem is that the COM+ object, written in VB and compiled
quite a few years ago, has a different understanding of what a legal
"string" is than C#/DotNet2/CLR(/...?) does. it returns strings which
contain sequences of bytes which are not legal UTF characters. this tupel of
bytes is in effect smuggled into DotNet as a string. since it's a string i
can convert it to a character array (in general with losses of data -- the
illegal byte combinations), but not to a byte array (which i guess is
considered a no-no in the modern, protected world.)

i'm sorry but I don't know whether the "string" that comes from COM+ is a
0-terminated sequence of non-0 bytes or a (size, array-of-bytes) pair --
haven't needed badly enough to "look under the hood" in DotNet, not even
sure whether I can.

sorry i can't give you more than these vague descriptions. we will find
other ways to deal with our problem so it will become academic to me at some
point. (the data should have been binary in the DB and array-of-bytes or
equivalent in DotNet anyway, we just have no way to make it that in this
case at this time.)

cheers,

Tim
 
W

Walter Wang [MSFT]

Hi Tim,

COM uses Unicode exclusively. COM strings are called "OLE Strings" or
"Basic Strings" (BSTR). This is a data type that is stored as a string
length value and a null-terminated character array.

Though you mentioned that you will find other ways to deal with this issue,
if you can provide some source code of the COM+ object, especially how it
returns the byte array as a BSTR, other community members and I might be
able to help to convert them back to the byte array in .NET.

Please reply to let us know whether or not you want to continue this
discussion.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top