469,646 Members | 1,339 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,646 developers. It's quick & easy.

Convert ByteArray to String

I am reading a file (txt, xml, gif, ico, bmp etc) byte by byte and filling
it into a byte arry. Now i have to convert it into a string to store it in
the database.

I use
System.Text.UnicodeEncoding enc = new System.Text.UnicodeEncoding();
now i am using enc.GetString(value) and the value retured is one byte less
if the size of the byte array is Odd. In case of files having even number
of bytes, the convertion is happening correctly and everything is fine.

Can somebody help me overcome this odd problem i am facing? (The encoding
must be UnicodeEncoding only. If i use the UTF7 Encoding, the one byte is
not getting lost but the data retrieved does not display the gif and such
files properly. Only the text files are displayed correctly)

Kindly help. Thank you.
Arvind

Sep 1 '05 #1
1 4001
Swarup wrote:
I am reading a file (txt, xml, gif, ico, bmp etc) byte by byte and filling
it into a byte arry. Now i have to convert it into a string to store it in
the database.

I use
System.Text.UnicodeEncoding enc = new System.Text.UnicodeEncoding();
now i am using enc.GetString(value) and the value retured is one byte less
if the size of the byte array is Odd. In case of files having even number
of bytes, the convertion is happening correctly and everything is fine.

Can somebody help me overcome this odd problem i am facing? (The encoding
must be UnicodeEncoding only. If i use the UTF7 Encoding, the one byte is
not getting lost but the data retrieved does not display the gif and such
files properly. Only the text files are displayed correctly)


You can't do this this way. If you try interpreting an arbitrary array of
bytes as a Unicode string, you won't end up with a representation that's
reversible - so you'll never be able to convert the resulting string back
into the image, for example.

First, you should probably consider using a blob field in the database
instead of the string. If you can't do that, you need to convert the
binary data into a string representation that can hold all the information
and that can be converted back into the same binary data later on. The
best way to do that is to make use of a standard algorithm: base64 is
readily available in .NET.

byte[] myData;
// Fill your byte array
string encodedData = Convert.ToBase64String(myData);

byte[] decodedData = Convert.FromBase64String(encodedData);

// at this point, myData and encodedData will hold the same bytes.

Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
Sep 1 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Allan Ebdrup | last post: by
6 posts views Thread by Petar Popara | last post: by
3 posts views Thread by Asha | last post: by
1 post views Thread by Swarup | last post: by
6 posts views Thread by as400tips | last post: by
4 posts views Thread by Peter | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.