473,325 Members | 2,860 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

converting string to byte array

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
8 9276
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: matt melton | last post by:
Hi there, I am trying to write a method that accepts an array of any primitive type and will return the same array without copying memory as an array of bytes. ie. I'd like to be able to do...
4
by: Hal Vaughan | last post by:
If I have a byte and I convert it to string (String sData = new String(byte bData), then convert it back (byte bData = sData.getBytes()), will all data be intact, or do Strings have problems with...
3
by: Pete Davis | last post by:
I've never done this in C# so I don't know what the appropriate way of doing it is. I've got an array of bytes and I need to convert the array into "usable" data. For example, the first 4 bytes...
4
by: Prabhu | last post by:
Hi, We are having problem in converting a byte array to string, The byte array has char(174), char(175), char(240), char(242) and char(247) as delimiters for the message. when we use...
8
by: iyuen | last post by:
I'm having problems with converting a byte array to an image object~ My byte array is an picture in VB6 StdPicture format. I've used propertybag to convert the picture into base64Array format in...
1
by: Benoit | last post by:
Hi, what is the most performing way to convert a byte stream to a string? Byte() to be converted to String. Thanks, iBen. Sorry if it ia a double post in this newsgroup, I cannot set my...
5
by: David | last post by:
I note that you can null teminate a string by adding controlchar.null. Is there a way of adding a null to a Buffer of Bytes and converting it to a string. I have packets coming in from a...
8
by: moondaddy | last post by:
I need to convert a byte array to a string and pass it as a parameter in a URL and then convert it back to the original byte array. However, its getting scrambled in the conversion. In short,...
16
by: manmit.walia | last post by:
Hello All, I have tried multiple online tools to convert an VB6 (bas) file to VB.NET file and no luck. I was hoping that someone could help me covert this. I am new to the .NET world and still...
6
by: ogtheterror | last post by:
Hi I have a very limited understanding of Python and have given this the best shot i have but still have not been able to get it working. Is there anyone that knows how to get this into a .net...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.