Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add binary support to number formatting
  • Loading branch information
stephentoub committed Apr 16, 2023
commit e2c433036b6d7f4af0b91bdf3b2beff1613b41c0
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@
<Compile Include="$(MSBuildThisFileDirectory)System\Globalization\CompareInfo.Icu.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Globalization\CompareInfo.Invariant.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Globalization\CompareInfo.Nls.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Globalization\CompareInfo.WebAssembly.cs" Condition="'$(TargetsBrowser)' == 'true'"/>
<Compile Include="$(MSBuildThisFileDirectory)System\Globalization\CompareInfo.WebAssembly.cs" Condition="'$(TargetsBrowser)' == 'true'" />
<Compile Include="$(MSBuildThisFileDirectory)System\Globalization\CompareOptions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Globalization\CultureData.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Globalization\CultureData.Icu.cs" />
Expand Down Expand Up @@ -2599,4 +2599,4 @@
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\IUnaryPlusOperators.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\IUnsignedNumber.cs" />
</ItemGroup>
</Project>
</Project>
64 changes: 38 additions & 26 deletions src/libraries/System.Private.CoreLib/src/System/Convert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2250,43 +2250,55 @@ public static ulong ToUInt64(string? value, int fromBase)
}

// Convert the byte value to a string in base fromBase
public static string ToString(byte value, int toBase)
{
if (toBase != 2 && toBase != 8 && toBase != 10 && toBase != 16)
{
throw new ArgumentException(SR.Arg_InvalidBase);
}
return ParseNumbers.IntToString((int)value, toBase, -1, ' ', ParseNumbers.PrintAsI1);
}
public static string ToString(byte value, int toBase) =>
ToString((int)value, toBase);

// Convert the Int16 value to a string in base fromBase
public static string ToString(short value, int toBase)
{
if (toBase != 2 && toBase != 8 && toBase != 10 && toBase != 16)
public static string ToString(short value, int toBase) =>
toBase switch
{
throw new ArgumentException(SR.Arg_InvalidBase);
}
return ParseNumbers.IntToString((int)value, toBase, -1, ' ', ParseNumbers.PrintAsI2);
}
2 => value.ToString("b"),
8 => ToOctalString((ushort)value),
10 => value.ToString(CultureInfo.InvariantCulture),
16 => value.ToString("x"),
_ => throw new ArgumentException(SR.Arg_InvalidBase),
};

// Convert the Int32 value to a string in base toBase
public static string ToString(int value, int toBase)
{
if (toBase != 2 && toBase != 8 && toBase != 10 && toBase != 16)
public static string ToString(int value, int toBase) =>
toBase switch
{
throw new ArgumentException(SR.Arg_InvalidBase);
}
return ParseNumbers.IntToString(value, toBase, -1, ' ', 0);
}
2 => value.ToString("b"),
8 => ToOctalString((uint)value),
10 => value.ToString(CultureInfo.InvariantCulture),
16 => value.ToString("x"),
_ => throw new ArgumentException(SR.Arg_InvalidBase),
};

// Convert the Int64 value to a string in base toBase
public static string ToString(long value, int toBase)
public static string ToString(long value, int toBase) =>
toBase switch
{
2 => value.ToString("b"),
8 => ToOctalString((ulong)value),
10 => value.ToString(CultureInfo.InvariantCulture),
16 => value.ToString("x"),
_ => throw new ArgumentException(SR.Arg_InvalidBase),
};

private static string ToOctalString(ulong value)
{
if (toBase != 2 && toBase != 8 && toBase != 10 && toBase != 16)
Span<char> chars = stackalloc char[22]; // max length of a ulong in octal

int i = chars.Length;
do
{
throw new ArgumentException(SR.Arg_InvalidBase);
chars[--i] = (char)('0' + (value & 7));
value >>= 3;
}
return ParseNumbers.LongToString(value, toBase, -1, ' ', 0);
while (value != 0);

return chars.Slice(i).ToString();
}

public static string ToBase64String(byte[] inArray)
Expand Down
Loading