Is there a way to convert int[] to byte[] easily?
I want to write my int[] indexTbl to a MemoryStream but the MemoryStream
class only accepts byte[] buffer. I have more than one int[] indexTbl to
write to the stream so it would be nice if I could just write the whole int[]
as a block and then read it back later when needed.
I thought the Buffer class might be handy here but it seems to be best for
converting byte[] to int[] with Block Copy.
Any suggestions here. I am new to C# and wanted to avoid a for-loop and
convert each int to a byte and then write to the stream one by one. 6 25597
Hi,
One thing you can do is convert each int to bytes and write them in the
stream, youdo so using BitConverter.Ge tBytes
foreach( int i in the_int_Array)
memoryStream1.W rite( BitConverter.Ge tBytes( i);
I really don't remember a better way.
cheers,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Mimi" <Mi**@discussio ns.microsoft.co m> wrote in message
news:90******** *************** ***********@mic rosoft.com... Is there a way to convert int[] to byte[] easily?
I want to write my int[] indexTbl to a MemoryStream but the MemoryStream class only accepts byte[] buffer. I have more than one int[] indexTbl to write to the stream so it would be nice if I could just write the whole int[] as a block and then read it back later when needed.
I thought the Buffer class might be handy here but it seems to be best for converting byte[] to int[] with Block Copy.
Any suggestions here. I am new to C# and wanted to avoid a for-loop and convert each int to a byte and then write to the stream one by one.
"Mimi" <Mi**@discussio ns.microsoft.co m> wrote in message
news:90******** *************** ***********@mic rosoft.com... Is there a way to convert int[] to byte[] easily?
I want to write my int[] indexTbl to a MemoryStream but the MemoryStream class only accepts byte[] buffer. I have more than one int[] indexTbl to write to the stream so it would be nice if I could just write the whole
int[] as a block and then read it back later when needed.
I thought the Buffer class might be handy here but it seems to be best for converting byte[] to int[] with Block Copy.
Any suggestions here. I am new to C# and wanted to avoid a for-loop and convert each int to a byte and then write to the stream one by one.
I would do it this way
int[] ints = new int[3];
ArrayList arrList = new ArrayList();
arrList.AddRang e(ints);
byte[] bytes = (byte[]) arrList.ToArray (typeof(byte));
the ArrayList has a handy ToArray method.
regards Richard
<"Richard Kure" <None>> wrote: I would do it this way
int[] ints = new int[3];
ArrayList arrList = new ArrayList();
arrList.AddRang e(ints); byte[] bytes = (byte[]) arrList.ToArray (typeof(byte)); the ArrayList has a handy ToArray method.
Yes, but it doesn't convert things from one type to another. The above
code gives an InvalidCastExce ption.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Mimi <Mi**@discussio ns.microsoft.co m> wrote: Is there a way to convert int[] to byte[] easily?
I want to write my int[] indexTbl to a MemoryStream but the MemoryStream class only accepts byte[] buffer. I have more than one int[] indexTbl to write to the stream so it would be nice if I could just write the whole int[] as a block and then read it back later when needed.
I thought the Buffer class might be handy here but it seems to be best for converting byte[] to int[] with Block Copy.
Any suggestions here. I am new to C# and wanted to avoid a for-loop and convert each int to a byte and then write to the stream one by one.
I'd suggest you use a BinaryWriter, assuming you want to convert each
int to four bytes. If you need to convert each int to *one* byte,
you'll need to loop through.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
hi,
How the use of a BinaryWriter will avoid the need of a loop?
cheers,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com... Mimi <Mi**@discussio ns.microsoft.co m> wrote: Is there a way to convert int[] to byte[] easily?
I want to write my int[] indexTbl to a MemoryStream but the MemoryStream class only accepts byte[] buffer. I have more than one int[] indexTbl to write to the stream so it would be nice if I could just write the whole int[] as a block and then read it back later when needed.
I thought the Buffer class might be handy here but it seems to be best for converting byte[] to int[] with Block Copy.
Any suggestions here. I am new to C# and wanted to avoid a for-loop and convert each int to a byte and then write to the stream one by one.
I'd suggest you use a BinaryWriter, assuming you want to convert each int to four bytes. If you need to convert each int to *one* byte, you'll need to loop through.
-- Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.mach in AT
dot.state.fl.us >> wrote: How the use of a BinaryWriter will avoid the need of a loop?
You're right, it doesn't. However, you don't need to do anything
particular with each int to convert it to bytes - you can just write
the int directly.
I don't believe that there's any real benefit in avoiding looping here.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Bryan Parkoff |
last post by:
I have two variables: "char A" and "short B". I can be able to convert
from A to B using explicit case conversion with no problem like "B = short
(A);". Right now, I have two variables: "char T" and "short A". T has
an array of six elements. I desire to capture first element and second
element as two bytes into word as short.
The problem is that "A" captures only one element instead of two
elements. I have looked at machine language...
|
by: MuZZy |
last post by:
Hi,
I just wonder if someone can help me wit this -
i have a byte array and need to convert it to short array, creating
short numbers by combining bytes by pairs:
My array: byte, byte, byte, etc.
I need: short = byte+byte, short = byte+byte, etc.
|
by: Allan Ebdrup |
last post by:
How do I easily convert a int to a string?
Kind Regards,
Allan Ebdrup
|
by: dale zhang |
last post by:
Hi,
I am trying to save and read an image from MS Access DB based on the
following article:
http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp
Right now, I saved images without any errors. After reading the ole object
from db, I saved it to C: as file1.bmp and displayed on the web. But it can
not be displayed. After I manually sent the file to wordpad, it shows
|
by: Daniel |
last post by:
I have looked everywhere on the web for an answer to this and the only
thing I can find is converting the image format when the file is
present on the local filesystem. What I want to do is use a web form
to upload a TIFF image (scanned images) and convert it to a JPEG
before I insert it into SQL Server. The file upload part works great,
but I can;t convert the image. I certainly don't want to upload it the
the server filesystem, convert...
| |
by: eking |
last post by:
//Thanks for any help,thank you!лл¡£
public override void FloodFill(Bitmap bmp, Point pt)
{
int ctr=timeGetTime();
//Debug.WriteLine("*******Flood Fill******");
//get the color's int value, and convert it from RGBA to BGRA format (as
GDI+ uses BGRA)
|
by: bbb |
last post by:
Hi,
I need to convert XML files from Japanese encoding to UTF-8.
I was using the following code:
using ( FileStream fs = File.OpenRead(fromFile) )
{
int fileSize = (int)fs.Length;
int buffer = fileSize;
byte b = new byte;
|
by: tshad |
last post by:
I have a value in my sql table set to tinyint (can't set to bit).
I am trying to move it into a boolean field in my program and have tried:
isTrue = (int)dbReader
and
isTrue = Convert.ToBoolean((int)dbReader)
|
by: Debadatta Mishra |
last post by:
Introduction
In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information inside an image, hide your complete image as text ,search for a particular image inside a directory, minimize the size of the image. However this is not a new concept, there is a concept called Steganography which enables to conceal your secret...
|
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 usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |