/usr/lib/python2.7/dist-packages/dicom/util/hexutil.py is in python-dicom 0.9.9-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | # hexutil.py
"""Miscellaneous utility routines relating to hex and byte strings"""
# Copyright (c) 2008-2012 Darcy Mason
# This file is part of pydicom, released under a modified MIT license.
# See the file license.txt included with this distribution, also
# available at http://pydicom.googlecode.com
from binascii import a2b_hex, b2a_hex
from dicom import in_py3
from dicom.charset import default_encoding
def hex2bytes(hexstring):
"""Return bytestring for a string of hex bytes separated by whitespace
This is useful for creating specific byte sequences for testing, using
python's implied concatenation for strings with comments allowed.
Example:
hex_string = (
"08 00 32 10" # (0008, 1032) SQ "Procedure Code Sequence"
" 08 00 00 00" # length 8
" fe ff 00 e0" # (fffe, e000) Item Tag
)
byte_string = hex2bytes(hex_string)
Note in the example that all lines except the first must start with a space,
alternatively the space could end the previous line.
"""
# This works in both 3.x and 2.x because the first conditional evaluates to
# true in 2.x so the difference in bytes constructor doesn't matter
if isinstance(hexstring, bytes):
return a2b_hex(hexstring.replace(b" ", b""))
else:
return a2b_hex(bytes(hexstring.replace(" ", ""), default_encoding))
def bytes2hex(byte_string):
s = b2a_hex(byte_string)
if in_py3:
s = s.decode()
return " ".join(s[i:i + 2] for i in range(0, len(s), 2))
|