473,699 Members | 2,387 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1906
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.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:
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******** ******@TK2MSFTN GP10.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*******@yaho o.nospammin.com > wrote in message
news:BE******** *************** ***********@mic rosoft.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.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:
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.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
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
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.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
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.c om> 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.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
Jan 11 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
13431
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 bBytes)" or "void Write(byte bBytes, int nLen)" or "int Write(byte bBytes, int nLen)"VBScript returns a "Invalid procedure call or argument" exception. However when I define the method as "int Write(byte bBytes)" it works just fine. Unfortunately...
1
25655
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 {System.Array} and the Type is {System.Object}. vSound has no properties or methods that allow you to get access to the byte values. When I type ?vSound
15
5078
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 sender, System.Windows.Forms.DragEventArgs e) {
3
2437
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 object :(((( Anyone pls help me to solve this problem!!! thanks in advance,
4
1410
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 MortgageFileWriter.FileEncrypt Dim myBuffer As New IO.MemoryStream Dim OutBuffer As New IO.MemoryStream
1
3216
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 without a problem(IF I don't encrypt it). The encryption routine I am using works great when I am just reading in text and writing out encrypted data/Reading in encrpted data and writing out decrypted text.
7
11942
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 "DirectoryEntry" objects within the path that I specify (either using the DirectoryEnrtry.Children or using the DirectorySearcher class) and all started well and dandy! Now the problem; some of the properties of the DirectoryEntry objects being...
5
1894
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 do with those. So please dont jump on me for that. I am making this graphics library and my bitmap data is stored in an array. This library works some time and when I have to use the copy constructor it dosn't seem to work. I did my best to...
0
2040
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. Ok my problem is the following. I have a class that contains a "MakeByteArray" function. I have many objects of that class. Inside that function, I have a private variable, that is NOT static. It seems that when I put all these objects in...
1
3949
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 comms The RX even fires and passes an Object (Type: Object) parameter. When looking @ the Locals window it shows the Type as: Object { byte } <Code>
0
9197
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9054
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8941
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8897
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7785
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6549
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3071
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.