How can I convert a binary file to the text declaring a C/C++ array with that content?

On Debian and other Linux distros is installed by default (along with vim) the xxd tool, which, given the -i option, can do what you want: matteo@teodeb:~/Desktop$ echo Hello World\! > temp matteo@teodeb:~/Desktop$ xxd -i temp unsigned char temp[] = { 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x0a }; … Read more

Binary representation of float in Python (bits not hex)

You can do that with the struct package: import struct def binary(num): return ”.join(‘{:0>8b}’.format(c) for c in struct.pack(‘!f’, num)) That packs it as a network byte-ordered float, and then converts each of the resulting bytes into an 8-bit binary representation and concatenates them out: >>> binary(1) ‘00111111100000000000000000000000’ Edit: There was a request to expand the … Read more

Flask to return image stored in database

Create a response object with the data and then set the content type header. Set the content disposition header to attachment if you want the browser to save the file instead of displaying it. @app.route(‘/images/<int:pid>.jpg’) def get_image(pid): image_binary = read_image(pid) response = make_response(image_binary) response.headers.set(‘Content-Type’, ‘image/jpeg’) response.headers.set( ‘Content-Disposition’, ‘attachment’, filename=”%s.jpg” % pid) return response Relevant: werkzeug.Headers … Read more

Convert binary to ASCII and vice versa

For ASCII characters in the range [ -~] on Python 2: >>> import binascii >>> bin(int(binascii.hexlify(‘hello’), 16)) ‘0b110100001100101011011000110110001101111’ In reverse: >>> n = int(‘0b110100001100101011011000110110001101111’, 2) >>> binascii.unhexlify(‘%x’ % n) ‘hello’ In Python 3.2+: >>> bin(int.from_bytes(‘hello’.encode(), ‘big’)) ‘0b110100001100101011011000110110001101111’ In reverse: >>> n = int(‘0b110100001100101011011000110110001101111’, 2) >>> n.to_bytes((n.bit_length() + 7) // 8, ‘big’).decode() ‘hello’ To support all … Read more

Reading integers from binary file in Python

The read method returns a sequence of bytes as a string. To convert from a string byte-sequence to binary data, use the built-in struct module: http://docs.python.org/library/struct.html. import struct print(struct.unpack(‘i’, fin.read(4))) Note that unpack always returns a tuple, so struct.unpack(‘i’, fin.read(4))[0] gives the integer value that you are after. You should probably use the format string … Read more

Converting from an integer to its binary representation

The strconv package has FormatInt, which accepts an int64 and lets you specify the base. n := int64(123) fmt.Println(strconv.FormatInt(n, 2)) // 1111011 DEMO: http://play.golang.org/p/leGVAELMhv http://golang.org/pkg/strconv/#FormatInt func FormatInt(i int64, base int) string FormatInt returns the string representation of i in the given base, for 2 <= base <= 36. The result uses the lower-case letters ‘a’ … Read more

C++ – Decimal to binary converting

std::bitset has a .to_string() method that returns a std::string holding a text representation in binary, with leading-zero padding. Choose the width of the bitset as needed for your data, e.g. std::bitset<32> to get 32-character strings from 32-bit integers. #include <iostream> #include <bitset> int main() { std::string binary = std::bitset<8>(128).to_string(); //to binary std::cout<<binary<<“\n”; unsigned long decimal … Read more

How to remove unused objects from a git repository?

I answered this elsewhere, and will copy here since I’m proud of it! … and without further ado, may I present to you this useful script, git-gc-all, guaranteed to remove all your git garbage until they might come up with extra config variables: git -c gc.reflogExpire=0 -c gc.reflogExpireUnreachable=0 \ -c gc.rerereresolved=0 -c gc.rerereunresolved=0 \ -c … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)