You're here: Snippet Directory » Microsoft .NET » C# (33)
Language:

Binary2Decimal

Language: Deutsch
Programming Language: C#
Published by: Cyron
Last Update: 6/22/2006
Views: 793


Description

Diese Methode rechnet einen achtstelligen Binärwert (in Form eines Strings) in einen Dezimalwert um.
Es findet keine Fehlerbehandlung statt (z.B. ob der Bitstring auch wirklich nur aus Nullen und Einsen besteht). Ich denke das wäre für ein Snippet zu viel und gehört in eine separate Fehlerbehandlungs-Routine.

Code

1 // Konvertiert Binär nach Dezimal 2 public static byte bin2dec(string Bitstring) 3 { 4 char[] inchar = Bitstring.ToCharArray(); 5 byte Bytewert = 0; 6 double Cumulator = 0; 7 for(uint counter = 0; counter < 8; counter++) 8 { 9 if(inchar[counter] == '1') 10 { 11 Cumulator += Math.Pow(2, 7 - counter); 12 } 13 } 14 Bytewert = Convert.ToByte(Cumulator); 15 return Bytewert; 16 }

No comments avaiable

Add a comment

Name *  

Email (won't be displayed) *    

Website  

Comment *  

Sicherheitscode Security Code *    

RSS