How to return the pointer of int32 in C# .NET?

  • Thread starter Thread starter Gravity
  • Start date Start date
G

Gravity

Hi,

I am a bit shocked that C# got nothing to do with C/C++.

Here is what I wanted to do;
Int32 f_nSomeValue = 200; // Supposed to be 4 bytes long in length, right?

MemoryStream f_MemoryStream = new MemoryStream(20);


f_MemoryStream.Write(&f_nSomeValue, 0 , 4);

The first param of the MemoryStream is expecting byte[ ]. I tried to cast it
but the complier would not let me to continue.

Does anyone know how do I convert the int32 so that it is can be used on the
MemoryStrem.Write( )?

Thanks for reading.
 
Gravity said:
Hi,

I am a bit shocked that C# got nothing to do with C/C++.

Here is what I wanted to do;
Int32 f_nSomeValue = 200; // Supposed to be 4 bytes long in length,
right?

MemoryStream f_MemoryStream = new MemoryStream(20);


f_MemoryStream.Write(&f_nSomeValue, 0 , 4);

The first param of the MemoryStream is expecting byte[ ]. I tried to cast
it but the complier would not let me to continue.


Try this:

f_MemoryStream.Write(BitConverter.GetBytes(f_nSomeValue), 0 , 4);


Best regards,

Jeroen Vandezande
 
Thanks for the quick reply. It works.

After learning C#, I hope I do not confuss with my current C/C++. There are
just so much differences.

Jeroen Vandezande said:
Gravity said:
Hi,

I am a bit shocked that C# got nothing to do with C/C++.

Here is what I wanted to do;
Int32 f_nSomeValue = 200; // Supposed to be 4 bytes long in length,
right?

MemoryStream f_MemoryStream = new MemoryStream(20);


f_MemoryStream.Write(&f_nSomeValue, 0 , 4);

The first param of the MemoryStream is expecting byte[ ]. I tried to cast
it but the complier would not let me to continue.


Try this:

f_MemoryStream.Write(BitConverter.GetBytes(f_nSomeValue), 0 , 4);


Best regards,

Jeroen Vandezande
 
Gravity said:
Thanks for the quick reply. It works.

After learning C#, I hope I do not confuss with my current C/C++. There
are just so much differences.

Yeah I know...
I also had to learn it the hard way... I come from delphi.
The trick is: If want to do something.. the chance is that there is a
function inside the .net framework to do it...
If you want to do low level bytemanipulations... take a good look at the
Bitconvertor Class... It's priceless.

Best Regards,

Jeroen Vandezande.
 
Back
Top