-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin.cs
54 lines (45 loc) · 1.18 KB
/
bin.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;
#if WITH_NAME_SPACE
namespace CScripting
{
#endif
public partial class CScripting
{
#region bin
/// <summary>
/// 将整形的数转为二进制
/// </summary>
/// <param name="x">整数</param>
/// <param name="width">位宽,<see langword="null"/> 则不关注,如果转换结果大于位宽,则取位宽</param>
/// <returns>二进制字符串</returns>
public static string Bin(long x, int? width = null)
=> Bin(x, width);
#if OBSOLETE
[Obsolete("This method is deprecated, use `{nameof(bin)}` instead.")]
#endif
public static string bin(long x, int? width = null)
{
if (width == null || width <= 0)
{
return "0b" + Convert.ToString(x, 2);
}
else
{
string format = Convert.ToString(x, 2);
if (format.Length < width)
{
string par = "";
for (int c = 0; c < width - format.Length; c++)
{
par += "0";
}
return "0b" + par + format;
}
return "0b" + format;
}
}
#endregion
}
#if WITH_NAME_SPACE
}
#endif