C# 如何获得;X8“;六角型拆分为;X2-X2-X2-X2“;类型
这是我的代码,我只是想知道如何将4字节分割成1-1-1-1-1字节C# 如何获得;X8“;六角型拆分为;X2-X2-X2-X2“;类型,c#,C#,这是我的代码,我只是想知道如何将4字节分割成1-1-1-1-1字节 static int count; private void SetClock_Click(object sender, EventArgs e) { count++; label5.Text = count.ToString("X8"); DateTime time = DateTime.Now; txtSend.Text = "4D-"
static int count;
private void SetClock_Click(object sender, EventArgs e)
{
count++;
label5.Text = count.ToString("X8");
DateTime time = DateTime.Now;
txtSend.Text = "4D-" + "1A-" + "2B-" + "3C-" +
(label5.Text.ToString()) + "-" + "03-" + "07-" + "00-" +
time.ToString("yy-MM-dd-") +
((int)time.DayOfWeek).ToString("00") +
time.ToString("-HH-mm-ss");
string[] allHaxValues = txtSend.Text.Split(new char[] { '-' });
int result = 0;
foreach (string haxValue in allHaxValues)
{
result = result ^ Convert.ToInt32(haxValue, 16);
}
//txtSend.Text = s;
txtSend.Text = txtSend.Text + ("-") + result.ToString("X2");
}
我在点击“00000001”时收到值。我想像其他人一样获得它“00-00-00-01”谢谢您可以使用
位转换器
类将int
值转换为xx xx
格式:
// make a four byte array of the int
byte[] parts = BitConverter.GetBytes(result);
// put the bytes in the right order
if (BitConverter.IsLittleEndian) {
Array.Reverse(parts);
}
// turn the bytes into the xx-xx-xx-xx format
string resultString = BitConverter.ToString(parts);
基于@Guffa解决方案:
private string IntToBytes(int count)
{
// make a four byte array of the int
byte[] parts = BitConverter.GetBytes(count);
// put the bytes in the right order
if (BitConverter.IsLittleEndian) {
Array.Reverse(parts);
}
// turn the bytes into the xx-xx-xx-xx format
return BitConverter.ToString(parts);
}
现在只需更换
label5.Text = count.ToString("X8");
与
你能从一开始就键入全部代码吗。我不知道在哪里用这些。Thanks@NikolaObretenov:我不确定您想在代码中的何处使用它。代码只接受一个
int
值,并将其转换为xx xx xx xx
格式的字符串。使用您想要的任何值作为输入,并在您想要显示它的地方使用结果字符串。7类似的问题,您仍然无法解决您的问题?
label5.Text = IntToBytes(count);