Have you ever used Buffer and/or BitConverter?
-
The last line of code below crashes my program. I am stumped as to why it's happening. I've double and triple checked the definitions for the BlockCopy and GetBytes functions and I still can't work out what I'm doing wrong. Any insight would be appreciated.
namespace ConsoleApplication1 { class Class1 { [STAThread] static void Main(string[] args) { int int1 = 3; int int2 = 1000; byte[] b = new byte[16]; Buffer.BlockCopy(BitConverter.GetBytes(int1), 0, b, 0, 8); //the following line crashes the program Buffer.BlockCopy(BitConverter.GetBytes(int2), 0, b, 12, 4); } } }
NATHAN RIDLEY Web Application Developer email: nathan @ netlab.com.au [remove the spaces before and after the @ symbol]
-
The last line of code below crashes my program. I am stumped as to why it's happening. I've double and triple checked the definitions for the BlockCopy and GetBytes functions and I still can't work out what I'm doing wrong. Any insight would be appreciated.
namespace ConsoleApplication1 { class Class1 { [STAThread] static void Main(string[] args) { int int1 = 3; int int2 = 1000; byte[] b = new byte[16]; Buffer.BlockCopy(BitConverter.GetBytes(int1), 0, b, 0, 8); //the following line crashes the program Buffer.BlockCopy(BitConverter.GetBytes(int2), 0, b, 12, 4); } } }
NATHAN RIDLEY Web Application Developer email: nathan @ netlab.com.au [remove the spaces before and after the @ symbol]
Try the following:
using System;
namespace ConsoleApplication1
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
int int1 = 3;
int int2 = 1000;
byte[] b = new byte[16];
byte[] b2 = BitConverter.GetBytes(int1);
byte[] b3 = BitConverter.GetBytes(int2);
Buffer.BlockCopy(b2, 0, b, 0, b2.Length);
Buffer.BlockCopy(b3, 0, b, 12, 4);
}
}
}- Nick Parker
My Blog | My Articles -
The last line of code below crashes my program. I am stumped as to why it's happening. I've double and triple checked the definitions for the BlockCopy and GetBytes functions and I still can't work out what I'm doing wrong. Any insight would be appreciated.
namespace ConsoleApplication1 { class Class1 { [STAThread] static void Main(string[] args) { int int1 = 3; int int2 = 1000; byte[] b = new byte[16]; Buffer.BlockCopy(BitConverter.GetBytes(int1), 0, b, 0, 8); //the following line crashes the program Buffer.BlockCopy(BitConverter.GetBytes(int2), 0, b, 12, 4); } } }
NATHAN RIDLEY Web Application Developer email: nathan @ netlab.com.au [remove the spaces before and after the @ symbol]
It's a weird problem, eh? The length of both arrays returned from GetBytes() is 4, so I think that it's actually the second-to-last line that's causing the problem with its length of 8. That code must be badly written in the current version of .NET: the second-to-last line above leaves the static stuff in Buffer somehow in an incorrect state, which is exposed by the subsequent call. Sorry I can't be of more help. Regards, Jeff Varszegi