Doru Roman wrote:
Thank you all for the reply.
What I was looking for was a fast MANUAL solution (not C#) to determine a &
b result when let's say none of the integers are 255.
I had a few <ahem> minutes, and I did this, as just a little exercise
and I thought you might like it.
It's a win form application with four text boxes (one for each base: 2,
8, 10, 12). Type into any to get the results in the others.
I apologize in advance for the line wrapping. If you really want it
nicely formatted, email me and i'll send it to you as an attachement.
Scott.
Quick and dirty, a single class:
---------------------------------------------
using System;
using System.Collecti ons.Generic;
using System.Drawing;
using System.Windows. Forms;
using System.Globaliz ation;
using System.Text;
namespace NumberViewer
{
/// <summary>
/// Description of MainForm.
/// </summary>
public class MainForm : System.Windows. Forms.Form
{
public MainForm()
{
//
// The InitializeCompo nent() call is required for Windows Forms
designer support.
//
InitializeCompo nent();
texts = new TextBox[] {
binText, octText, decText, hexText};
}
[STAThread]
public static void Main(string[] args)
{
Application.Ena bleVisualStyles ();
Application.Run (new MainForm());
}
#region Windows Forms Designer generated code
/// <summary>
/// This method is required for Windows Forms designer support.
/// Do not change the method contents inside the source code editor.
The Forms designer might
/// not be able to load this method if it was changed manually.
/// </summary>
private void InitializeCompo nent()
{
this.groupBox2 = new System.Windows. Forms.GroupBox( );
this.hexText = new System.Windows. Forms.TextBox() ;
this.label4 = new System.Windows. Forms.Label();
this.decText = new System.Windows. Forms.TextBox() ;
this.label3 = new System.Windows. Forms.Label();
this.octText = new System.Windows. Forms.TextBox() ;
this.label2 = new System.Windows. Forms.Label();
this.binText = new System.Windows. Forms.TextBox() ;
this.label1 = new System.Windows. Forms.Label();
this.groupBox2. SuspendLayout() ;
this.SuspendLay out();
//
// groupBox2
//
this.groupBox2. Anchor =
((System.Window s.Forms.AnchorS tyles)((((Syste m.Windows.Forms .AnchorStyles.T op
| System.Windows. Forms.AnchorSty les.Bottom)
| System.Windows. Forms.AnchorSty les.Left)
| System.Windows. Forms.AnchorSty les.Right)));
this.groupBox2. Controls.Add(th is.hexText);
this.groupBox2. Controls.Add(th is.label4);
this.groupBox2. Controls.Add(th is.decText);
this.groupBox2. Controls.Add(th is.label3);
this.groupBox2. Controls.Add(th is.octText);
this.groupBox2. Controls.Add(th is.label2);
this.groupBox2. Controls.Add(th is.binText);
this.groupBox2. Controls.Add(th is.label1);
this.groupBox2. Location = new System.Drawing. Point(1, 3);
this.groupBox2. Name = "groupBox2" ;
this.groupBox2. Size = new System.Drawing. Size(386, 136);
this.groupBox2. TabIndex = 1;
this.groupBox2. TabStop = false;
this.groupBox2. Text = "Type in any box";
this.groupBox2. UseCompatibleTe xtRendering = true;
//
// hexText
//
this.hexText.An chor =
((System.Window s.Forms.AnchorS tyles)(((System .Windows.Forms. AnchorStyles.To p
| System.Windows. Forms.AnchorSty les.Left)
| System.Windows. Forms.AnchorSty les.Right)));
this.hexText.Lo cation = new System.Drawing. Point(71, 106);
this.hexText.Na me = "hexText";
this.hexText.Si ze = new System.Drawing. Size(305, 21);
this.hexText.Ta bIndex = 7;
this.hexText.Ta g = "16";
this.hexText.En ter += new
System.EventHan dler(this.curre ntTextBoxChange dEventHander);
this.hexText.Ke yDown += new
System.Windows. Forms.KeyEventH andler(this.Hex TextKeyDown);
//
// label4
//
this.label4.Loc ation = new System.Drawing. Point(10, 106);
this.label4.Nam e = "label4";
this.label4.Siz e = new System.Drawing. Size(100, 23);
this.label4.Tab Index = 6;
this.label4.Tex t = "Hex";
this.label4.Use CompatibleTextR endering = true;
//
// decText
//
this.decText.An chor =
((System.Window s.Forms.AnchorS tyles)(((System .Windows.Forms. AnchorStyles.To p
| System.Windows. Forms.AnchorSty les.Left)
| System.Windows. Forms.AnchorSty les.Right)));
this.decText.Lo cation = new System.Drawing. Point(71, 76);
this.decText.Na me = "decText";
this.decText.Si ze = new System.Drawing. Size(305, 21);
this.decText.Ta bIndex = 5;
this.decText.Ta g = "10";
this.decText.En ter += new
System.EventHan dler(this.curre ntTextBoxChange dEventHander);
this.decText.Ke yDown += new
System.Windows. Forms.KeyEventH andler(this.Dec TextKeyDown);
//
// label3
//
this.label3.Loc ation = new System.Drawing. Point(10, 76);
this.label3.Nam e = "label3";
this.label3.Siz e = new System.Drawing. Size(100, 23);
this.label3.Tab Index = 4;
this.label3.Tex t = "Decimal";
this.label3.Use CompatibleTextR endering = true;
//
// octText
//
this.octText.An chor =
((System.Window s.Forms.AnchorS tyles)(((System .Windows.Forms. AnchorStyles.To p
| System.Windows. Forms.AnchorSty les.Left)
| System.Windows. Forms.AnchorSty les.Right)));
this.octText.Lo cation = new System.Drawing. Point(71, 46);
this.octText.Na me = "octText";
this.octText.Si ze = new System.Drawing. Size(305, 21);
this.octText.Ta bIndex = 3;
this.octText.Ta g = "8";
this.octText.En ter += new
System.EventHan dler(this.curre ntTextBoxChange dEventHander);
this.octText.Ke yDown += new
System.Windows. Forms.KeyEventH andler(this.Oct TextKeyDown);
//
// label2
//
this.label2.Loc ation = new System.Drawing. Point(10, 46);
this.label2.Nam e = "label2";
this.label2.Siz e = new System.Drawing. Size(100, 23);
this.label2.Tab Index = 2;
this.label2.Tex t = "Octal";
this.label2.Use CompatibleTextR endering = true;
//
// binText
//
this.binText.An chor =
((System.Window s.Forms.AnchorS tyles)(((System .Windows.Forms. AnchorStyles.To p
| System.Windows. Forms.AnchorSty les.Left)
| System.Windows. Forms.AnchorSty les.Right)));
this.binText.Lo cation = new System.Drawing. Point(72, 17);
this.binText.Na me = "binText";
this.binText.Si ze = new System.Drawing. Size(305, 21);
this.binText.Ta bIndex = 1;
this.binText.Ta g = "2";
this.binText.En ter += new
System.EventHan dler(this.curre ntTextBoxChange dEventHander);
this.binText.Ke yDown += new
System.Windows. Forms.KeyEventH andler(this.Bin TextKeyDown);
//
// label1
//
this.label1.Loc ation = new System.Drawing. Point(11, 17);
this.label1.Nam e = "label1";
this.label1.Siz e = new System.Drawing. Size(100, 23);
this.label1.Tab Index = 0;
this.label1.Tex t = "Binary";
this.label1.Use CompatibleTextR endering = true;
//
// MainForm
//
this.AutoScaleD imensions = new System.Drawing. SizeF(6F, 13F);
this.AutoScaleM ode = System.Windows. Forms.AutoScale Mode.Font;
this.ClientSize = new System.Drawing. Size(390, 142);
this.Controls.A dd(this.groupBo x2);
this.Name = "MainForm";
this.Text = "Number viewer";
this.groupBox2. ResumeLayout(fa lse);
this.groupBox2. PerformLayout() ;
this.ResumeLayo ut(false);
}
private System.Windows. Forms.TextBox binText;
private System.Windows. Forms.TextBox hexText;
private System.Windows. Forms.TextBox decText;
private System.Windows. Forms.TextBox octText;
private System.Windows. Forms.Label label1;
private System.Windows. Forms.Label label2;
private System.Windows. Forms.Label label3;
private System.Windows. Forms.Label label4;
private System.Windows. Forms.GroupBox groupBox2;
#endregion
TextBox current;
TextBox[] texts;
EventHandler textChangedEven t;
long theNumber;
string getHexString() {
string s = Convert.ToStrin g(theNumber, 16).ToUpper();
StringBuilder sb = new StringBuilder(s );
for(int i=s.Length; i>0; i-=2)
sb.Insert(i, " ");
return sb.ToString();
}
string getDecString() {
StringBuilder sb = new StringBuilder( Convert.ToStrin g(theNumber, 10));
for(int i=sb.Length; i>0; i-=3)
sb.Insert(i, " ");
return sb.ToString();
}
string getBinString() {
StringBuilder sb =
new StringBuilder(C onvert.ToString (theNumber, 2));
for(int i=sb.Length; i>0; i-=4)
sb.Insert(i, " ");
return sb.ToString();
}
string getOctString() {
return Convert.ToStrin g(theNumber, 8);
}
// the "input" text box has changed
// user is typing in a different textbox
private void currentTextBoxC hangedEventHand er(object sender, EventArgs e)
{
// unwire event
if(current != null && textChangedEven t != null)
current.TextCha nged -= textChangedEven t;
// change textbox and wire up
current = (TextBox)sender ;
textChangedEven t = new EventHandler(th is.textChangedE ventHandler);
current.TextCha nged += textChangedEven t;
}
// text in current textbox has changed, so update others
private void textChangedEven tHandler(object sender, EventArgs e) {
try {
if( (sender as TextBox).TextLe ngth==0)
theNumber = 0;
else
setTheNumber();
setTexts();
}
catch(FormatExc eption ex) {
MessageBox.Show (ex.Message, ex.GetType().To String());
}
catch(ArgumentE xception ex) {
MessageBox.Show (ex.Message, ex.GetType().To String());
}
catch(OverflowE xception ex) {
MessageBox.Show (ex.Message, ex.GetType().To String());
}
}
void setTexts() {
for(int i=0; i<texts.Length ; i++) {
if(texts[i]==current) continue;
TextBox t = texts[i];
int radix = Convert.ToInt32 ( t.Tag );
switch(radix) {
case 2: t.Text = getBinString(); break;
case 8: t.Text = getOctString(); break;
case 10:t.Text = getDecString(); break;
case 16:t.Text = getHexString(); break;
default: throw new ArgumentOutOfRa ngeException(
"Base must be one of 2, 8, 10 or 16.");
}
}
}
void setTheNumber() {
int radix = Convert.ToInt32 (current.Tag);
string txt = current.Text.Re place(" ", "");
theNumber = Convert.ToInt32 (current.Text, radix);
}
void BinTextKeyDown( object sender, System.Windows. Forms.KeyEventA rgs e)
{
e.SuppressKeyPr ess = !keyIsValid(e.K eyCode, 2);
}
void OctTextKeyDown( object sender, System.Windows. Forms.KeyEventA rgs e)
{
e.SuppressKeyPr ess = !keyIsValid(e.K eyCode, 8);
}
void DecTextKeyDown( object sender, System.Windows. Forms.KeyEventA rgs e)
{
e.SuppressKeyPr ess = !keyIsValid(e.K eyCode, 10);
}
void HexTextKeyDown( object sender, System.Windows. Forms.KeyEventA rgs e)
{
e.SuppressKeyPr ess = !keyIsValid(e.K eyCode, 16);
}
bool keyIsValid(Keys key, int radix) {
// non-printing characters
if(key == Keys.Back
|| key == Keys.Left || key == Keys.Right) {
return true;
}
int k = (int)key;
int low1, hi1; // numbers on top of keyboard
int low2, hi2; // numeric keypad
low1 = (int)Keys.D0;
low2 = (int)Keys.NumPa d0;
switch(radix) {
case 2:
hi1 = (int)Keys.D1;
hi2 = (int)Keys.NumPa d1;
return (low1 <= k && k <= hi1) || (low2 <= k && k <= hi2);
case 8:
hi1 = (int)Keys.D7;
hi2 = (int)Keys.NumPa d7;
return (low1 <= k && k <= hi1) || (low2 <= k && k <= hi2);
case 10:
hi1 = (int)Keys.D9;
hi2 = (int)Keys.NumPa d9;
return (low1 <= k && k <= hi1) || (low2 <= k && k <= hi2);
case 16:
// check for numeric value
if( ((int)Keys.D0 <= k && k <= (int)Keys.D9) ||
((int)Keys.NumP ad0 <= k && k <= (int)Keys.NumPa d9))
return true;
// check for a-f/A-F value
if( ((int)Keys.A <= k && k <= (int)Keys.F ))
return true;
return false;
default:
throw new ArgumentExcepti on("Invalid radix.");
}
}
}
}