By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,364 Members | 1,652 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,364 IT Pros & Developers. It's quick & easy.

Convert ByteArray to String

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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.