====== C# Zahlensysteme ====== {{:inf:csharp:pasted:20251204-175821.png}} //Zahlensysteme using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Zahlensysteme { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public string DecIntoBin(int zahl) { string binaer = ""; while (zahl > 0) { binaer = (zahl % 2) + binaer; zahl = zahl / 2; } Console.Out.WriteLine(binaer); return binaer; } public string BinIntoDec(string binaer) { int zahl = 0; for (int i = 0; i < binaer.Length; i++) { Console.Out.WriteLine("zahl:" + (binaer[i] - '0') * 1); zahl = zahl + ((binaer[i] - '0') * (int)Math.Pow(2, binaer.Length - i - 1)); } return zahl.ToString(); } public string OktIntoDec(string oktal) { int zahl = 0; for (int i = 0; i < oktal.Length; i++) { Console.Out.WriteLine("zahl:" + (oktal[i] - '0') * 1); zahl = zahl + ((oktal[i] - '0') * (int)Math.Pow(8, oktal.Length - i - 1)); } return zahl.ToString(); } public string HexIntoDec(string hex) { int zahl = 0; for (int i = 0; i < hex.Length; i++) { Console.Out.WriteLine("zahl:" + (hex[i] - '0') * 1); switch(hex[i]) { case 'A': zahl = zahl + (10 * (int)Math.Pow(16, hex.Length - i - 1)); break; case 'B': zahl = zahl + (11 * (int)Math.Pow(16, hex.Length - i - 1)); break; case 'C': zahl = zahl + (12 * (int)Math.Pow(16, hex.Length - i - 1)); break; case 'D': zahl = zahl + (13 * (int)Math.Pow(16, hex.Length - i - 1)); break; case 'E': zahl = zahl + (14 * (int)Math.Pow(16, hex.Length - i - 1)); break; case 'F': zahl = zahl + (15 * (int)Math.Pow(16, hex.Length - i - 1)); break; default: zahl = zahl + ((hex[i] - '0') * (int)Math.Pow(8, hex.Length - i - 1)); break; } } return zahl.ToString(); } public string DecIntoOkt(int zahl) { string oktal = ""; while (zahl > 0) { oktal = (zahl % 8) + oktal; zahl = zahl / 8; } Console.Out.WriteLine(oktal); return oktal; } public string DecIntoHex(int zahl) { string hex = ""; int help = 0; while (zahl > 0) { help = zahl % 16; switch(help) { case 10: hex = 'A' + hex; break; case 11: hex = 'B' + hex; break; case 12: hex = 'C' + hex; break; case 13: hex = 'D' + hex; break; case 14: hex = 'E' + hex; break; case 15: hex = 'F' + hex; break; default: hex = (zahl % 16) + hex; break; } zahl = zahl / 16; } Console.Out.WriteLine(hex); return hex; } private void tb_binaer_KeyPress(object sender, KeyPressEventArgs e) { Console.Out.WriteLine(e.KeyChar); if (char.IsDigit(e.KeyChar)) { if(e.KeyChar!= '1' && e.KeyChar!='0') { e.Handled = true; } } else { if(e.KeyChar!=8) { e.Handled = true; } } } private void tb_oktal_KeyPress(object sender, KeyPressEventArgs e) { Console.Out.WriteLine(e.KeyChar); if (char.IsDigit(e.KeyChar)) { if (e.KeyChar > 55 || e.KeyChar < 48 ) { e.Handled = true; } } else { if (e.KeyChar != 8) { e.Handled = true; } } } private void tb_dezimal_KeyPress(object sender, KeyPressEventArgs e) { Console.Out.WriteLine(e.KeyChar); if (!char.IsDigit(e.KeyChar) && e.KeyChar != 8) { e.Handled = true; } } private void tb_hexa_KeyPress(object sender, KeyPressEventArgs e) { Console.Out.WriteLine(e.KeyChar); if ((e.KeyChar > 64 && e.KeyChar < 71) || char.IsDigit(e.KeyChar) || e.KeyChar==8) { Console.Out.WriteLine("Zecihen erlaubt!"); } else { e.Handled = true; } } private void tb_binaer_KeyUp(object sender, KeyEventArgs e) { if (tb_binaer.Text.Length > 0) { tb_dezimal.Text = BinIntoDec(tb_binaer.Text); tb_oktal.Text = DecIntoOkt(int.Parse(BinIntoDec(tb_binaer.Text))); tb_hexa.Text = DecIntoHex(int.Parse(BinIntoDec(tb_binaer.Text))); } else { tb_dezimal.Text = ""; tb_oktal.Text = ""; tb_hexa.Text = ""; } } private void tb_hexa_KeyUp(object sender, KeyEventArgs e) { if (tb_hexa.Text.Length > 0) { tb_dezimal.Text = HexIntoDec(tb_hexa.Text); tb_binaer.Text = DecIntoBin(int.Parse(tb_dezimal.Text)); tb_oktal.Text = DecIntoOkt(int.Parse(BinIntoDec(tb_binaer.Text))); } else { tb_dezimal.Text = ""; tb_oktal.Text = ""; tb_binaer.Text = ""; } } private void tb_oktal_KeyUp(object sender, KeyEventArgs e) { if (tb_oktal.Text.Length > 0) { tb_dezimal.Text = OktIntoDec(tb_oktal.Text); tb_binaer.Text = DecIntoBin(int.Parse(tb_dezimal.Text)); tb_hexa.Text = DecIntoHex(int.Parse(BinIntoDec(tb_binaer.Text))); } else { tb_dezimal.Text = ""; tb_hexa.Text = ""; tb_binaer.Text = ""; } } private void tb_dezimal_KeyUp(object sender, KeyEventArgs e) { if (tb_dezimal.Text.Length > 0) { tb_binaer.Text = DecIntoBin(int.Parse(tb_dezimal.Text)); tb_oktal.Text = DecIntoOkt(int.Parse(tb_dezimal.Text)); tb_hexa.Text = DecIntoHex(int.Parse(tb_dezimal.Text)); } else { tb_oktal.Text = ""; tb_hexa.Text = ""; tb_binaer.Text = ""; } } } }