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

converting string to byte array

P: n/a
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
Sep 12 '06 #1
Share this Question
Share on Google+
8 Replies


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

On Tue, 12 Sep 2006 10:19:50 +0200, tbh <fe****@newsgroups.nospamwrote:
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



--
Happy Coding!
Morten Wennevik [C# MVP]
Sep 12 '06 #2

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

"Morten Wennevik" <Mo************@hotmail.comwrote in message
news:op.tfra9cguklbvpo@tr024...
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.

Sep 12 '06 #3

P: n/a
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 (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

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

Sep 12 '06 #4

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

On Tue, 12 Sep 2006 10:34:24 +0200, tbh <fe****@newsgroups.nospamwrote:
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.)

"Morten Wennevik" <Mo************@hotmail.comwrote in message
news:op.tfra9cguklbvpo@tr024...
>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.



--
Happy Coding!
Morten Wennevik [C# MVP]
Sep 12 '06 #5

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

"tbh" <fe****@newsgroups.nospamschrieb im Newsbeitrag
news:uC**************@TK2MSFTNGP04.phx.gbl...
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.)

"Morten Wennevik" <Mo************@hotmail.comwrote in message
news:op.tfra9cguklbvpo@tr024...
>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.


Sep 12 '06 #6

P: n/a
Hi Tim,

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

Regards,
Walter Wang (wa****@online.microsoft.com, 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.

Sep 14 '06 #7

P: n/a
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
"tbh" <fe****@newsgroups.nospamwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
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


Sep 15 '06 #8

P: n/a
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 (wa****@online.microsoft.com, 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.

Sep 18 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.