473,378 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 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 1894
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ken Layton | last post by:
Attempting to pass a VBScript Array of Bytes to a C# Managed COM object has resulted in some very undesirable and strange behavior. When definining a method in my C# COM class as "void Write(byte...
1
by: steve | last post by:
I need to get byte values from an object into a byte in order to write the bytes to file. An object (vSound) is passed to a function. From the Autos window I can see that the Value of vSound is...
15
by: Lou | last post by:
How do I get the data in the clipbaord fro a registered data type. The code snippet below doesn't work? But the VB6 example does. What am I doing wrong. private void lstItems_DragDrop(object...
3
by: Adriano | last post by:
Hello, when I try to print something, either DataGrid or from Crystal Report viever the folowing error message appears and cancels printing: Object reference not set to an instance of an...
4
by: DazedAndConfused | last post by:
I encryted a serialized binary formatted object. Now I can't figure out how to deserialize it so that I can decrypt it. I used this code encrypt and write it out: Dim fe As New...
1
by: DazedAndConfused | last post by:
Can you encrpt a serialized object? Or am I trying to do something that just doesn't work that way? I am trying to encrypt a serialized object. I can read and write the object to a file...
7
by: Martin Robins | last post by:
I am currently looking to be able to read information from Active Directory into a data warehouse using a C# solution. I have been able to access the active directory, and I have been able to return...
5
by: JoeC | last post by:
I am still working in my maze game and I am making improvments. The main sticking problem that I have is passing my graphic library. This probram uses windows apis but my question has nothing to...
0
by: zman77 | last post by:
EDIT: -- forgot to mention... I am using Visual Studio 2005, on Win XP, on an intel machine Hi. This is my first post, though I've "lurked" for a while because I find these forums very helpful....
1
by: miller.brettm | last post by:
Hi, I'm working with an ActiveX serial component that fires an event when data is received. I need to continue to use this component because it contains the ability to do Xmodem1k an d XmodemCRC...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.