The wire-format of names in the DNS protocol is (by example):
The name www.example.com.
splits into four labels:
"www", "example", "com" and "" (there's always the empty label at the end, representing the root of the tree).
Each label is encoded with a prefix specifying the length of the label (single byte, with first two bits reserved, resulting in a six bit integer), followed by the raw contents of the label (as many bytes as the prefix specified).
The example name above is \x03www\x07example\x03com\x00
(if we use the normal \xNN
format for values of bytes that are not necessarily readable, with the value in hexadecimal).
When reading a name in wire-format, you would start at the first byte, look at how long it specifies that the next label is, read that many bytes as that label, repeating this process until you get to the zero-length label that represents the root of the tree (and end of the name).
In a packet, however, there is also the possibility of compressing names (by referring to previous labels).
Unless the purpose specifically is learning, you might want to look at eg Dnspython rather than starting from scratch.