ASP.NET: Number Format Conversions
Notes:

Web Page

<%@ Page Language="C#" Inherits="Calculator" src="Calculator.aspx.cs" %>
<html>
<head>
<title>Hex/Dec/Oct/Bin conversions</title>
</head>
<body>
<form method="post" Runat="server">
<table border="1">
<tr>
<td>
<asp:TextBox ID="val" Width="140" Runat="server"/>
</td>
</tr>
<tr>
<td>
<asp:Button ID="hex" Text="Hex" OnClick="ToHex" Runat="server"/>
<asp:Button ID="dec" Text="Dec" OnClick="ToDec" Runat="server"/>
<asp:Button ID="oct" Text="Oct" OnClick="ToOct" Runat="server"/>
<asp:Button ID="bin" Text="Bin" OnClick="ToBin" Runat="server"/>
</td>
</tr>
<tr>
<td>
<asp:Label ID="error" Runat="server"/>
</td>
</tr>
</table>
</form>
</body>
</html>
 

Code Behind

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

public class Calculator : Page {
protected TextBox val;
protected Button hex;
protected Button dec;
protected Button oct;
protected Button bin;
protected Label error;

const string errMsg = "Illegal format. Should be: 128 | 0x80 | 0o200 | 0b10000000";

//----- convert hexadecimal string to int
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;
}
}

}

 

 

 

No votes yet
Related Links:ASP.NET: Displaying HTTP Request ParametersASP.NET: User ControlsASP.NET: Validity ChecksASP.NET: Validity ChecksASP.NET: Page Counter
© Copyright 2008. All Rights Reserved.