473,618 Members | 3,005 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9297
Hi Tim,

Well, UTF8 won't work due to characters being both 1 and 2 bytes long.
You could try using Encoding.Defaul t, 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****@newsgro ups.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.comwro te in message
news:op.tfra9cg uklbvpo@tr024.. .
Hi Tim,

Well, UTF8 won't work due to characters being both 1 and 2 bytes long.
You could try using Encoding.Defaul t, 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****@newsgro ups.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.comwro te in message
news:op.tfra9cg uklbvpo@tr024.. .
>Hi Tim,

Well, UTF8 won't work due to characters being both 1 and 2 bytes long.
You could try using Encoding.Defaul t, 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.Unicod e (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****@newsgro ups.nospamschri eb im Newsbeitrag
news:uC******** ******@TK2MSFTN GP04.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.comwro te in message
news:op.tfra9cg uklbvpo@tr024.. .
>Hi Tim,

Well, UTF8 won't work due to characters being both 1 and 2 bytes long.
You could try using Encoding.Defaul t, 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****@newsgro ups.nospamwrote in message
news:%2******** ********@TK2MSF TNGP04.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
13914
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 something like: char chars = "Hello!"; byte bytes = (byte) chars;
4
10298
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 bytes that are not printable characters? I've tested this and it seems to work fine, but I want to make sure there isn't some condition or situation I'm not aware of that could cause problems. I'm doing this because it's easier to do some of my...
3
2336
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 need to be converted to an enum. The next 4 bytes to a 32-bit int. And so on. I'm basically populating a struct with the data. How does one do this in C#? Thanks.
4
5381
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 "System.Text.Encoding.ASCII.GetString(bytearray)" of .Net library, we found that the char (delimiters) specified above are replaced with different char.
8
4557
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 XML, and embedded the array as some child element in an xml file, i.e.: <Mask>bHQAAH4AAABCTX4AAAAAAAAAPgAAACgAAAAQAAAAEAAAAAEAAQAAAAAAQAAAAAAAAAAAAA AA AAAAAAAAAAAAAAAA////AP//AAD//wAA//8AAP//AAD/7wAA//cAALtzAABVeQAAVUAAAFVA...
1
9557
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 first one.
5
4179
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 serial ports as bytes and some of these represent strings. (Like the Packed BCD date/time stamp etc). At present I read through each Byte and convert to char and append to a string.
8
4192
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, here's the code: ====================================== Dim textConverter As New ASCIIEncoding Dim sParam As String = "This is my cool param" Dim bytParam() As Byte 'load the byte array here...
16
5438
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 learning all help would be greatly apperciated. Attribute VB_Name = "Module1" Option Explicit
6
8385
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 assembly? _________________________________________________________ def encrypt_password(challenge, password): if challenge == 'md5': inner = md5(challenge + password).hexdigest() elif challenge == 'sha1': inner =...
0
8212
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8653
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8595
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8455
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7126
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6101
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5552
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4065
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1760
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.