converting string to byte array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tbh

    converting string to byte array

    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


  • Morten Wennevik

    #2
    Re: converting string to byte array

    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 <femdev@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]

    Comment

    • tbh

      #3
      Re: converting string to byte array

      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" <MortenWennevik @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.

      Comment

      • Walter Wang [MSFT]

        #4
        RE: converting string to byte array

        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 (wawang@online. microsoft.com, remove 'online.')
        Microsoft Online Community Support

        =============== =============== =============== =====
        Get notification to my posts through email? Please refer to
        Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.

        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.

        Comment

        • Morten Wennevik

          #5
          Re: converting string to byte array

          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 <femdev@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" <MortenWennevik @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]

          Comment

          • Christof Nordiek

            #6
            Re: converting string to byte array

            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" <femdev@newsgro ups.nospamschri eb im Newsbeitrag
            news:uCKADZk1GH A.1252@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" <MortenWennevik @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.
            >
            >

            Comment

            • Walter Wang [MSFT]

              #7
              RE: converting string to byte array

              Hi Tim,

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

              Regards,
              Walter Wang (wawang@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.

              Comment

              • tbh

                #8
                Re: converting string to byte array

                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" <femdev@newsgro ups.nospamwrote in message
                news:%23N964Qk1 GHA.4748@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
                >


                Comment

                • Walter Wang [MSFT]

                  #9
                  Re: converting string to byte array

                  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 (wawang@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.

                  Comment

                  Working...