public static string ToString(uint value, NumeralBase radix = NumeralBase.Decimal, string digits = kBase64Digits) { Contract.Requires(!string.IsNullOrEmpty(digits)); Contract.Requires(IsValidLookupTable(radix, digits)); return(ToStringImpl(value, (int)radix, digits)); }
/// <summary>Convert a character into the byte digit it represents</summary> /// <param name="c">Character representing the byte digit</param> /// <param name="radix">The base we're converting from</param> /// <param name="place">base-0 position of the digit in the number string</param> /// <returns></returns> /// <remarks> /// "Extended" char digits map different int values for upper ('A') and lower ('a') case. /// Does not validate that <paramref name="c"/> is within <paramref name="radix"/>'s range /// </remarks> public static int CharToIntExtended(char c, NumeralBase radix, int place) { int digit = CharToAnyDigitExtended(c); int from_base = (int)radix; int multiplier = (int)System.Math.Pow(from_base, place); return(digit * multiplier); }
public static List <char> ToStringBuilder(List <char> sb, uint value, NumeralBase radix = NumeralBase.Decimal, string digits = kBase64Digits) { Contract.Requires(sb != null); Contract.Requires(!string.IsNullOrEmpty(digits)); Contract.Requires(IsValidLookupTable(radix, digits)); ToStringBuilder(sb, value, (int)radix, digits); return(sb); }
/// <summary>Convert a byte digit character pair to the byte they represent</summary> /// <param name="radix">The base we're converting from</param> /// <param name="data">Buffer that holds the byte digit character pair</param> /// <param name="index">Index to start processing in <paramref name="data"/></param> /// <returns></returns> /// <remarks>Upper ('A') and lower ('a') case char digits map to the same int values</remarks> /// <example> /// int b = CharsToByte(NumeralBase.Hex, "3F"); /// b == 63; /// </example> public static int CharsToByte(NumeralBase radix, string data, int index = 0) { Contract.Requires <ArgumentNullException>(data != null); Contract.Requires(index >= 0); Contract.Requires(index < data.Length); Contract.Ensures(Contract.Result <int>() >= byte.MinValue); Contract.Ensures(Contract.Result <int>() <= byte.MaxValue); return(CharsToByte(radix, data[index], data[index + 1])); }
public void Text_ToAcceptableNumberBaseTest() { const NumeralBase k_invalid = 0; const NumeralBase k_nonstandard_36 = (NumeralBase)36; Assert.AreEqual(k_invalid, Util.ToAcceptableNumberBase(-2)); Assert.AreEqual(k_invalid, Util.ToAcceptableNumberBase(0)); Assert.AreEqual(k_invalid, Util.ToAcceptableNumberBase(64)); Assert.AreEqual(k_nonstandard_36, Util.ToAcceptableNumberBase(36)); Assert.AreEqual(NumeralBase.Hex, Util.ToAcceptableNumberBase(16)); }
public static StringBuilder ToStringBuilder(StringBuilder sb, uint value, NumeralBase radix = NumeralBase.Decimal, int startIndex = -1, string digits = kBase64Digits) { Contract.Requires(sb != null); Contract.Requires(!string.IsNullOrEmpty(digits)); Contract.Requires(IsValidLookupTable(radix, digits)); Contract.Requires(startIndex.IsNoneOrPositive()); if (startIndex.IsNone()) { startIndex = sb.Length; } ToStringBuilder(sb, value, (int)radix, startIndex, digits); return(sb); }
private void radioButton_Hex_CheckedChanged(object sender, EventArgs e) { if (textBox_Data.Text != "") { if (chosenDataType == NumeralBase.Binary) { textBox_Data.Text = Convert.ToInt16(textBox_Data.Text, 2).ToString("X"); } else if (chosenDataType == NumeralBase.Decimal) { textBox_Data.Text = Convert.ToInt16(textBox_Data.Text, 10).ToString("X"); } } chosenDataType = NumeralBase.Hex; validateTextBox(); }
/// <summary>Convert a byte digit character pair to the byte they represent</summary> /// <param name="radix">The base we're converting from</param> /// <param name="c2">Character in the 2nd position (when reading right to left)</param> /// <param name="c1">Character in the 1st position (when reading right to left)</param> /// <returns></returns> /// <remarks>Upper ('A') and lower ('a') case char digits map to the same int values</remarks> /// <example> /// int b = CharsToByte(NumeralBase.Hex, '3', 'F'); /// b == 63; /// </example> public static int CharsToByte(NumeralBase radix, char c2, char c1) { Contract.Ensures(Contract.Result <int>() >= byte.MinValue); Contract.Ensures(Contract.Result <int>() <= byte.MaxValue); int value = 0; if (CharIsAnyDigit(c2) && CharIsAnyDigit(c1)) { value = CharToInt(c2, radix, 1) + CharToInt(c1, radix, 0); } // Someone could supply a radix value that isn't technically a member of NumeralBase (eg, 36) // So we clamp it to a byte here return(value > byte.MaxValue ? 0 : value); }
private void radioButton_Bin_CheckedChanged(object sender, EventArgs e) { if (textBox_Data.Text != "") { if (chosenDataType == NumeralBase.Decimal) { textBox_Data.Text = Convert.ToString(Convert.ToInt16(textBox_Data.Text, 10), 2); } else if (chosenDataType == NumeralBase.Hex) { textBox_Data.Text = string.Join(string.Empty, textBox_Data.Text.Select(c => Convert.ToString(Convert.ToInt16(c.ToString(), 16), 2).PadLeft(4, '0'))); } } chosenDataType = NumeralBase.Binary; validateTextBox(); }
public override void WriteAttribute(string name, sbyte value, NumeralBase toBase) { CursorWriteAttribute(name, Numbers.ToString(value, toBase)); }
protected override void WriteElement(TCursor n, long value, NumeralBase toBase) { WriteElement(n, Numbers.ToString(value, toBase)); }
public static bool IsValidLookupTable(NumeralBase radix, string digits) { return(radix >= NumeralBase.Binary && (int)radix <= digits.Length); }
protected override void ReadElement(TCursor n, ref long value, NumeralBase fromBase) { Numbers.ParseString(GetInnerText(n), ref value, kThrowExcept, mReadErrorState, fromBase); }
public override bool ReadAttributeOpt(string name, ref long value, NumeralBase fromBase = kDefaultRadix) { return(Numbers.ParseString(ReadAttributeOpt(name), ref value, kNoExcept, mReadErrorState, fromBase)); }
public override void ReadAttribute(string name, ref long value, NumeralBase fromBase = kDefaultRadix) { Numbers.ParseString(ReadAttribute(name), ref value, kThrowExcept, mReadErrorState, fromBase); }