...
1 /// <summary> 2 /// 本類提供了對byte數據的常用操作函數 3 /// </summary> 4 public class ByteUtil 5 { 6 private static char[] HEX_CHARS = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; 7 private static byte[] BITS = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80}; 8 9 10 /// <summary> 11 /// 將位元組數組轉換為HEX形式的字元串, 使用指定的間隔符 12 /// </summary> 13 public static string ByteToHex(byte[] buf, string separator) 14 { 15 System.Text.StringBuilder sb = new System.Text.StringBuilder(); 16 for(int i = 0;i < buf.Length;i++) 17 { 18 if (i > 0) 19 { 20 sb.Append(separator); 21 } 22 sb.Append(HEX_CHARS[buf[i] >> 4]).Append(HEX_CHARS[buf[i] & 0x0F]); 23 } 24 return sb.ToString(); 25 } 26 27 28 29 30 /// <summary> 31 /// 將位元組數組轉換為HEX形式的字元串, 使用指定的間隔符 32 /// </summary> 33 public static string ByteToHex(byte[] buf, char c) 34 { 35 System.Text.StringBuilder sb = new System.Text.StringBuilder(); 36 for(int i = 0;i < buf.Length;i++) 37 { 38 if (i > 0) 39 { 40 sb.Append(c); 41 } 42 sb.Append(HEX_CHARS[buf[i] >> 4]).Append(HEX_CHARS[buf[i] & 0x0F]); 43 } 44 return sb.ToString(); 45 } 46 47 48 /// <summary> 49 /// 判斷位元組數組前幾位是否符合一定規則 50 /// </summary> 51 /// <param name="data">需要判斷的位元組數組</param> 52 /// <param name="pattern">匹配規則</param> 53 /// <returns>如果匹配返回true</returns> 54 public static bool IsMatch(byte[] data, params byte[] pattern) 55 { 56 if (data == null || data.Length < pattern.Length) 57 return false; 58 59 60 for(int i = 0;i < pattern.Length;i++) 61 { 62 if (data[i] != pattern[i]) 63 return false; 64 } 65 return true; 66 } 67 68 69 /// <summary> 70 /// 判斷指定位元組是否為列舉的某個值 71 /// </summary> 72 /// <param name="value">需要判斷的值</param> 73 /// <param name="choice">可能值</param> 74 /// <returns>如果與任一個可能值相等則返回true</returns> 75 public static bool IsMatch(byte value, params byte[] choice) 76 { 77 if (choice == null || choice.Length == 0) 78 return false; 79 80 81 foreach(byte item in choice) 82 { 83 if (item == value) 84 return true; 85 } 86 return false; 87 } 88 89 90 91 92 /// <summary> 93 /// 將位元組數組轉換為HEX形式的字元串, 沒有間隔符 94 /// </summary> 95 public static string ByteToHex(byte[] buf) 96 { 97 return ByteToHex(buf, string.Empty); 98 } 99 100 101 102 103 /// <summary> 104 /// 將位元組數組轉換為HEX形式的字元串 105 /// 轉換後的字元串長度為位元組數組長度的兩倍 106 /// 如: 1, 2 轉換為 0102 107 /// </summary> 108 public static string ByteToHex(byte b) 109 { 110 return string.Empty + HEX_CHARS[b >> 4] + HEX_CHARS[b & 0x0F]; 111 } 112 113 114 115 116 /// <summary> 117 /// 將位元組流信息轉換為HEX字元串 118 /// </summary> 119 public static string DumpBytes(byte[] bytes) 120 { 121 return DumpBytes(bytes, 0, bytes.Length); 122 } 123 124 125 /// <summary> 126 /// 將位元組流信息轉換為HEX字元串 127 /// </summary> 128 public static string DumpBytes(byte[] bytes, int offset, int len) 129 { 130 StringBuilder buf = new StringBuilder(); 131 for(int i = 0;i < len;i++) 132 { 133 if (i == 0 || i % 16 == 0) 134 buf.AppendLine(); 135 136 buf.Append(ByteToHex(bytes[i + offset])); 137 buf.Append(' '); 138 } 139 buf.AppendLine(); 140 return buf.ToString(); 141 } 142 143 144 145 146 /// <summary> 147 /// 計算位元組塊的模256校驗和 148 /// </summary> 149 public static byte SumBytes(byte[] bytes, int offset, int len) 150 { 151 int sum = 0; 152 for(int i = 0;i < len;i++) 153 { 154 sum += bytes[i + offset]; 155 if (sum >= 256) 156 { 157 sum = sum % 256; 158 } 159 } 160 return (byte)sum; 161 } 162 163 164 /// <summary> 165 /// 計算位元組塊的模256雙位元組校驗和(低位在前) 166 /// </summary> 167 public static byte[] Sum2Bytes(byte[] bytes, int offset, int len) 168 { 169 int sum = 0; 170 for(int i = 0;i < len;i++) 171 sum += bytes[i + offset]; 172 return new byte[] { (byte)(sum % 256), (byte)(sum / 256) }; 173 } 174 175 176 /// <summary> 177 /// 計算位元組塊的異或校驗和 178 /// </summary> 179 public static byte XorSumBytes(byte[] bytes, int offset, int len) 180 { 181 byte sum = bytes[0 + offset]; 182 for(int i = 1;i < len;i++) 183 { 184 sum = (byte)(sum ^ bytes[i + offset]); 185 } 186 return sum; 187 } 188 189 190 191 192 /// <summary> 193 /// 計算位元組塊的異或校驗和 194 /// </summary> 195 public static byte XorSumBytes(byte[] bytes) 196 { 197 return XorSumBytes(bytes, 0, bytes.Length); 198 } 199 200 201 202 203 /// <summary> 204 /// 比較兩個位元組塊是否相等。相等返回true否則false 205 /// </summary> 206 public static bool CompareBytes(byte[] bytes1, int offset1, byte[] bytes2, int offset2, int len) 207 { 208 for(int i = 0;i < len;i++) 209 { 210 if (bytes1[i + offset1] != bytes2[i + offset2]) 211 { 212 return false; 213 } 214 } 215 return true; 216 } 217 218 219 /// <summary> 220 /// 將兩個字元的hex轉換為byte 221 /// </summary> 222 public static byte HexToByte(char[] hex, int offset) 223 { 224 byte result = 0; 225 for(int i = 0;i < 2;i++) 226 { 227 char c = hex[i + offset]; 228 byte b = 0; 229 switch (c) 230 { 231 case '0': 232 case '1': 233 case '2': 234 case '3': 235 case '4': 236 case '5': 237 case '6': 238 case '7': 239 case '8': 240 case '9': 241 b = (byte)(c - '0'); 242 break; 243 case 'A': 244 case 'B': 245 case 'C': 246 case 'D': 247 case 'E': 248 case 'F': 249 b = (byte)(10 + c - 'A'); 250 break; 251 case 'a': 252 case 'b': 253 case 'c': 254 case 'd': 255 case 'e': 256 case 'f': 257 b = (byte)(10 + c - 'a'); 258 break; 259 } 260 if (i == 0) 261 { 262 b = (byte)(b * 16); 263 } 264 result += b; 265 } 266 267 268 return result; 269 } 270 271 272 /// <summary> 273 /// 將兩個字元的hex轉換為byte 274 /// </summary> 275 public static byte HexToByte(byte[] hex, int offset) 276 { 277 char[] chars = {(char)hex[offset], (char)hex[offset + 1]}; 278 return HexToByte(chars, 0); 279 } 280 281 282 /// <summary> 283 /// 轉換16進位字元串為位元組數組 284 /// <param name="hex">有分隔或無分隔的16進位字元串,如“AB CD EF 12 34...”或“ABCDEF1234...”</param> 285 /// <param name="dot">任意分隔字元,但不能是16進位字元</param> 286 /// <returns>位元組數組</returns> 287 /// </summary> 288 public static byte[] HexToByte(string hex, params char[] dot) { 289 char[] ca = new char[2]; 290 List<byte> list = new List<byte>(); 291 for (int i = 0, n = 0; i < hex.Length; i++) { 292 if (Array.IndexOf<char>(dot, hex[i]) >= 0) { 293 continue; 294 } 295 296 297 switch (++n) { 298 case 1: 299 ca[0] = hex[i]; 300 break; 301 302 303 case 2: 304 ca[1] = hex[i]; 305 list.Add(ByteUtil.HexToByte(ca, 0)); 306 n = 0; 307 break; 308 } 309 } 310 311 312 return list.ToArray(); 313 } 314 315 316 /// <summary> 317 /// 將uint變數分解為四個位元組。高位在前。 318 /// </summary> 319 public static void UintToBytes(uint i, byte[] bytes, int offset) 320 { 321 bytes[offset] = (byte)((i & 0xFF000000) >> 24); 322 bytes[offset + 1] = (byte)((i & 0x00FF0000) >> 16); 323 bytes[offset + 2] = (byte)((i & 0x0000FF00) >> 8); 324 bytes[offset + 3] = (byte)(i & 0x000000FF); 325 } 326 327 328 /// <summary> 329 /// 將uint變數分解為四個位元組。高位在前。 330 /// </summary> 331 public static byte[] UintToBytes(uint i) 332 { 333 byte[] bytes = new byte[4]; 334 bytes[0] = (byte)((i & 0xFF000000) >> 24); 335 bytes[1] = (byte)((i & 0x00FF0000) >> 16); 336 bytes[2] = (byte)((i & 0x0000FF00) >> 8); 337 bytes[3] = (byte)(i & 0x000000FF); 338 return bytes; 339 } 340 341 342 /// <summary> 343 /// 將int變數分解為四個位元組。高位在前。 344 /// </summary> 345 public static byte[] IntToBytes(int i) 346 { 347 byte[] data = BitConverter.GetBytes(i); 348 Array.Reverse(data); 349 return data; 350 351 352 //byte[] bytes = new byte[4]; 353 //bytes[0] = (byte)((i & 0xFF000000) >> 24); 354 //bytes[1] = (byte)((i & 0x00FF0000) >> 16); 355 //bytes[2] = (byte)((i & 0x0000FF00) >> 8); 356 //bytes[3] = (byte)(i & 0x000000FF); 357 //return bytes; 358 } 359 360 361 /// <summary> 362 /// 將四個位元組合成為一個int 363 /// </summary> 364 public static uint BytesToUint(byte[] bytes, int offset) 365 { 366 uint a = ((uint)bytes[offset]) << 24; 367 uint b = ((uint)bytes[offset + 1]) << 16; 368 uint c = ((uint)bytes[offset + 2]) << 8; 369 uint d = bytes[offset + 3]; 370 return a + b + c + d; 371 } 372 373 374 /// <summary> 375 /// 將ulong變數分解為八個位元組。高位在前。 376 /// </summary> 377 public static byte[] UlongToBytes(ulong i) 378 { 379 byte[] bytes = new byte[8]; 380 bytes[0] = (byte)((i & 0xFF00000000000000) >> 56); 381 bytes[1] = (byte)((i & 0x00FF000000000000) >> 48); 382 bytes[2] = (byte)((i & 0x0000FF0000000000) >> 40); 383 bytes[3] = (byte)((i & 0x000000FF00000000) >> 32); 384 bytes[4] = (byte)((i & 0x00000000FF000000) >> 24); 385 bytes[5] = (byte)((i & 0x0000000000FF0000) >> 16); 386 bytes[6] = (byte)((i & 0x000000000000FF00) >> 8); 387 bytes[7] = (byte)(i & 0x00000000000000FF); 388 return bytes; 389 } 390 391 392 /// <summary> 393 /// 將八個位元組合成為一個ulong 394 /// </summary> 395 public static ulong BytesToUlong(byte[] bytes, int offset) 396 { 397 ulong a = ((ulong)bytes[offset]) << 56; 398 ulong b = ((ulong)bytes[offset + 1]) << 48; 399 ulong c = ((ulong)bytes[offset + 2]) << 40; 400 ulong d = ((ulong)bytes[offset + 3]) << 32; 401 ulong e = ((ulong)bytes[offset + 4]) << 24; 402 ulong f = ((ulong)bytes[offset + 5]) << 16; 403 ulong g = ((ulong)bytes[offset + 6]) << 8; 404 ulong h = bytes[offset + 7]; 405 return a + b + c + d + e + f + g + h; 406 } 407 408 409 410 411 /// <summary> 412 /// 設置某個位元組的指定位 413 /// </summary> 414 /// <param name="b">需要設置的位元組</param> 415 /// <param name="pos">1-8, 1表示最低位, 8表示最高位</param> 416 /// <param name="on">true表示設置1, false表示設置0</param> 417