471,337 Members | 1,275 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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.

Jan 10 '06 #1
9 1804
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.
Jan 11 '06 #2
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.Encoding.UTF8.GetBytes(row["FullName"])
and
System.Text.Encoding.UTF8.GetString(byteArray)

Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Pohihihi" wrote:
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

Jan 11 '06 #3
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*****@no-spam.com> wrote in message
news:O3**************@TK2MSFTNGP10.phx.gbl...
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.

Jan 11 '06 #4
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]" <pb*******@yahoo.nospammin.com> wrote in message
news:BE**********************************@microsof t.com...
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.Encoding.UTF8.GetBytes(row["FullName"])
and
System.Text.Encoding.UTF8.GetString(byteArray)

Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Pohihihi" wrote:
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

Jan 11 '06 #5
Gabriel Magaña <no*****@no-spam.com> wrote:
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.


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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 11 '06 #6
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]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Gabriel Magaña <no*****@no-spam.com> wrote:
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.


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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 11 '06 #7
Gabriel Magana wrote:
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?
No. See later.
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...


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

Jan 11 '06 #8
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?


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
Jan 11 '06 #9
Gabriel Magana <no***@nospam.com> wrote:
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?


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.


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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 11 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by steve | last post: by
15 posts views Thread by Lou | last post: by
3 posts views Thread by Adriano | last post: by
1 post views Thread by DazedAndConfused | last post: by
7 posts views Thread by Martin Robins | last post: by
5 posts views Thread by JoeC | last post: by
reply views Thread by rosydwin | last post: by

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.