Sprache: C#
Konvertiert folgende Formate DEZ/HEX/OKT/BIN
int FromHex(string s) {
int res = 0;
foreach (char ch in s) {
if ('0' <= ch && ch <= '9') res = 16 * res + (ch - '0');
else if ('a' <= ch && ch <= 'f') res = 16 * res + (ch - 'a' + 10);
else if ('A' <= ch && ch <= 'A') res = 16 * res + (ch - 'A' + 10);
else throw new FormatException();
}
return res;
}
//----- convert decimal string to int
int FromDec(string s) {
int res = 0;
foreach (char ch in s) {
if ('0' <= ch && ch <= '9') res = 10 * res + (ch - '0');
else throw new FormatException();
}
return res;
}
//----- convert octal string to int
int FromOct(string s) {
int res = 0;
foreach (char ch in s) {
if ('0' <= ch && ch <= '7') res = 8 * res + (ch - '0');
else throw new FormatException();
}
return res;
}
//----- convert binary string to int
int FromBin(string s) {
int res = 0;
foreach (char ch in s) {
if (ch == '0') res = 2 * res;
else if (ch == '1') res = 2 * res + 1;
else throw new FormatException();
}
return res;
}
//----- parse and return the value of the TextBox val
int Value() {
string s = val.Text;
if (s.Length == 0) throw new FormatException();
else if (s.Length > 2 && s[0] == '0') {
if (s[1] == 'x') return FromHex(s.Substring(2));
else if (s[1] == 'o') return FromOct(s.Substring(2));
else if (s[1] == 'b') return FromBin(s.Substring(2));
}
return FromDec(s);
}
//----- convert n to numeric string of base b
string ToNumeric(int n, int b) {
StringBuilder buf = new StringBuilder();
do {
buf.Append((char)(n % b + '0'));
n = n / b;
} while (n != 0);
StringBuilder s = new StringBuilder();
for (int i = buf.Length - 1; i >= 0; i--) s.Append(buf[i]);
return s.ToString();
}
//-------------- event handlers ---------------------
//---- Convert TextBox val to hexadecimal format
public void ToHex(object sender, EventArgs e) {
try {
val.Text = "0x" + Value().ToString("x8");
error.Text = "";
} catch (FormatException) {
error.Text = errMsg;
}
}
//---- Convert TextBox val to decimal format
public void ToDec(object sender, EventArgs e) {
try {
val.Text = Value().ToString();
error.Text = "";
} catch (FormatException) {
error.Text = errMsg;
}
}
//---- Convert TextBox val to octal format
public void ToOct(object sender, EventArgs e) {
try {
val.Text = "0o" + ToNumeric(Value(), 8);
error.Text = "";
} catch (FormatException) {
error.Text = errMsg;
}
}
//---- Convert TextBox val to binary format
public void ToBin(object sender, EventArgs e) {
try {
val.Text = "0b" + ToNumeric(Value(), 2);
error.Text = "";
} catch (FormatException) {
error.Text = errMsg;
}
}
}
}
int FromHex(string s) {
int res = 0;
foreach (char ch in s) {
if ('0' <= ch && ch <= '9') res = 16 * res + (ch - '0');
else if ('a' <= ch && ch <= 'f') res = 16 * res + (ch - 'a' + 10);
else if ('A' <= ch && ch <= 'A') res = 16 * res + (ch - 'A' + 10);
else throw new FormatException();
}
return res;
}
//----- convert decimal string to int
int FromDec(string s) {
int res = 0;
foreach (char ch in s) {
if ('0' <= ch && ch <= '9') res = 10 * res + (ch - '0');
else throw new FormatException();
}
return res;
}
//----- convert octal string to int
int FromOct(string s) {
int res = 0;
foreach (char ch in s) {
if ('0' <= ch && ch <= '7') res = 8 * res + (ch - '0');
else throw new FormatException();
}
return res;
}
//----- convert binary string to int
int FromBin(string s) {
int res = 0;
foreach (char ch in s) {
if (ch == '0') res = 2 * res;
else if (ch == '1') res = 2 * res + 1;
else throw new FormatException();
}
return res;
}
//----- parse and return the value of the TextBox val
int Value() {
string s = val.Text;
if (s.Length == 0) throw new FormatException();
else if (s.Length > 2 && s[0] == '0') {
if (s[1] == 'x') return FromHex(s.Substring(2));
else if (s[1] == 'o') return FromOct(s.Substring(2));
else if (s[1] == 'b') return FromBin(s.Substring(2));
}
return FromDec(s);
}
//----- convert n to numeric string of base b
string ToNumeric(int n, int b) {
StringBuilder buf = new StringBuilder();
do {
buf.Append((char)(n % b + '0'));
n = n / b;
} while (n != 0);
StringBuilder s = new StringBuilder();
for (int i = buf.Length - 1; i >= 0; i--) s.Append(buf[i]);
return s.ToString();
}
//-------------- event handlers ---------------------
//---- Convert TextBox val to hexadecimal format
public void ToHex(object sender, EventArgs e) {
try {
val.Text = "0x" + Value().ToString("x8");
error.Text = "";
} catch (FormatException) {
error.Text = errMsg;
}
}
//---- Convert TextBox val to decimal format
public void ToDec(object sender, EventArgs e) {
try {
val.Text = Value().ToString();
error.Text = "";
} catch (FormatException) {
error.Text = errMsg;
}
}
//---- Convert TextBox val to octal format
public void ToOct(object sender, EventArgs e) {
try {
val.Text = "0o" + ToNumeric(Value(), 8);
error.Text = "";
} catch (FormatException) {
error.Text = errMsg;
}
}
//---- Convert TextBox val to binary format
public void ToBin(object sender, EventArgs e) {
try {
val.Text = "0b" + ToNumeric(Value(), 2);
error.Text = "";
} catch (FormatException) {
error.Text = errMsg;
}
}
}
}
Alte URL:
/snippet/multiconverter-dez-hex-okt-bin/870
Es geht viiiel einfacher:[code]
/* Erweiterte Funktion der Covert.ToString() Methode.
* Einige der Überladungen dieser Methode erlauben als zweiten Parameter
* eine Zahlenbasis. */
class Converters
{
public void TestMethode()
{
/* Convert.ToString(byte byteValue, int base);
* Convert.ToString(int intValue, int base);
* … usw … */
// Beispiel:
string test1 = Convert.ToString(123, 2); // erzeugt 1111011
string test2 = Convert.ToString(123, 16); // ezeugt 7b
/* Umwandlung von STrings mit dualen oder hexadezimalen Zeichen
* in echte Zahlen.
* Convert.ToByte(string, base);
* Convert.ToInt32(string, base); */
// Beispiel:
byte b = Convert.ToByte(„00110110“, 2); // erzeugt 54
uint u = Convert.ToUInt32(„4d2“, 16); // erzeugt 1234
}
} [/code]
Hallo,
wahrscheinlich ging es dem Autor darum, das mal von Hand zu programmieren (?).
Jan
Dem Autor ging es darum einen Code aus einem Lehrbuch zu kopieren……
Mössenböck, Beer, Birngruber, Wöß:
.NET Application Development
with C#, ADO.NET, ASP.NET and Web Services
Pearson Addison Wesley 2003
ISBN 032117349X
Aber trotzdem hat er gut gemacht….. 😉