-3

これが私のコードで、4バイトを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-" + "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」と同じように取得したい ありがとう

4

2 に答える 2

2

BitConverterこのクラスを使用して、int値を次のxx-xx-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);
于 2012-10-28T10:12:35.000 に答える
0

@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");

label5.Text = IntToBytes(count);
于 2012-10-28T10:33:48.210 に答える