Convert Binary to Ascii and Vice Versa

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 Unicode characters in Python 3:

def text_to_bits(text, encoding='utf-8', errors='surrogatepass'):
bits = bin(int.from_bytes(text.encode(encoding, errors), 'big'))[2:]
return bits.zfill(8 * ((len(bits) + 7) // 8))

def text_from_bits(bits, encoding='utf-8', errors='surrogatepass'):
n = int(bits, 2)
return n.to_bytes((n.bit_length() + 7) // 8, 'big').decode(encoding, errors) or '\0'

Here's single-source Python 2/3 compatible version:

import binascii

def text_to_bits(text, encoding='utf-8', errors='surrogatepass'):
bits = bin(int(binascii.hexlify(text.encode(encoding, errors)), 16))[2:]
return bits.zfill(8 * ((len(bits) + 7) // 8))

def text_from_bits(bits, encoding='utf-8', errors='surrogatepass'):
n = int(bits, 2)
return int2bytes(n).decode(encoding, errors)

def int2bytes(i):
hex_string = '%x' % i
n = len(hex_string)
return binascii.unhexlify(hex_string.zfill(n + (n & 1)))

Example

>>> text_to_bits('hello')
'0110100001100101011011000110110001101111'
>>> text_from_bits('110100001100101011011000110110001101111') == u'hello'
True

How to implement a binary to ascii conversion? (and vice versa)

Google for Base64, and use Apache commons codec to have a ready to use implementation.

Converting Binary to ASCII, and ASCII to Binary

If you want to decode and encode, have this solutions

Encode ascii to bin

def toBinary(string):
return "".join([format(ord(char),'#010b')[2:] for char in string])

Encode bin to ascii

def toString(binaryString):
return "".join([chr(int(binaryString[i:i+8],2)) for i in range(0,len(binaryString),8)])

Python Binary string to ASCII text

Use following one liner

x = chr(int('010101010',2))

will work.

Building a code for ASCII to binary vice versa in C++ and running into a weird error

These are two statements and you need a semicolon between these two statements.

ascii=input[x] char* binary_reverse=new char [9];

Something like:

ascii=input[x];
char* binary_reverse=new char [9];

Convert a string of binary into an ASCII string (C++)

An alternative if you're using C++11:

#include <iostream>
#include <string>
#include <sstream>
#include <bitset>

int main()
{
std::string data = "01110100011001010111001101110100";
std::stringstream sstream(data);
std::string output;
while(sstream.good())
{
std::bitset<8> bits;
sstream >> bits;
char c = char(bits.to_ulong());
output += c;
}

std::cout << output;

return 0;
}

Note that bitset is part of C++11.

Also note that if data is not well formed, the result will be silently truncated when sstream.good() returns false.

Binary to ASCII in Python

From your example, your input looks like a string of a binary formatted number.

If so, you don't need a dictionnary for that:

def byte_to_char(input):
return chr(int(input, base=2))

Using the data you gave in the comments, you have to split your binary string into bytes.

input ='01010100011010000110100101110011001000000110100101110011001000000110101001110101011100110111010000100000011000010010000001110100011001010111001101110100001000000011000100110000001110100011000100110000'
length = 8
input_l = [input[i:i+length] for i in range(0,len(input),length)]

And then, per byte, you convert it into a char:

input_c = [chr(int(c,base=2)) for c in input_l]
print ''.join(input_c)

Putting it all together:

def string_decode(input, length=8):
input_l = [input[i:i+length] for i in range(0,len(input),length)]
return ''.join([chr(int(c,base=2)) for c in input_l])

decode(input)
>'This is just a test 10:10'


Related Topics



Leave a reply



Submit