How to convert an Int32 or an Int to a byte (encoded 8 bytes) ?

He,

How to convert an Int32 or an Int to a byte (encoded 8 bytes) ?

for examle convert 2 (Int32) to a byte 00000001

Best regards


f
0
Goraleye
8/26/2008 3:57:31 PM
asp.net.getting-started 91979 articles. 4 followers. Follow

5 Replies
772 Views

Similar Articles

[PageSpeed] 46
Get it on Google Play
Get it on Apple App Store

Friend, if you want to convert to a byte array, here is the code

Dim arr as byte()
arr = BitConverter.GetBytes(MyInt)

Here MyInt is you Int32 variable

Else you can try this also

http://www.eggheadcafe.com/PrintSearchContent.asp?LINKID=788

Hope it helped you. Good luck!

 


Please Don't forget to click "Mark as Answer" on the post that helped you.
This can be beneficial to other community members reading the thread.
0
cv_vikram
8/26/2008 4:05:15 PM

Convert.ToByte(12)

Convert.ToSomeType(value)

etc...

0
HeartattacK
8/26/2008 4:07:09 PM

Hi,

The byte denotes an integral type that stores values (range from 0-255). Int value can be converted to byte explicitly. For more information, see http://msdn.microsoft.com/en-us/library/5bdb6693(VS.80).aspx

I guess you want to convert it to binary format. If so, we can use ToString method with second parameter with value  2, 8, 10 or 16 for binary, octal, decimal and hexadecimal, respectively. You can try the following code:

         byte b = 255;
         string s = Convert.ToString(b, 2);
         s = String.Format("{0:000000000}", Convert.ToInt32(s));
         Response.Write(s);
       
         Response.Write("<br />");
       
         int i = 1;
         s = Convert.ToString(i, 2);
         s = String.Format("{0:000000000}", Convert.ToInt32(s));
         Response.Write(s);

 

I look forward to hearing from you.


Thomas Sun
Microsoft Online Community Support

Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
0
Thomas
8/29/2008 6:24:13 AM

He,

I I have two integers for example :

  int i = 1;
         si = Convert.ToString(i, 2);
         si = String.Format("{0:000000000}", Convert.ToInt32(si));
         Response.Write(s);

and

  int j = 4;
         sj = Convert.ToString(j, 2);
         sj = String.Format("{0:000000000}", Convert.ToInt32(sj));
         Response.Write(s);

How can we binary add(&)  i and j : i&j ? si&sj does not work

Best regards


f
0
Goraleye
8/29/2008 3:33:06 PM

Hi,

We can convert these int values to byte values and then perform and (&) operation.

For instance:

        string sR;
        int i = 1, j = 4;
        byte a = Convert.ToByte(i);
        byte b = Convert.ToByte(j);
        byte result = Convert.ToByte(a & b);
        sR = Convert.ToString(result, 2);
        sR = String.Format("{0:000000000}", Convert.ToInt32(sR));
        Response.Write(sR);

 I look forward to hearing from you.       


Thomas Sun
Microsoft Online Community Support

Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
0
Thomas
9/1/2008 2:41:47 AM
Reply: