using System; public class MainEntryPoint { public static void Main() { string userInput; while (true) { try { Console.Write("Input a number between 0 and 5 " + "(or just hit return to exit)> "); userInput = Console.ReadLine(); if (userInput == "") break; int index = Convert.ToInt32(userInput); if (index < 0 || index > 5) throw new IndexOutOfRangeException("You typed in " + userInput); Console.WriteLine("Your number was " + index); } catch (IndexOutOfRangeException e) { Console.WriteLine("Exception: Number should be between 0 and 5. " + e.Message); } catch (Exception e) { Console.WriteLine("An exception was thrown. Message was: " + e.Message); } catch { Console.WriteLine("Some other exception has occurred"); } finally { Console.WriteLine("Thank you"); } } } }
Convert Hex char To Int
#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the “Software”), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.Text;
namespace Newtonsoft.Json.Utilities
{
internal class MathUtils
{
public static int HexToInt(char h)
{
if ((h >= '0') && (h <= '9'))
{
return (h - '0');
}
if ((h >= 'a') && (h <= 'f'))
{
return ((h - 'a') + 10);
}
if ((h >= 'A') && (h <= 'F'))
{
return ((h - 'A') + 10);
}
return -1;
}
}
}
[/csharp]
Hex Translator
//http://www.bouncycastle.org/
//MIT X11 License
using System;
namespace Org.BouncyCastle.Utilities.Encoders
{
///
///
public class HexTranslator
{
private static readonly byte[] hexTable =
{
(byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)'7',
(byte)'8', (byte)'9', (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f'
};
///
///
///
public int GetEncodedBlockSize()
{
return 2;
}
///
///
/// Input data array.
/// Start position within input data array.
/// The amount of data to process.
/// The output data array.
/// The offset within the output data array to start writing from.
///
public int Encode(
byte[] input,
int inOff,
int length,
byte[] outBytes,
int outOff)
{
for (int i = 0, j = 0; i < length; i++, j += 2)
{
outBytes[outOff + j] = hexTable[(input[inOff] >> 4) & 0x0f];
outBytes[outOff + j + 1] = hexTable[input[inOff] & 0x0f];
inOff++;
}
return length * 2;
}
///
///
///
public int GetDecodedBlockSize()
{
return 1;
}
///
///
/// The input data array.
/// Start position within input data array.
/// The amounty of data to process.
/// The output data array.
/// The position within the output data array to start writing from.
///
public int Decode(
byte[] input,
int inOff,
int length,
byte[] outBytes,
int outOff)
{
int halfLength = length / 2;
byte left, right;
for (int i = 0; i < halfLength; i++)
{
left = input[inOff + i * 2];
right = input[inOff + i * 2 + 1];
if (left < (byte)'a')
{
outBytes[outOff] = (byte)((left - '0') << 4);
}
else
{
outBytes[outOff] = (byte)((left - 'a' + 10) << 4);
}
if (right < (byte)'a')
{
outBytes[outOff] += (byte)(right - '0');
}
else
{
outBytes[outOff] += (byte)(right - 'a' + 10);
}
outOff++;
}
return halfLength;
}
}
}
[/csharp]
Hex Encoder
//http://www.bouncycastle.org/
//MIT X11 License
using System;
using System.IO;
namespace Org.BouncyCastle.Utilities.Encoders
{
public class HexEncoder
{
private static readonly byte[] encodingTable =
{
(byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)'7',
(byte)'8', (byte)'9', (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f'
};
/*
* set up the decoding table.
*/
internal static readonly byte[] decodingTable = new byte[128];
static HexEncoder()
{
for (int i = 0; i < encodingTable.Length; i++)
{
decodingTable[encodingTable[i]] = (byte)i;
}
decodingTable['A'] = decodingTable['a'];
decodingTable['B'] = decodingTable['b'];
decodingTable['C'] = decodingTable['c'];
decodingTable['D'] = decodingTable['d'];
decodingTable['E'] = decodingTable['e'];
decodingTable['F'] = decodingTable['f'];
}
/**
* encode the input data producing a Hex output stream.
*
* @return the number of bytes produced.
*/
public int Encode(
byte[] data,
int off,
int length,
Stream outStream)
{
for (int i = off; i < (off + length); i++)
{
int v = data[i];
outStream.WriteByte(encodingTable[v >> 4]);
outStream.WriteByte(encodingTable[v & 0xf]);
}
return length * 2;
}
private bool ignore(
char c)
{
return (c == '
' || c =='
' || c == ' ' || c == ' ');
}
/**
* decode the Hex encoded byte data writing it to the given output stream,
* whitespace characters will be ignored.
*
* @return the number of bytes produced.
*/
public int Decode(
byte[] data,
int off,
int length,
Stream outStream)
{
byte b1, b2;
int outLen = 0;
int end = off + length;
while (end > off)
{
if (!ignore((char)data[end – 1]))
{
break;
}
end–;
}
int i = off;
while (i < end)
{
while (i < end && ignore((char)data[i]))
{
i++;
}
b1 = decodingTable[data[i++]];
while (i < end && ignore((char)data[i]))
{
i++;
}
b2 = decodingTable[data[i++]];
outStream.WriteByte((byte)((b1 << 4) | b2));
outLen++;
}
return outLen;
}
/**
* decode the Hex encoded string data writing it to the given output stream,
* whitespace characters will be ignored.
*
* @return the number of bytes produced.
*/
public int DecodeString(
string data,
Stream outStream)
{
byte b1, b2;
int length = 0;
int end = data.Length;
while (end > 0)
{
if (!ignore(data[end – 1]))
{
break;
}
end–;
}
int i = 0;
while (i < end)
{
while (i < end && ignore(data[i]))
{
i++;
}
b1 = decodingTable[data[i++]];
while (i < end && ignore(data[i]))
{
i++;
}
b2 = decodingTable[data[i++]];
outStream.WriteByte((byte)((b1 << 4) | b2));
length++;
}
return length;
}
}
}
[/csharp]
To specify a hexadecimal constant, begin with 0x or 0X, followed by a sequence of digits in the range 0 through 9 and a (or A) through f (or F).
//Hexadecimal digits a (or A) through f (or F) represent values in the range 10 through 15. using System; public class MainClass{ public static void Main() { int i = 0x3fff; // Hexadecimal constant int j = 0X3FFF; // Equal to i Console.WriteLine(i); Console.WriteLine(j); } }
The hex dump program
using System;
using System.IO;
class Class1 {
public static string PadZero( string s, int len ) {
string temp = s;
for ( int i=s.Length; i
Format float point value as number
using System; class Sample { public static void Main() { Console.WriteLine("(N) Number: . . . . . . . . . {0:N} " + -123, -123.45f); } }