-
-
Save rbertucat/1973809 to your computer and use it in GitHub Desktop.
part 21 encode/decode binary
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
def values = ["0", "1", "111011", "100100101010", "001101", "01000", "10101010110111110110000"] | |
def binaries = [] | |
values.each { value -> | |
def i = Long.parseLong(value, 2) | |
def rem = (4 - i.toString().size()%4)%4 | |
binaries << (rem + Long.toHexString(i)).toUpperCase() | |
} | |
assert binaries == ["30", "31", "23B", "092A", "2D", "38", "1556FB0"] | |
def uncode = [] | |
binaries.each { | |
int i = Integer.parseInt(it.substring(1), 16) | |
uncode << Integer.toBinaryString(i) | |
} | |
assert uncode.collect {Long.parseLong(it, 2)} == values.collect {Long.parseLong(it, 2)} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment