| ... | @@ -468,6 +468,26 @@ pub fn readIntLE(comptime T: type, bytes: []const u8) T { | ... | @@ -468,6 +468,26 @@ pub fn readIntLE(comptime T: type, bytes: []const u8) T { |
| 468 | return result; | 468 | return result; |
| 469 | } | 469 | } |
| 470 | | 470 | |
| | 471 | test "readIntBE/LE" { |
| | 472 | assert(readIntBE(u0, []u8{}) == 0x0); |
| | 473 | assert(readIntLE(u0, []u8{}) == 0x0); |
| | 474 | |
| | 475 | assert(readIntBE(u8, []u8{0x32}) == 0x32); |
| | 476 | assert(readIntLE(u8, []u8{0x12}) == 0x12); |
| | 477 | |
| | 478 | assert(readIntBE(u16, []u8{0x12, 0x34}) == 0x1234); |
| | 479 | assert(readIntLE(u16, []u8{0x12, 0x34}) == 0x3412); |
| | 480 | |
| | 481 | assert(readIntBE(u72, []u8{ 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x24 }) == 0x123456789abcdef024); |
| | 482 | assert(readIntLE(u72, []u8{ 0xec, 0x10, 0x32, 0x54, 0x76, 0x98, 0xba, 0xdc, 0xfe }) == 0xfedcba9876543210ec); |
| | 483 | |
| | 484 | assert(readIntBE(i8, []u8{0xff}) == -1); |
| | 485 | assert(readIntLE(i8, []u8{0xfe}) == -2); |
| | 486 | |
| | 487 | assert(readIntBE(i16, []u8{0xff, 0xfd}) == -3); |
| | 488 | assert(readIntLE(i16, []u8{0xfc, 0xff}) == -4); |
| | 489 | } |
| | 490 | |
| 471 | /// Writes an integer to memory with size equal to bytes.len. Pads with zeroes | 491 | /// Writes an integer to memory with size equal to bytes.len. Pads with zeroes |
| 472 | /// to fill the entire buffer provided. | 492 | /// to fill the entire buffer provided. |
| 473 | /// value must be an integer. | 493 | /// value must be an integer. |