user34537
user34537

Reputation:

int[] to byte[], am i forgetting something?

This is untested as i need to write more code. But is this correct and i feel like i am missing something, like this could be better written. Do i need the c.lose at the end? should i flush anything(i'll assume no if i do close())?

Byte[] buffer;
using (var m = new MemoryStream())
{
    using (var binWriter = new BinaryWriter(m))
    {
        foreach (var v in wordIDs)
            binWriter.Write(v);
        binWriter.Close();
    }
    buffer = m.GetBuffer();
    m.Close();
}

Upvotes: 2

Views: 322

Answers (6)

blowdart
blowdart

Reputation: 56500

I disagree with the Skeet here.

Whilst you may not need close by using using you are relying on the implementation of BinaryWriter and MemoryStream to do it for you in the Dispose method. This is true for framework types, but what if someone writes a Writer or Stream which doesn't do it?

Adding close does no harm and protects you against badly written classes.

Upvotes: 0

Samuel
Samuel

Reputation: 38346

JaredPar's and Jonathan's answers are correct. If you want an alternative, you use BitConverter.GetBytes(int). So now your code turns into this

wordIDs.SelectMany(i => BitConverter.GetBytes(i));

Upvotes: 0

user7116
user7116

Reputation: 64068

What is wordIDs, is it an enumeration or is it an Int32[]? You can use the following if it is just Int32[]:

byte[] bytes = new byte[wordIDs.Length * 4];
Buffer.BlockCopy(wordIDs, 0, bytes, 0, bytes.Length);

Otherwise, if wordIDs is an enumeration that you must step through, all you need to change is remove the m.Close (as mentioned) and use MemoryStream.ToArray (as mentioned).

Upvotes: 2

RossFabricant
RossFabricant

Reputation: 12492

Thus sayeth Skeet:

There's no real need to close either a MemoryStream or a BinaryWriter, but I think it's good form to use a using statement to dispose of both - that way if you change at a later date to use something that really does need disposing, it will fit into the same code.

So you don't need the Close or the using statement, but using is idiomatic C#.

Upvotes: 0

Jonathan Rupp
Jonathan Rupp

Reputation: 15762

You don't need the .Close() calls (the automatic .Dispose() the using block generates takes care of those).

Also, you'll want to use .ToArray() on the MemoryStream, not .GetBuffer(). GetBuffer() returns the underlying buffer, no matter how much of it is used. ToArray() returns a copy that is the perfect length.

If you're using this to communicate with another program, make sure you and it agree on the order of the bytes (aka endianness). If you're using network byte-order, you'll need to flip the order of the bytes (using something like IPAddress.HostToNetworkOrder()), as network byte-order is big-endian, and BinaryWriter uses little-endian.

Upvotes: 4

JaredPar
JaredPar

Reputation: 754575

Close is not needed here. The using statements will ensure the Dispose method on these types are called on exit and this will have the same effect as calling Close. In fact if you look at the code in reflector, you'll find that Close in both cases just proxies off to the Dispose method on both types.

Upvotes: 0

Related Questions