473,581 Members | 2,514 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Uni codeEncoding enc = new System.Text.Uni codeEncoding();
now i am using enc.GetString(v alue) 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 4462
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.Un icodeEncoding enc = new System.Text.Uni codeEncoding();
now i am using enc.GetString(v alue) 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.ToBase6 4String(myData) ;

byte[] decodedData = Convert.FromBas e64String(encod edData);

// 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
16971
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 // so i want byteArray=0000 0001 , byteArray=0110 0110 , // byteArray=0100 1010, byteArray=0101 1011. Is there a way to get the 4 bytes that make...
1
1605
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. public const int BufferSize = 256; // Receive buffer.
6
12750
by: Allan Ebdrup | last post by:
How do I easily convert a int to a string? Kind Regards, Allan Ebdrup
6
17319
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); a.Add(buffer); Array result = a.toArray();
3
1421
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 great at this stage. C# code new Bitmap(new MemoryStream((byte)serializerXML.Deserialize(readerText))); yours truly
1
480
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 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...
6
28056
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
2901
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 this data into a char. Please help me, and I would like to ask to explain me step by step all the process, including if i will need to put any...
4
4645
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 sngArray(0) As Single strString = Chr$(107) & Chr$(62) & Chr(139) & Chr$(65) CopyMemory sngArray(0), ByVal strString, Len(strString) After running...
0
7873
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7800
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8309
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...
1
7902
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...
0
8178
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...
1
5679
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...
0
5365
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3830
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1405
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.