Two’s Complement Binary in Python?

It works best if you provide a mask. That way you specify how far to sign extend. >>> bin(-27 & 0b1111111111111111) ‘0b1111111111100101’ Or perhaps more generally: def bindigits(n, bits): s = bin(n & int(“1″*bits, 2))[2:] return (“{0:0>%s}” % (bits)).format(s) >>> print bindigits(-31337, 24) 111111111000010110010111 In basic theory, the actual width of the number is a … Read more

Binary buffer in Python

You are probably looking for io.BytesIO class. It works exactly like StringIO except that it supports binary data: from io import BytesIO bio = BytesIO(b”some initial binary data: \x00\x01″) StringIO will throw TypeError: from io import StringIO sio = StringIO(b”some initial binary data: \x00\x01″)

How do I create binary patches?

Check out bsdiff and bspatch (website, manpage, paper, GitHub fork). To install this tool: Windows: Download and extract this package. You will also need a copy of bzip2.exe in PATH; download that from the “Binaries” link here. macOS: Install Homebrew and use it to install bsdiff. Linux: Use your package manager to install bsdiff.

Difference between opening a file in binary vs text [duplicate]

The link you gave does actually describe the differences, but it’s buried at the bottom of the page: http://www.cplusplus.com/reference/cstdio/fopen/ Text files are files containing sequences of lines of text. Depending on the environment where the application runs, some special character conversion may occur in input/output operations in text mode to adapt them to a system-specific … Read more

How to convert hex number to bin in Swift?

You can use NSScanner() from the Foundation framework: let scanner = NSScanner(string: str) var result : UInt32 = 0 if scanner.scanHexInt(&result) { println(result) // 37331519 } Or the BSD library function strtoul() let num = strtoul(str, nil, 16) println(num) // 37331519 As of Swift 2 (Xcode 7), all integer types have an public init?(_ text: … Read more