110,556
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Windows.Forms;
using System.Globalization;
using System.Text.RegularExpressions;
struct UInt96
{
ulong hi;
uint lo;
// 构造函数
public UInt96(ulong h, uint l)
{
hi = h;
lo = l;
}
// 解析字符串获得 UInt96
public static UInt96 Parse(string s)
{
s = s.Replace(" ", "");
if (s.Length == 0) return new UInt96();
ulong h = 0;
uint l = 0;
if (s.Length <= 8) uint.TryParse(s, NumberStyles.HexNumber, null, out l);
else
{
ulong.TryParse(s.Substring(0, s.Length - 8), NumberStyles.HexNumber, null, out h);
uint .TryParse(s.Substring(s.Length - 8, 8), NumberStyles.HexNumber, null, out l);
}
return new UInt96(h, l);
}
// 返回加1后的结果
public UInt96 Inc()
{
if (lo < 0xFFFFFFFF) return new UInt96(hi, lo + 1);
return new UInt96(hi + 1, 0);
}
// 自增运算符
public static UInt96 operator ++(UInt96 x)
{
return x.Inc();
}
// 十六进制字符串表示
public override string ToString()
{
return string.Join(" ", Regex.Split(string.Format("{0:X16}{1:X8}", hi, lo), "(?!^)(?=(?:.{4})+$)"));
}
}
class Form1 : Form
{
Form1()
{
TextBox tbx = new TextBox();
tbx.Parent = this;
tbx.Text = "1234 5678 9ABC DEFF FFFF FFFE";
tbx.Width = 190;
Button btn = new Button();
btn.Parent = this;
btn.Text = "递增(&I)";
btn.Left = 200;
btn.Click += delegate { tbx.Text = UInt96.Parse(tbx.Text).Inc().ToString(); };
}
static void Main()
{
Application.Run(new Form1());
}
}
using System;
using System.Text.RegularExpressions;
struct UInt96
{
ulong hi;
uint lo;
// 构造函数
public UInt96(ulong h, uint l)
{
hi = h;
lo = l;
}
// 返回加1后的结果
public UInt96 Inc()
{
if (lo < 0xFFFFFFFF) return new UInt96(hi, lo + 1);
return new UInt96(hi + 1, 0);
}
// 自增运算符
public static UInt96 operator ++(UInt96 x)
{
return x.Inc();
}
// 十六进制字符串表示
public override string ToString()
{
return string.Join(" ", Regex.Split(string.Format("{0:X16}{1:X8}", hi, lo), "(?!^)(?=(?:.{4})+$)"));
}
}
class Program
{
static void Main()
{
UInt96 a = new UInt96(0x123456789ABCDEF0, 0xFFFFFFFE);
UInt96 b = a.Inc();
UInt96 c = b.Inc();
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(c);
++c;
Console.WriteLine(c);
}
}
/* 程序输出:
1234 5678 9ABC DEF0 FFFF FFFE
1234 5678 9ABC DEF0 FFFF FFFF
1234 5678 9ABC DEF1 0000 0000
1234 5678 9ABC DEF1 0000 0001
*/
using System;
class Program
{
static void Main()
{
Console.WriteLine(new UInt96(0x123456789ABCDEF0, 0x11111111));
}
}
struct UInt96
{
ulong hi;
uint lo;
public UInt96(ulong h, uint l)
{
hi = h;
lo = l;
}
public override string ToString()
{
return string.Format("{0:X16}{1:X8}", hi, lo);
}
}
struct UInt96
{
ulong hi;
uint lo;
public override ToString()
{
return string.Format("{0:16X}{1:8X}", hi, lo);
}
}
using System;
using System.Windows.Forms;
using System.Globalization;
using System.Text.RegularExpressions;
struct UInt96
{
ulong hi;
uint lo;
// 构造函数
public UInt96(ulong h, uint l)
{
hi = h;
lo = l;
}
// 解析字符串获得 UInt96
public static UInt96 Parse(string s)
{
s = s.Replace(" ", "");
if (s.Length == 0) return new UInt96();
ulong h = 0;
uint l = 0;
if (s.Length <= 8) uint.TryParse(s, NumberStyles.HexNumber, null, out l);
else
{
ulong.TryParse(s.Substring(0, s.Length - 8), NumberStyles.HexNumber, null, out h);
uint .TryParse(s.Substring(s.Length - 8, 8), NumberStyles.HexNumber, null, out l);
}
return new UInt96(h, l);
}
// 返回加1后的结果
public UInt96 Inc()
{
if (lo < uint.MaxValue) return new UInt96(hi, lo + 1);
return new UInt96(hi + 1, 0);
}
// 返回减1后的结果
public UInt96 Dec()
{
if (lo > 0) return new UInt96(hi, lo - 1);
return new UInt96(hi - 1, uint.MaxValue);
}
// 自增运算符
public static UInt96 operator ++(UInt96 x)
{
return x.Inc();
}
// 自减运算符
public static UInt96 operator --(UInt96 x)
{
return x.Dec();
}
// 十六进制字符串表示
public override string ToString()
{
return string.Join(" ", Regex.Split(string.Format("{0:X16}{1:X8}", hi, lo), "(?!^)(?=(?:.{4})+$)"));
}
}
class Form1 : Form
{
Form1()
{
TextBox tbx = new TextBox();
tbx.Parent = this;
tbx.Text = "1234 5678 9ABC DEFF FFFF FFFE";
tbx.Width = 190;
Button btn1 = new Button();
btn1.Parent = this;
btn1.Text = "递减(&D)";
btn1.Top = 25;
btn1.Left = 10;
btn1.Click += delegate { tbx.Text = UInt96.Parse(tbx.Text).Dec().ToString(); };
Button btn2 = new Button();
btn2.Parent = this;
btn2.Text = "递增(&I)";
btn2.Top = 25;
btn2.Left = 105;
btn2.Click += delegate { tbx.Text = UInt96.Parse(tbx.Text).Inc().ToString(); };
}
static void Main()
{
Application.Run(new Form1());
}
}