Re: convert int[] to byte[]



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]" <skeet@xxxxxxxxx> wrote in message
news:MPG.1cc4cb985320a85c98bfa4@xxxxxxxxxxxxxxxxxxxxxxx
> Mimi <Mimi@xxxxxxxxxxxxxxxxxxxxxxxxx> 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 - <skeet@xxxxxxxxx>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too


.



Relevant Pages

  • Re: more hand written integer pow() functions (LONG POST)
    ... > trying to avoid implementation defined behavior. ... static int ibmpow ... I tested by replacing the test loop ... And the ibmpow() version is still faster than pow_b. ...
    (comp.lang.c)
  • Re: convert int[] to byte[]
    ... > How the use of a BinaryWriter will avoid the need of a loop? ... Prev by Date: ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: faster solution
    ... The following piece of code (a search algorithm) has to be evaluated ... int search ... xi and xppare independent of the while loop above. ... You can avoid the while loop completely for the cases where either of ...
    (comp.lang.c)
  • Re: How to set up a global variable in a sub-routine?
    ... the 'global variables' are more like constants ... ... single script, I need a way to tell it only once where the file is and ... So, if you construct a loop with a loop variable, you ... >programmers avoid global variables completely. ...
    (perl.beginners)
  • Re: Is .NET good for high volume numerical computing?
    ... of changing the decision tree structure later. ... > into one managed class and call it through a managed class wrapper. ... > But try to avoid managed/unmanaged transitions, in the performance loop. ...
    (microsoft.public.dotnet.general)