Convert an integer to a binary string with leading zeros

前端 未结 6 2088
醉酒成梦
醉酒成梦 2021-02-05 01:24

I need to convert int to bin and with extra bits.

string aaa = Convert.ToString(3, 2);

it returns 11, but I need 0011

相关标签:
6条回答
  • 2021-02-05 01:52

    I've created a method to dynamically write leading zeroes

    public static string ToBinary(int myValue)
    {
          string binVal = Convert.ToString(myValue, 2);
          int bits = 0;
          int bitblock = 4;
    
          for (int i = 0; i < binVal.Length; i = i + bitblock)
          { bits += bitblock; }
    
          return binVal.PadLeft(bits, '0');
    }
    

    At first we convert my value to binary. Initializing the bits to set the length for binary output. One Bitblock has 4 Digits. In for-loop we check the length of our converted binary value und adds the "bits" for the length for binary output.

    Examples: Input: 1 -> 0001; Input: 127 -> 01111111 etc....

    0 讨论(0)
  • 2021-02-05 02:03
    string aaa = Convert.ToString(3, 2).PadLeft(10, '0');
    
    0 讨论(0)
  • 2021-02-05 02:05

    11 is binary representation of 3. The binary representation of this value is 2 bits.

    3 = 20 * 1 + 21 * 1

    You can use String.PadLeft(Int, Char) method to add these zeros.

    Convert.ToString(3, 2).PadLeft(4, '0') // 0011
    Convert.ToString(3, 2).PadLeft(8, '0') // 00000011
    
    0 讨论(0)
  • 2021-02-05 02:07
    public static String HexToBinString(this String value)
    {
            String binaryString = Convert.ToString(Convert.ToInt32(value, 16), 2);
            Int32 zeroCount = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(binaryString.Length) / 8)) * 8;
    
            return binaryString.PadLeft(zeroCount, '0');
    }
    
    0 讨论(0)
  • 2021-02-05 02:13

    You can use these methods:

    public static class BinaryExt
    {
        public static string ToBinary(this int number, int bitsLength = 32)
        {
            return NumberToBinary(number, bitsLength);
        }
    
        public static string NumberToBinary(int number, int bitsLength = 32)
        {
            string result = Convert.ToString(number, 2).PadLeft(bitsLength, '0');
    
            return result;
        }
    
        public static int FromBinaryToInt(this string binary)
        {   
            return BinaryToInt(binary);
        }
    
        public static int BinaryToInt(string binary)
        {   
            return Convert.ToInt32(binary, 2);
        }   
    }
    

    Sample:

    int number = 3; 
    string byte3 = number.ToBinary(8); // output: 00000011
    
    string bits32 = BinaryExt.NumberToBinary(3); // output: 00000000000000000000000000000011
    
    0 讨论(0)
  • 2021-02-05 02:17

    Just what Soner answered use:

    Convert.ToString(3, 2).PadLeft(4, '0') 
    

    Just want to add just for you to know. The int parameter is the total number of characters that your string and the char parameter is the character that will be added to fill the lacking space in your string. In your example, you want the output 0011 which which is 4 characters and needs 0's thus you use 4 as int param and '0' in char.

    0 讨论(0)
提交回复
热议问题