C# 16進制數多位顯示,填充高位

項目中有個需求,一個16進制數,要用4位顯示,也就是說在數字比較小的狀況下,填充高位。方法以下:code

byte rs = 0xFF;

byte[] result = new byte[4];
//有規律,依此類推
result[0] = (byte)(rs / Math.Pow(256, 3));
result[1] = (byte)(rs / Math.Pow(256, 2));
result[2] = (byte)(rs / 256);
result[3] = (byte)(rs % 256);

輸入: FF         輸出: 00 00 00 FFclass

輸入:ABFC    輸出:00 00 AB FC方法