Skip to content

Instantly share code, notes, and snippets.

@rbertucat
Forked from mpictor/gist:1967483
Created March 4, 2012 16:42
Show Gist options
  • Save rbertucat/1973809 to your computer and use it in GitHub Desktop.
Save rbertucat/1973809 to your computer and use it in GitHub Desktop.
part 21 encode/decode binary
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