Last active
November 4, 2016 12:07
-
-
Save boxofrad/346bce7e4fb801a8cf7c7c827aa7f99b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UDPDatagram | |
def source_port | |
word16(bytes[0], bytes[1]) | |
end | |
def destination_port | |
word16(bytes[2], bytes[3]) | |
end | |
def length | |
word16(bytes[4], bytes[5]) | |
end | |
def checksum | |
word16(bytes[6], bytes[7]) | |
end | |
# Interpret bytes `a` and `b` as a 16-bit word | |
# by shifting `a` 8 bits to the left and holding | |
# `b` in the rightmost 8 bits: | |
# | |
# 0000 0000 1010 1111 | |
# ^ | |
# | |
# 1010 1111 0000 0000 | |
# ^ | |
# | |
# OR 0000 0000 1100 0011 | |
# ------------------- | |
# 1010 1111 1100 0011 | |
# | |
def word16(a, b) | |
(a << 8) | b | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment