object to a byte[]

P

Pohihihi

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
 
G

Gabriel Magaña

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.
 
G

Guest

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
 
P

Pohihihi

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.
 
P

Pohihihi

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


Peter Bromberg said:
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 said:
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





--
 
J

Jon Skeet [C# MVP]

Gabriel Magaña said:
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.
 
G

Gabriel Magana

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...

Gabriel Magaña said:
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.
 
J

Jon Skeet [C# MVP]

Gabriel said:
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
 
G

Gabriel Magana

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
 
J

Jon Skeet [C# MVP]

Gabriel Magana said:
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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top