Posts

Showing posts with the label Binary

Binary Puzzle Solver

Answer : Brachylog, 34 bytes {ℕ<2}ᵐ²&≜{d?ọᵐctᵐ=&{ḅlᵐ⌉<3}ᵐ}&\↰₂& Try it online! This is pretty damn slow, so the test case on TIO is 4x4. I am currently running the 6x6 test case on my computer to see how much time it takes. This takes a list of lists as input. The unknown values should be indicated with variables, that is with all-uppercase strings (and they should all be different, as otherwise you would be indicating that some cells must have the same value) Explanation We constrain the values to be in {0,1} , then we try instantiations of the variables until one respects all 3 rules. This is why this is so slow (because it will try all of them until finding one; and because in that case Brachylog is not implemented well enough so that constraints can be imposed before trying a possible matrix). & Output = Input { }ᵐ² Map two levels on the Input (i.e. each cell): ℕ<2 ...

Binary To String/Text In Python

Answer : It looks like you are trying to decode ASCII characters from a binary string representation (bit string) of each character. You can take each block of eight characters (a byte), convert that to an integer, and then convert that to a character with chr() : >>> X = "0110100001101001" >>> print(chr(int(X[:8], 2))) h >>> print(chr(int(X[8:], 2))) i Assuming that the values encoded in the string are ASCII this will give you the characters. You can generalise it like this: def decode_binary_string(s): return ''.join(chr(int(s[i*8:i*8+8],2)) for i in range(len(s)//8)) >>> decode_binary_string(X) hi If you want to keep it in the original encoding you don't need to decode any further. Usually you would convert the incoming string into a Python unicode string and that can be done like this (Python 2): def decode_binary_string(s, encoding='UTF-8'): byte_string = ''.join(chr(int(s[i*8:i*8+8]...

ASCII To Binary And Binary To ASCII Conversion Tools?

Answer : $ echo AB | perl -lpe '$_=unpack"B*"' 0100000101000010 $ echo 0100000101000010 | perl -lpe '$_=pack"B*",$_' AB -e expression evaluate the given expression as perl code -p : sed mode. The expression is evaluated for each line of input, with the content of the line stored in the $_ variable and printed after the evaluation of the expression . -l : even more like sed : instead of the full line, only the content of the line (that is, without the line delimiter) is in $_ (and a newline is added back on output). So perl -lpe code works like sed code except that it's perl code as opposed to sed code. unpack "B*" works on the $_ variable by default and extracts its content as a bit string walking from the highest bit of the first byte to the lowest bit of the last byte. pack does the reverse of unpack . See perldoc -f pack for details. With spaces: $ echo AB | perl -lpe '$_=join " ", unpack...

C Read Binary Stdin

Answer : What you need is freopen() . From the manpage: If filename is a null pointer, the freopen() function shall attempt to change the mode of the stream to that specified by mode, as if the name of the file currently associated with the stream had been used. In this case, the file descriptor associated with the stream need not be closed if the call to freopen() succeeds. It is implementation-defined which changes of mode are permitted (if any), and under what circumstances. Basically, the best you can really do is this: freopen(NULL, "rb", stdin); This will reopen stdin to be the same input stream, but in binary mode. In the normal mode, reading from stdin on Windows will convert \r\n (Windows newline) to the single character ASCII 10. Using the "rb" mode disables this conversion so that you can properly read in binary data. freopen() returns a filehandle, but it's the previous value (before we put it in binary mode), so don't use i...

Binary To Hexadecimal And Decimal In A Shell Script

Answer : It's fairly straightforward to do the conversion from binary in pure bash ( echo and printf are builtins): Binary to decimal $ echo "$((2#101010101))" 341 Binary to hexadecimal $ printf '%x\n' "$((2#101010101))" 155 Going back to binary using bash alone is somewhat more complex, so I suggest you see the other answers for solutions to that. Assuming that by binary, you mean binary data as in data with any possible byte value including 0, and not base-2 numbers: To convert from binary, od (standard), xxd (comes with vim ) or perl 's unpack come to mind. od -An -vtu1 # for decimal od -An -vtx1 # for hexadecimal xxd -p # for hexa perl -pe 'BEGIN{$\="\n";$/=\30};$_=unpack("H*",$_)' # like xxd -p # for decimal: perl -ne 'BEGIN{$\="\n";$/=\30;$,=" "}; print unpack("C*",$_)' Now, to convert back to binary, awk (standard), xxd -r or perl 's pack : ...