473,386 Members | 1,745 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,386 software developers and data experts.

Converting from Byte Arrays to Streams and Back

I just wanted to know if I am converting to/from streams the easiest and
correct way. I am performing the following statements to convert byte
arrays to and from streams during different processing in my application:

Here is a sample where I convert from a stream to a byte array:

strm = mqMsg.BodyStream;
BinaryReader br = new BinaryReader(strm);
byteArray= br.ReadBytes(Convert.ToInt32(strm.Length));

Here is a sample where I convert from a byte array to a stream:

MemoryStream memStream = new MemoryStream();
BinaryWriter binWriter = new BinaryWriter(memStream);
binWriter.Write(byteArray);
Thanks,

Matt

Nov 16 '05 #1
2 7278
Matt,

That's pretty much how I would do it if I needed the complete array.
The only change I would make is that I would pass the byte array to the
constructor of the memory stream (since you are just reading and writing
bytes, right?). That would eliminate one line of code (which is always a
good thing).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Matt" <md*****@sorvive.DONT-SEND-SPAM.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
I just wanted to know if I am converting to/from streams the easiest and
correct way. I am performing the following statements to convert byte
arrays to and from streams during different processing in my application:

Here is a sample where I convert from a stream to a byte array:

strm = mqMsg.BodyStream;
BinaryReader br = new BinaryReader(strm);
byteArray= br.ReadBytes(Convert.ToInt32(strm.Length));

Here is a sample where I convert from a byte array to a stream:

MemoryStream memStream = new MemoryStream();
BinaryWriter binWriter = new BinaryWriter(memStream);
binWriter.Write(byteArray);
Thanks,

Matt


Nov 16 '05 #2
Matt <md*****@sorvive.DONT-SEND-SPAM.com> wrote:
I just wanted to know if I am converting to/from streams the easiest and
correct way. I am performing the following statements to convert byte
arrays to and from streams during different processing in my application:

Here is a sample where I convert from a stream to a byte array:

strm = mqMsg.BodyStream;
BinaryReader br = new BinaryReader(strm);
byteArray= br.ReadBytes(Convert.ToInt32(strm.Length));
Don't forget that Length isn't available on all streams. I would
personally use a method like the one I've got at the bottom of
http://www.yoda.arachsys.com/csharp/readbinary.html

That's basically what BinaryReader is going to be doing anyway, and
there's no need to create a new object (the BinaryReader) to do that :)
Here is a sample where I convert from a byte array to a stream:

MemoryStream memStream = new MemoryStream();
BinaryWriter binWriter = new BinaryWriter(memStream);
binWriter.Write(byteArray);


Why bother with the BinaryReader or BinaryWriter, out of interest? For
the latter example, just use:

MemoryStream memStream = new MemoryStream();
memStream.Write(byteArray, 0, byteArray.Length);

That's assuming you want them to be independent copies - you can make a
memory stream backed by a byte array using one of the MemoryStream
constructors.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3

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

Similar topics

5
by: matt melton | last post by:
Hi there, I am trying to write a method that accepts an array of any primitive type and will return the same array without copying memory as an array of bytes. ie. I'd like to be able to do...
4
by: Hal Vaughan | last post by:
If I have a byte and I convert it to string (String sData = new String(byte bData), then convert it back (byte bData = sData.getBytes()), will all data be intact, or do Strings have problems with...
22
by: Keith MacDonald | last post by:
Hello, Is there a portable (at least for VC.Net and g++) method to convert text between wchar_t and char, using the standard library? I may have missed something obvious, but the section on...
11
by: Peter | last post by:
Hi how can I compare two byte arrays in VB.NET Thank Peter
3
by: Pete Davis | last post by:
I've never done this in C# so I don't know what the appropriate way of doing it is. I've got an array of bytes and I need to convert the array into "usable" data. For example, the first 4 bytes...
4
by: movieknight | last post by:
Hi, I have an application that stores raw .wav files (and also jpgs/bitmaps) within strings, and I need to sometimes convert these strings to byte arrays, and sometimes go from byte arrays back...
7
by: james | last post by:
Hi, I am trying to insert a record into a database table, one field of which is a byte array. Using the below: Byte imgArr; .... <code to put image into imgArr>
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
2
by: shahiz | last post by:
basically im having null pointer exception //read an inputstream is = new DataInputStream(new FileInputStream("test.mpg")); loadBytes(is); //pass it as a datasource for the player public...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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,...

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.