2009-09-27 4 views

Répondre

-1

Je parie qu'il ya une habile façon de le faire, mais il fonctionne:

private string byteToBitsString(byte byteIn) 
{ 
    char[] bits = new char[8]; 
    bits(0) = Convert.ToString((byteIn/128) % 2); 
    bits(1) = Convert.ToString((byteIn/64) % 2); 
    bits(2) = Convert.ToString((byteIn/32) % 2); 
    bits(3) = Convert.ToString((byteIn/16) % 2); 
    bits(4) = Convert.ToString((byteIn/8) % 2); 
    bits(5) = Convert.ToString((byteIn/4) % 2); 
    bits(6) = Convert.ToString((byteIn/2) % 2); 
    bits(7) = Convert.ToString((byteIn/1) % 2); 
    return bits; 
} 
+2

Voir l'autre réponse en utilisant Convert.ToString (value, 2) .PadLeft (8, '0') à la place. –

21

Jetez un oeil à Convert.ToString(). Vous pouvez l'utiliser dans les deux sens, pour la conversion octet-> chaîne de bits et vice versa.

byte value = 56; 
// There ... 
string bits = Convert.ToString(value, 2); 
// ...and Back Again 
value = Convert.ToByte(bits, 2); 
+2

Cette réponse devrait être la réponse acceptée. Lorsqu'on lui donne un seul argument, Convert.ToString (byte) renvoie une chaîne hexadécimale, mais lorsqu'il reçoit le second argument, Convert.ToString (byte, int) peut utiliser la base 2, 8, 10 ou 16. '' 'Convertir. ToString (56,2) '' 'renvoie' '' '" 111000 "' '' mais si vous voulez * all * les bits, alors '' 'Convert.ToString (56,2) .PadLeft (8, '0') '' 'renvoie' '' "00111000" '' ' –

2

Voici la méthode écrite de re-de clweek, qui fonctionne réellement:
Je l'ai utilisé StringBuilder classe au lieu de tableau de caractères .

Exemples: byteToBitsString(157) imprime "00101111"

private string byteToBitsString(byte byteIn) 
    { 
     var bitsString = new StringBuilder(8); 

     bitsString.Append(Convert.ToString((byteIn/128) % 2)); 
     bitsString.Append(Convert.ToString((byteIn/64) % 2)); 
     bitsString.Append(Convert.ToString((byteIn/32) % 2)); 
     bitsString.Append(Convert.ToString((byteIn/16) % 2)); 
     bitsString.Append(Convert.ToString((byteIn/8) % 2)); 
     bitsString.Append(Convert.ToString((byteIn/4) % 2)); 
     bitsString.Append(Convert.ToString((byteIn/2) % 2)); 
     bitsString.Append(Convert.ToString((byteIn/1) % 2)); 

     return bitsString.ToString(); 
    } 
+0

A été buggé? Je l'ai converti à partir de VB et n'utilise pas C# régulièrement, donc il y a de la place pour une erreur stupide, mais qu'est-ce que c'est? – clweeks

0

Voici un peu façon cognant de le faire:

public static string ByteToBinaryString(byte byteIn) 
{ 
    StringBuilder out_string = new StringBuilder(); 
    byte mask = 128; 
    for (int i = 7; i >=0 ; --i) 
    { 
     out_string.Append((byteIn & mask) != 0 ? "1" : "0"); 
     mask >>= 1; 
    } 
    return out_string.ToString(); 
} 
0

Ou la façon dont la variante de luxe du bit cognant à le faire:

public static string ToByteFormat(int valIn, int digits) 
    { 
     var bitsString = new StringBuilder(digits); 
     int mask = (1 << digits - 1); 
     for(int i = 0; i < digits; i++) 
     { 
      bitsString.Append((valIn & mask) != 0 ? "1" : "0"); 
      mask >>= 1; 
     } 
     return bitsString.ToString(); 
    } 
Questions connexes