object to a byte[]

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

    #1

    object to a byte[]

    How can I change object returned from database to a byte array
    e.g.
    byte[] b = row["FullName"];

    also

    how can I save a byte[] to database row?



    Thank you,
    Po





    --
    ----------------------------------------------------------------------
    This is an unmonitored email account. Please do not reply directly to
    this email. Bunch of green developers use this email account to post
    question in hope of not getting hahaha in return.



  • Gabriel Magaña

    #2
    Re: object to a byte[]

    If what you are asking is how to store binary data in a text DB field, you
    should not do that, as you will get into trouble when you start converting
    bytes to chars and vice versa...

    If you are sure this is what you want to do, though, the answer depends on
    what datatype the DB field is... Since you specified "FullName" as the
    field name, I'll assume it;s a varchar or other text field... In this case
    use the Enconding functions, such as:

    Encoding.ASCII. GetBytes(row["FullName"].ToString());

    To do the opposite use the GetChars() function.

    Be warned, though, if you are not saving text characters make absolutely
    sure that whatever encoding you choose can save (and give you back) the byte
    value ranges you need to store and retrieve.


    Comment

    • Peter Bromberg [C# MVP]

      #3
      RE: object to a byte[]

      To save a byte array to a Database column, you need to use a column type of
      image.
      As Gabriel pointed out you can use the encoding GetString or GetBytes
      methods to convert. There is a static overload that looks like this (example)
      System.Text.Enc oding.UTF8.GetB ytes(row["FullName"])
      and
      System.Text.Enc oding.UTF8.GetS tring(byteArray )

      Peter
      --
      Co-founder, Eggheadcafe.com developer portal:

      UnBlog:





      "Pohihihi" wrote:
      [color=blue]
      > How can I change object returned from database to a byte array
      > e.g.
      > byte[] b = row["FullName"];
      >
      > also
      >
      > how can I save a byte[] to database row?
      >
      >
      >
      > Thank you,
      > Po
      >
      >
      >
      >
      >
      > --
      > ----------------------------------------------------------------------
      > This is an unmonitored email account. Please do not reply directly to
      > this email. Bunch of green developers use this email account to post
      > question in hope of not getting hahaha in return[/color]

      Comment

      • Pohihihi

        #4
        Re: object to a byte[]

        My field is varbinary in SQL db. Do you think converting it into ToString()
        might create problem. "FullName" col is just an example, actually I am
        serializing a 3rd party control.



        "Gabriel Magaña" <no-spam@no-spam.com> wrote in message
        news:O3q2RPkFGH A.3120@TK2MSFTN GP10.phx.gbl...[color=blue]
        > If what you are asking is how to store binary data in a text DB field, you
        > should not do that, as you will get into trouble when you start converting
        > bytes to chars and vice versa...
        >
        > If you are sure this is what you want to do, though, the answer depends on
        > what datatype the DB field is... Since you specified "FullName" as the
        > field name, I'll assume it;s a varchar or other text field... In this
        > case use the Enconding functions, such as:
        >
        > Encoding.ASCII. GetBytes(row["FullName"].ToString());
        >
        > To do the opposite use the GetChars() function.
        >
        > Be warned, though, if you are not saving text characters make absolutely
        > sure that whatever encoding you choose can save (and give you back) the
        > byte value ranges you need to store and retrieve.
        >[/color]


        Comment

        • Pohihihi

          #5
          Re: object to a byte[]

          My bad, I should have given more information on col type, it is varbinary.
          Will this work on that as well?


          "Peter Bromberg [C# MVP]" <pbromberg@yaho o.nospammin.com > wrote in message
          news:BE6EA5E4-F889-4E91-9474-9E5ACD5F6573@mi crosoft.com...[color=blue]
          > To save a byte array to a Database column, you need to use a column type
          > of
          > image.
          > As Gabriel pointed out you can use the encoding GetString or GetBytes
          > methods to convert. There is a static overload that looks like this
          > (example)
          > System.Text.Enc oding.UTF8.GetB ytes(row["FullName"])
          > and
          > System.Text.Enc oding.UTF8.GetS tring(byteArray )
          >
          > Peter
          > --
          > Co-founder, Eggheadcafe.com developer portal:
          > http://www.eggheadcafe.com
          > UnBlog:
          > http://petesbloggerama.blogspot.com
          >
          >
          >
          >
          > "Pohihihi" wrote:
          >[color=green]
          >> How can I change object returned from database to a byte array
          >> e.g.
          >> byte[] b = row["FullName"];
          >>
          >> also
          >>
          >> how can I save a byte[] to database row?
          >>
          >>
          >>
          >> Thank you,
          >> Po
          >>
          >>
          >>
          >>
          >>
          >> --
          >> ----------------------------------------------------------------------
          >> This is an unmonitored email account. Please do not reply directly to
          >> this email. Bunch of green developers use this email account to post
          >> question in hope of not getting hahaha in return[/color][/color]


          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: object to a byte[]

            Gabriel Magaña <no-spam@no-spam.com> wrote:[color=blue]
            > If what you are asking is how to store binary data in a text DB field, you
            > should not do that, as you will get into trouble when you start converting
            > bytes to chars and vice versa...
            >
            > If you are sure this is what you want to do, though, the answer depends on
            > what datatype the DB field is... Since you specified "FullName" as the
            > field name, I'll assume it;s a varchar or other text field... In this case
            > use the Enconding functions, such as:
            >
            > Encoding.ASCII. GetBytes(row["FullName"].ToString());
            >
            > To do the opposite use the GetChars() function.
            >
            > Be warned, though, if you are not saving text characters make absolutely
            > sure that whatever encoding you choose can save (and give you back) the byte
            > value ranges you need to store and retrieve. [/color]

            Note that choosing Encoding.ASCII is almost always a bad idea, unless
            you're absolutely *positive* that there won't be any characters above
            Unicode 127. I would suggest using Encoding.UTF8 usually.

            --
            Jon Skeet - <skeet@pobox.co m>
            http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
            If replying to the group, please do not mail me too

            Comment

            • Gabriel Magana

              #7
              Re: object to a byte[]

              Agreed, I just picked the first encoding on the list :-)

              When dealing with binary data, wouldn't using UTF8 (or any other MBCS, for
              that matter) be a bad idea too? At the worst case it would waste a lot of
              space, since two bytes (or more, depending on the MBCS binary encoding)
              would be used for each byte worth of data? As I understand it (and I could
              very well be wrong, since it's been a long time sonce I have sotred binary
              data in a character data type ;-) ) , the best character set for doing this
              is ISO8859P1 since theoretically it holds the 127 ASCII values plus the 128
              "high ASCII" characters? Anyway, if it did, it'd be cool since each
              character/byte would end up taking a single byte's worth of space after
              conversion...

              "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
              news:MPG.1e2ec0 dbabefb57398cca d@msnews.micros oft.com...
              Gabriel Magaña <no-spam@no-spam.com> wrote:[color=blue]
              > If what you are asking is how to store binary data in a text DB field, you
              > should not do that, as you will get into trouble when you start converting
              > bytes to chars and vice versa...
              >
              > If you are sure this is what you want to do, though, the answer depends on
              > what datatype the DB field is... Since you specified "FullName" as the
              > field name, I'll assume it;s a varchar or other text field... In this
              > case
              > use the Enconding functions, such as:
              >
              > Encoding.ASCII. GetBytes(row["FullName"].ToString());
              >
              > To do the opposite use the GetChars() function.
              >
              > Be warned, though, if you are not saving text characters make absolutely
              > sure that whatever encoding you choose can save (and give you back) the
              > byte
              > value ranges you need to store and retrieve.[/color]

              Note that choosing Encoding.ASCII is almost always a bad idea, unless
              you're absolutely *positive* that there won't be any characters above
              Unicode 127. I would suggest using Encoding.UTF8 usually.

              --
              Jon Skeet - <skeet@pobox.co m>
              http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
              If replying to the group, please do not mail me too


              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: object to a byte[]

                Gabriel Magana wrote:[color=blue]
                > Agreed, I just picked the first encoding on the list :-)
                >
                > When dealing with binary data, wouldn't using UTF8 (or any other MBCS, for
                > that matter) be a bad idea too?[/color]

                No. See later.
                [color=blue]
                > At the worst case it would waste a lot of
                > space, since two bytes (or more, depending on the MBCS binary encoding)
                > would be used for each byte worth of data? As I understand it (and I could
                > very well be wrong, since it's been a long time sonce I have sotred binary
                > data in a character data type ;-) ) , the best character set for doing this
                > is ISO8859P1 since theoretically it holds the 127 ASCII values plus the 128
                > "high ASCII" characters? Anyway, if it did, it'd be cool since each
                > character/byte would end up taking a single byte's worth of space after
                > conversion...[/color]

                That's great if you only have characters within ISO-8859-1 - what about
                all the other Unicode characters though? Are you willing to bet that
                everything you'll ever need is within ISO-8859-1?

                If many of your characters are above U+07FF, you might consider using
                UTF-16, as those characters take 3 (or more) bytes in UTF-8. However,
                UTF-8 keeps ASCII characters in a single byte, and only two bytes for
                anything between U+0080 and U+07FF.

                Basically, if you want to be able to accurately store and retrieve
                arbitrary Unicode strings, you need to use an encoding which can cope
                with the full range. For many common uses (where ASCII characters form
                the bulk of the text) UTF-8 is very efficient.

                Jon

                Comment

                • Gabriel Magana

                  #9
                  Re: object to a byte[]

                  [color=blue]
                  > That's great if you only have characters within ISO-8859-1 - what about
                  > all the other Unicode characters though? Are you willing to bet that
                  > everything you'll ever need is within ISO-8859-1?[/color]

                  Thanks for your good reply Jon, but don't forget we are talking about byte[]
                  arrays, which imply the character ordinal range is 0-255. We only need to
                  map 1 char to 1 byte (0x00 to 0xFF value) at a time...

                  It's funny how we always seem to end up talking about character-related
                  issues.

                  gabriel


                  Comment

                  • Jon Skeet [C# MVP]

                    #10
                    Re: object to a byte[]

                    Gabriel Magana <noone@nospam.c om> wrote:[color=blue]
                    >[color=green]
                    > > That's great if you only have characters within ISO-8859-1 - what about
                    > > all the other Unicode characters though? Are you willing to bet that
                    > > everything you'll ever need is within ISO-8859-1?[/color]
                    >
                    > Thanks for your good reply Jon, but don't forget we are talking about byte[]
                    > arrays, which imply the character ordinal range is 0-255. We only need to
                    > map 1 char to 1 byte (0x00 to 0xFF value) at a time...
                    >
                    > It's funny how we always seem to end up talking about character-related
                    > issues.[/color]

                    Hmm... looking back, it's not at all clear to me where strings have
                    come into the conversation at all. Calling ToString() on the value
                    returned from the row seems to be a bad idea, really - if it's not
                    inherently text-based, it shouldn't be treated as a string.

                    I would have a look at the *actual* runtime type of the object returned
                    by row["FullName"]...

                    However, if it's a case of encoding a "full name" into binary, I'd
                    certainly not assume that all characters are less than 0xff in
                    unicode...

                    --
                    Jon Skeet - <skeet@pobox.co m>
                    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                    If replying to the group, please do not mail me too

                    Comment

                    Working...