Rafael Espindola | 228290c | 2010-09-11 15:25:58 +0000 | [diff] [blame] | 1 | def dataToHex(d): |
| 2 | """ Convert the raw data in 'd' to an hex string with a space every 4 bytes. |
| 3 | """ |
| 4 | bytes = [] |
| 5 | for i,c in enumerate(d): |
| 6 | byte = ord(c) |
| 7 | hex_byte = hex(byte)[2:] |
| 8 | if byte <= 0xf: |
| 9 | hex_byte = '0' + hex_byte |
| 10 | if i % 4 == 3: |
| 11 | hex_byte += ' ' |
| 12 | bytes.append(hex_byte) |
| 13 | return ''.join(bytes).strip() |
Jason W Kim | f7d5278 | 2010-10-19 17:39:10 +0000 | [diff] [blame^] | 14 | |
| 15 | def dataToHexUnified(d): |
| 16 | """ Convert the raw data in 'd' to an hex string with a space every 4 bytes. |
| 17 | Each 4byte number is prefixed with 0x for easy sed/rx |
| 18 | Fixme: convert all MC tests to use this routine instead of the above |
| 19 | """ |
| 20 | bytes = [] |
| 21 | for i,c in enumerate(d): |
| 22 | byte = ord(c) |
| 23 | hex_byte = hex(byte)[2:] |
| 24 | if byte <= 0xf: |
| 25 | hex_byte = '0' + hex_byte |
| 26 | if i % 4 == 0: |
| 27 | hex_byte = '0x' + hex_byte |
| 28 | if i % 4 == 3: |
| 29 | hex_byte += ' ' |
| 30 | bytes.append(hex_byte) |
| 31 | return ''.join(bytes).strip() |
| 32 | |
| 33 | |
| 34 | def HexDump(val, numBits=32): |
| 35 | """ |
| 36 | 1. do not print 'L' |
| 37 | 2. Handle negatives and large numbers by mod (2^numBits) |
| 38 | 3. print fixed length, prepend with zeros. |
| 39 | Length is exactly 2+(numBits/4) |
| 40 | 4. Do print 0x Why? |
| 41 | so that they can be easily distinguished using sed/rx |
| 42 | """ |
| 43 | val = val & (( 1 << numBits) - 1) |
| 44 | newFmt = "0x%0" + "%d" % (numBits / 4) + "x" |
| 45 | return newFmt % val |
| 46 | |