473,395 Members | 2,006 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,395 software developers and data experts.

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 4419
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: War Eagle | last post by:
Is it possible to convert an unsigned long to byte array. For instance, unsigned long = 23480923 unsigned long longNumber; longNumber=23480923; // in binary = 1 0110 0110 0100 1010 0101 1011 //...
1
by: Jim | last post by:
Here is a snipet of the code. // State object for receiving data from remote device. public class StateObject { // Client socket. public Socket workSocket = null; // Size of receive buffer....
6
by: Allan Ebdrup | last post by:
How do I easily convert a int to a string? Kind Regards, Allan Ebdrup
6
by: Petar Popara | last post by:
I need some help converting byte into Array: int bufferLen = Convert.ToInt32(file.Length); byte buffer = new byte; int len = sr.Read(buffer, 0, bufferLen); ArrayList a = new ArrayList(len);...
3
by: Asha | last post by:
greetings, i'm having trouble converting the below c# codes into vb.net, the other portion of it is working fine except this, cause i do not know how to convert it. any help provided would be...
1
by: Swarup | last post by:
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...
6
by: as400tips | last post by:
I have a Binary Data file (Packed Decimal and ASCII mixed) and would like to convert into ASCII (readable) file. How to do it in C#? Thanks.
6
by: andremarim | last post by:
Hi, i'm new in java and i wanna know how can i convert a bytearray into a char. Actually I need to take a data from a file (this file is written in a byte code, whose extensio is .dac) and to convert...
4
by: Peter | last post by:
Does anyone know how to convert the following VB6 code to C# code? Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As Any, Source As Any, ByVal bytes As Long) Dim...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
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...
0
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,...

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.