• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1def 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()
14
15def 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
34def HexDump(valPair):
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, numBits = valPair
44    assert 0 <= val < (1 << numBits)
45
46    val = val & (( 1 << numBits) - 1)
47    newFmt = "0x%0" + "%d" % (numBits / 4) + "x"
48    return newFmt % val
49