Home > Net >  C # how to convert an int into a binary array?
C # how to convert an int into a binary array?

Time:04-14

Int=3, for example, how to put it into a 16-bit array,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1 {0}
Is there a wrapper function directly?

CodePudding user response:

Public static byte [] IntArrToByteArr (int [] intArr)
{
Int intSize=sizeof (int) * intArr in Length;
Byte [] bytArr=new byte [intSize];
//apply for a block of unmanaged memory
IntPtr PTR=Marshal. AllocHGlobal (intSize);
//copy int array to the memory block
Marshal. Copy (intArr, 0, PTR, intArr. Length);
//copy to byte array
Marshal. Copy (PTR, bytArr, 0, bytArr. Length);
//release application of unmanaged memory
Marshal. FreeHGlobal (PTR);
Return bytArr;
}

CodePudding user response:

 static async Task Main (string [] args) 
{


Int I=3;
Var res=GetBit16Low (I). ToList ();

}


Public static IEnumerable GetBit16Low (int value)
{
Short I=(short) value;
BitArray BitArray=new BitArray (BitConverter GetBytes (I));
For (int j=bitArray Length - 1. J & gt;=0; J -)
{
Yield return the Convert. ToInt32 (bitArray [j]);
}
}

CodePudding user response:

The above is the use of Microsoft's existing function

If you want to dazzle head moving wheels, moves to the right to add a judge
 public static IEnumerable GetBit16Low1 (int value) 
{
Int t=1;
For (int I=15; I & gt;=0; I -)
{
Var t1=1 & lt; If (value | (t1)==value)
Yield return 1;
The else
{
Yield return 0;
}
}
}

CodePudding user response:

Below the post something of the sales, of course, the following two you look pure, don't take it seriously, just pure skills, rather than technology
1. Direct linq
Var b=Enumerable. Range (0, 16). The Select (p=& gt; ((1 & lt; <(15 - p) | value)==value)? 1:0);

2. To the third floor, the average person a see don't understand the
Public static IEnumerable GetBit16Low1 (int value)
{
For (int I=15, t=1 & lt; 1)
{
Int r=value | (t)==value? 1:0;
Yield return r;
}
}

CodePudding user response:

reference 4 floor wanghui0380 response:
below is sticking point of sales, of course, the following two you look pure, don't take it seriously, just pure skills, rather than technology
1. Direct linq
Var b=Enumerable. Range (0, 16). The Select (p=& gt; ((1 & lt; <(15 - p) | value)==value)? 1:0);

2. To the third floor, the average person a see don't understand the
Public static IEnumerable GetBit16Low1 (int value)
{
For (int I=15, t=1 & lt; 1)
{
Int r=value | (t)==value? 1:0;
Yield return r;
}
}

This looked at meng force...

CodePudding user response:

Can change to more dizzy

 public static IEnumerable GetBit16Low1 (int value) 
{
For (int I=15, t=1 & lt; Value | 1, r=(t)==value? 1:0)
Yield return r;

}


Under normal circumstances, of course, you must write in this way, estimation is to be scolded, anyway, I want to see the codereview I don't allow, I have to get people to shout into the little black house let him explain exactly want to do, can't explain, buckle performance cannot be run

CodePudding user response:

By the way, is to use BitConverter

CodePudding user response:

 int [] arr=the Convert. ToString (3, 2) 
PadLeft (Marshal. SizeOf (a) * 4, '0')
. ToCharArray ()
The Select (ss=& gt; Int. Parse (ss. The ToString ()))
The ToArray ();
  •  Tags:  
  • C#
  • Related