integer into binary in c sharp

  • Thread starter Thread starter mei xiao
  • Start date Start date
M

mei xiao

Hi, there,

I want to change an integer into 4 bytes in c sharp, what should I do? Thank
you.

-May
 
Try this


int myint = 2345;

byte [] mybytes = new byte[4];

int i, shift;
for(i = 0, shift = 24; i < 4; i++, shift -= 8)
mybytes = (byte)(0xFF & (myint >> shift));
 
I want to change an integer into 4 bytes in c sharp, what should I do?

Use the shift operators or the BitConverter class.



Mattias
 
mei said:
Hi, there,

I want to change an integer into 4 bytes in c sharp, what should I do? Thank
you.

byte [] myBytes = BitConverter.GetBytes( myInt);
 
mei xiao said:
I want to change an integer into 4 bytes in c sharp, what should I do? Thank
you.

See BitConverter.GetBytes(int)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top