Skip to content

Commit

Permalink
swipe commentary from System.Convert.ToBase64String()
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Smith committed Feb 6, 2021
1 parent dcf3447 commit d60bf9b
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Base16k.Test/Base16kTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void RejectUnparsableLength()
public void RejectNullInput()
{
var ex = Assert.Throws<ArgumentNullException>(() => Convert.FromBase16KString(null));
Assert.Equal("input", ex.ParamName);
Assert.Equal("s", ex.ParamName);
}
}
}
Expand Down
79 changes: 64 additions & 15 deletions Base16k/Base16k.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ namespace JDanielSmith
public static partial class Convert
{
/// <summary>
/// Encode a binary array into a Base16k string for Unicode.
/// Converts an array of 8-bit unsigned integers to its equivalent string representation
// that is encoded with base-16k digits.
/// </summary>
/// <param name="inArray">An array of 8-bit unsigned integers.</param>
/// <returns>The string representation, in base 16k, of the contents of inArray.</returns>
public static string ToBase16KString(byte[] inArray)
{
if (inArray == null) throw new ArgumentNullException(nameof(inArray));
Expand Down Expand Up @@ -91,13 +94,42 @@ public static string ToBase16KString(byte[] inArray)
}

return sb.ToString();
}

static byte[] FromBase16KString_(string input)
}

/// <summary>
/// Converts a subset of an array of 8-bit unsigned integers to its equivalent string
// representation that is encoded with base-16k digits. Parameters specify the subset
// as an offset in the input array, and the number of elements in the array to convert.
/// </summary>
/// <param name="inArray">An array of 8-bit unsigned integers.</param>
/// <param name="offset">An offset in inArray.</param>
/// <param name="length">The number of elements of inArray to convert.</param>
/// <returns>The string representation in base 16k of length elements of inArray, starting at position offset.</returns>
public static string ToBase16KString(byte[] inArray, int offset, int length)
{
throw new NotImplementedException();
}

/// <summary>
/// Converts a subset of an 8-bit unsigned integer array to an equivalent subset
/// of a Unicode character array encoded with base-16k digits. Parameters specify
/// the subsets as offsets in the input and output arrays, and the number of elements
/// in the input array to convert.
/// </summary>
/// <param name="inArray">An input array of 8-bit unsigned integers.</param>
/// <param name="offsetIn">A position within inArray.</param>
/// <param name="length">The number of elements of inArray to convert.</param>
/// <param name="outArray">An output array of Unicode characters.</param>
/// <param name="offsetOut">A position within outArray.</param>
/// <returns>A 32-bit signed integer containing the number of bytes in outArray.</returns>
public static int ToBase16KCharArray(byte[] inArray, int offsetIn, int length, char[] outArray, int offsetOut)
{
throw new NotImplementedException();
}

static byte[] FromBase16KString_(string s)
{
if (input == null) throw new ArgumentNullException("input");

string s = input;
if (s == null) throw new ArgumentNullException("s");

// read the length
var lengthEnd = -1;
Expand Down Expand Up @@ -188,22 +220,39 @@ static byte[] FromBase16KString_(string input)
}

return buf.ToArray();
}

/// <summary>
/// Decode a Base16k string for Unicode into a binary array.
/// </summary>
public static byte[] FromBase16KString(string input)
}

/// <summary>
/// Converts the specified string, which encodes binary data as base-16k digits, to
/// an equivalent 8-bit unsigned integer array.
/// </summary>
/// <param name="input">The string to convert.</param>
/// <returns>An array of 8-bit unsigned integers that is equivalent to s.</returns>
public static byte[] FromBase16KString(string s)
{
try
{
return FromBase16KString_(input);
return FromBase16KString_(s);
}
catch (OverflowException oe)
{
// Throw FormatException instead as we get an OverflowException from bad string data.
throw new FormatException("Invalid data.", oe);
}
}

/// <summary>
/// Converts a subset of a Unicode character array, which encodes binary data as
/// base-16k digits, to an equivalent 8-bit unsigned integer array. Parameters specify
/// the subset in the input array and the number of elements to convert.
/// </summary>
/// <param name="inArray">A Unicode character array.</param>
/// <param name="offset">A position within inArray.</param>
/// <param name="length">The number of elements in inArray to convert.</param>
/// <returns>An array of 8-bit unsigned integers equivalent to length elements at position offset in inArray.</returns>
public static byte[] FromBase64CharArray(char[] inArray, int offset, int length)
{
throw new NotImplementedException();
}
}
}
}

0 comments on commit d60bf9b

Please sign in to comment.