Search Unity

Convert numbers in base 2

Discussion in 'Scripting' started by ARares, May 28, 2017.

  1. ARares

    ARares

    Joined:
    Mar 18, 2016
    Posts:
    167
    i have this code to convert a number in base 2, but if i want to convert 2 i get 10, and should be 00000010
    i think, so ho i can add for number in 1 Byte i made this, but for the rest?
    Code (CSharp):
    1.                 string binary = Convert.ToString(value, 2);
    2.  
    3.                 if (binary.Length < 8)
    4.                 {
    5.                     int test2 = 8 - binary.Length;
    6.  
    7.                     if (test2 == 7)
    8.                     {
    9.                         await ReplyAsync("0000000" + binary);
    10.                     }
    11.                     else if (test2 == 6)
    12.                     {
    13.                         await ReplyAsync("000000" + binary);
    14.                     }
    15.                     else if (test2 == 5)
    16.                     {
    17.                         await ReplyAsync("00000" + binary);
    18.                     }
    19.                     else if (test2 == 4)
    20.                     {
    21.                         await ReplyAsync("0000" + binary);
    22.                     }
    23.                     else if (test2 == 3)
    24.                     {
    25.                         await ReplyAsync("000" + binary);
    26.                     }
    27.                     else if (test2 == 2)
    28.                     {
    29.                         await ReplyAsync("00" + binary);
    30.                     }
    31.                     else if (test2 == 1)
    32.                     {
    33.                         await ReplyAsync("0" + binary);
    34.                     }
    35.                 }
    36.                 else if (binary.Length == 8)
    37.                 {
    38.                     await ReplyAsync(binary);
    39.                 }
    40.                 else if (binary.Length > 8)
    41.                 {
    42.                     await ReplyAsync(binary);
    43.                 }
     
  2. cstooch

    cstooch

    Joined:
    Apr 16, 2014
    Posts:
    354
    What about just simply this:

    string binary = System.Convert.ToString(value, 2).PadLeft(8,'0');

    That will convert to binary, then left pad with 0's to get up to 8 characters. Of course this will only work up to 255 though.. not sure what your exact needs are.
     
    ARares likes this.
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    base 2 does not necessarily have the padded zero's. People just often write binary in words of 8 digits, because it's often done so in the context of memory, where 1 byte (8 bits) is often the smallest word size.

    @cstooch has it right on the money as well if you want it.
     
    ARares likes this.
  4. ARares

    ARares

    Joined:
    Mar 18, 2016
    Posts:
    167
    Thnaks, for help, at code i have this:

    Code (CSharp):
    1.                 if(value <= 255)
    2.                 {
    3.                     string binary = Convert.ToString(value, 2).PadLeft(8, '0');
    4.                     await ReplyAsync(binary);
    5.                 }
    6.                 else if (value <= 65535)
    7.                 {
    8.                     string binary = Convert.ToString(value, 2).PadLeft(16, '0');
    9.                     await ReplyAsync(binary);
    10.                 }
    11.                 else if (value <= 16777215)
    12.                 {
    13.                     string binary = Convert.ToString(value, 2).PadLeft(24, '0');
    14.                     await ReplyAsync(binary);
    15.                 }
     
    Last edited: May 29, 2017