How do I make a text to binary? [duplicate] - python

This question already has answers here:
How to convert string to binary?
(9 answers)
Closed 2 years ago.
So I have been trying to make a text to binary converter but what I do is I convert every single letter and symbol to the binary equivalent but as you can imagine this takes long and i'm wondering if there is a shorter way to do this.

Text to binary convert :
name = "Name"
binary = ' '.join(format(ord(x), 'b') for x in name)
Binary to Text :
binary = '100001'
binary_values = binary.split()
ascii_string = ""
for binary_value in binary_values:
an_integer = int(binary_value, 2)
ascii_character = chr(an_integer)
ascii_string += ascii_character
print(ascii_string)
Here is my git repo

This programm convert text to binaries list :
a_string = "abc"
a_byte_array = bytearray(a_string, "utf8") #Create bytearray
byte_list = []
for byte in a_byte_array:
binary_representation = bin(byte) #Convert to binary
byte_list.append(binary_representation) #Add to list
print(byte_list)

There are plenty of binary modules on PyPI. You could just have the input go through one and print the result.

Related

Read string from html code and save to file [duplicate]

This question already has answers here:
Converting special characters to regular c#
(4 answers)
Closed 2 years ago.
How do I translate the bytes %C3%B8 in "testdoc%C3%B8%C3%B8%C3%B8.txt" to ø?
I tried the following which did not work:
var cont = response.Content.Headers.ContentDisposition?.FileName;
var bytes = Encoding.UTF8.GetBytes(cont);
var test = new string(bytes.Select(b => (char)b).ToArray());
var yourText = System.Text.Encoding.UTF8.GetString(bytes);
Don't bother with converting it to bytes at all. As has been noted in the comments, this is URL encoding, not UTF8.
Use HttpUtility in the System.Web namespace:
string input = "testdoc%C3%B8%C3%B8%C3%B8.txt";
string output = HttpUtility.UrlDecode(input);
Console.WriteLine(output); // testdocøøø.txt
Try it online

How to convert Binary to Strings?

I am currently working on a binary encryption code: [Sender(Msg Input=> Binary Conversion)] : [Receiver (Binary Conversion => Msg Output)]
As of now I am able to convert text based Msgs , e.g) How are you? etc.
print("Enter Msg:")
def Binary_Encryption(message):
message = ''.join(format(i, 'b') for i in bytearray(message, encoding ='utf-8'))
print(message)
Binary_Encryption(input("").replace (" ","\\"))
Output: 10010001101111111011110111001100001111001011001011011100111100111011111110101111111
After the binary string is obtained, by just copying the string and placing it within this block of code will decrypt it.
def Binary_Decryption(binary):
string = int(binary, 2)
return string
bin_data = (input("Enter Binary:\n"))
str_data =''
for i in range(0, len(bin_data), 7):
temp_data = bin_data[i:i + 7]
decimal_data = Binary_Decryption(temp_data)
str_data = str_data + chr(decimal_data)
print("Decrypted Text:\n"+str_data.replace("\\"," "))
Output: How are you?
But I am not able to convert a certain inputs , e.g) ?? , 8879 , Oh! How are You? etc.
basically the msgs that are not being converted are Msgs with multiple uses of numbers or special
characters.
Msg Input for ?? gives "⌂▼" and 8879 gives "qc?☺" while Oh! How are You? gives "OhC9◄_o9CeK93_k▼
I think the problem is that the special characters (!, ?) contains only 6 bits, while the other characters 7.This messes things up if there are other characters behind the special one I think. Maybe something like this should work. There is probably a better way to solve this though.
def Binary_Encryption(message):
s = ""
for i in bytearray(message, encoding="utf-8"):
c = format(i, "b")
addon = 7 - len(c)
c = addon * "0" + c # prepend 0 if len shorter than 7
s += c # Add to string
print(s)
Your problem is that you are copying the output from binary_encrypt directly which truncate leading zeros so 8 instead of being 00111000 it became 111000 which result in 2 bits being used from next ASCII binary character since ASCII characters are represented as 8-bits values to print number 8897 use0011100000111000001110010011011100001010 as input to binary_decrypt. look for ASCII table to see the binary equivalents for each character.Just edit your code like this.
print("Enter Msg:")
def Binary_Encryption(message):
# pass 08b to format
message = ''.join(format(i, '08b') for i in bytearray(message, encoding ='utf-8'))
print(message)
Binary_Encryption(input("").replace (" ","\\"))

If I have hexadecimal numbers with some 1 digit long like 0x1 and some 5 digits long like 0x1e4b1, how do I write code to make them all 8 digits long? [duplicate]

This question already has answers here:
Decorating Hex function to pad zeros
(8 answers)
Closed 2 years ago.
I have some hexadecimal numbers like this in a .txt file: 0x1, 0x2, 0x1e4b1, 0x5b, 0x80, 0x52, 0x111, 0x6b0d, 0x4e, 0x34a, 0x2067, 0x6ef3, 0x1cf, 0x1b, 0x15b, 0x4f, 0xba8, 0x319. What I am trying to do now is overwrite the contents (using code) of the file and make the end result like this: 0x00000001, 0x00000002, 0x0001e4b1, 0x0000005b, 0x00000080, 0x00000052, 0x00000111, 0x00006b0d, 0x0000004e, 0x0000034a, 0x00002067, 0x00006ef3, 0x000001cf, 0x0000001b, 0x0000015b, 0x0000004f, 0x00000ba8, 0x00000319.
Here is some necessary background info: I have a .txt file with some numbers on it that are all separated by commas. Then, using Python code, I opened it and read it. After that, I made a list out of all of the numbers that were on the file. When I made a list, the numbers were all strings (example: '9', '8', etc.), so I used some Python code to convert the values of the list into integers. After doing that, I converted all of the integers into a hexadecimal form. Then, I took the integers in hexadecimal form and put them into a new .text file called Hexadecimal Numbers. Now, what I am trying to do is overwrite the Hexadecimal Numbers file in order to replace it with the hexadecimal numbers padded with zeros to have them all 8 digits.
I have tried to search this on Google, but couldn't find something specific to this. Please help! Thank you! If you still don't understand my question, make sure to comment and ask me.
Picture before Output:
Expected Output:
Here is my code so far:
my_file = open(r'C:\Users\KAPS\Downloads\List of Numbers File.txt', encoding='utf-8-sig') content = my_file.read() print(content)
content_list = content.split(",") my_file.close() print(content_list)
for i in range(0, len(content_list)):
content_list[i] = int(content_list[i]) print(str(content_list))
hex_list = [hex(int(x)) for x in content_list] print(hex_list)
with open(r'C:\Users\KAPS\Downloads\Hexadecimal Numbers.txt', 'w') as my_file:
my_file.write(', '.join(hex_list))
padded = '0x' + '0' * (10 - len(mystring)) + mystring[2:]
We get the hex-number string, then we:
Slice the prefix '0x'
Pad with 8-(len(s)-2) zeroes, as we have to subtract 2 from the length of the string because they are part of the '0x' prefix
Add the actual hex-number
Simple method:
def pad_hex_strings(s):
# Add the '0x' prefix of a hex-number
padded = '0x'
# Add the zeroes padding
padded += '0' * (8-(len(s)-2))
# Adding the actual hex-number, going from index 2 till the end of the string
for i in range(2, len(s)):
padded += s[i]
return padded
Using provided code:
def pad_hex_strings(s):
padded = '0x'
padded += '0' * (8-(len(s)-2))
# Adding the actual hex-number
for i in range(2, len(s)):
padded += s[i]
return padded
my_file = open(r'C:\Users\KAPS\Downloads\List of Numbers File.txt', encoding='utf-8-sig') content = my_file.read() print(content)
content_list = content.split(",") my_file.close() print(content_list)
padded_strings = [pad_hex_strings(s) for s in content_list]
with open(r'C:\Users\KAPS\Downloads\Hexadecimal Numbers.txt', 'w') as my_file:
my_file.write(', '.join(padded_strings))
As a list of string, as we read from a file:
list_strings = ['0x1', '0x2', '0x1e4b1', '0x5b', '0x80', '0x52', '0x111', '0x6b0d', '0x4e', '0x34a', '0x2067', '0x6ef3', '0x1cf', '0x1b', '0x15b', '0x4f', '0xba8']
def pad_hex_strings(s, total_length=8, prefix_length=2):
# Slicing prefix of '0x', padding with zeroes, and writing the rest of the string
return s[:prefix_length] + (total_length-(len(s)-prefix_length)) * '0' + s[prefix_length:]
# Apply padding on each item of the list
padded_strings = [pad_hex_strings(s) for s in list_strings]
print(f'Padded Strings:\n{padded_strings}')
# Write to output file
with open("Hexadecimal Numbers.txt", "w") as text_file:
text_file.write(', '.join(padded_strings))
If it were numbers:
def pad_hex_numbers(n, total_length=8):
prefix_length = 2 # For '0x'
# Formatting the hex number to be padded with zeroes
return '{0:#0{1}x}'.format(n, total_length+prefix_length)
list_numbers = [ 0x1, 0x2, 0x1e4b1, 0x5b, 0x80, 0x52, 0x111, 0x6b0d, 0x4e, 0x34a, 0x2067, 0x6ef3, 0x1cf, 0x1b, 0x15b, 0x4f, 0xba8 ]
# Apply padding on each item of the list
padded_numbers = [pad_hex_numbers(n) for n in list_numbers]
print(f'Padded Numbers:\n{padded_numbers}')
# Write to output file
with open("Hexadecimal Numbers.txt", "w") as text_file:
text_file.write(', '.join(padded_numbers))
I'm new to python, so this might not work, but you could try to use the
len(hex(x))
to find the length of the hexadecimal, then use a if statement for each length of hexadecimals where you would convert it to strings and then add the 0s at the end of it? Then you could convert it back to a hexadecimal with the added zeros. let me know if it works!

Python Print Hex variable

I have hex variable that I want to print as hex
data = '\x99\x02'
print (data)
Result is: ™
I want to the python to print 0x9902
Thank you for your help
Please check this one.
data = r'\x99\x02'
a, b = [ x for x in data.split(r'\x') if x]
d = int(a+b, base=16)
print('%#x'%d)
You have to convert every char to its number - ord(char) - and convert every number to hex value - '{:02x}'.format() - and concatenate these values to string. And add string '0x'.
data = '\x99\x02'
print('0x' + ''.join('{:02x}'.format(ord(char)) for char in data))
EDIT: The same but first string is converted to bytes using encode('raw_unicode_escape')
data = '\x99\x02'
print('0x' + ''.join('{:02x}'.format(code) for code in data.encode('raw_unicode_escape')))
and if you have already bytes then you don't have to encode()
data = b'\x99\x02'
print('0x' + ''.join('{:02x}'.format(code) for code in data))
BTW: Similar way you can convert to binary using {:08b}
data = '\x99\x02'
print(''.join('{:08b}'.format(code) for code in data.encode('raw_unicode_escape')))

How to remove character outside a string from a tab delimited text [duplicate]

This question already has answers here:
How do I get rid of the b-prefix in a string in python?
(9 answers)
Closed 4 years ago.
I have a file lets say "Mrinq_Parts_Available.txt" which looks like this.
Source Date Category SubCategory Present Description Value Units Vendor Part No Package Box Name Location Quantity Ordered Used MOQ=1 MOQ=100 MOQ=1000 Comments Link
Digikey 29-May-15 RF Amplifier No 0.5 W RFMD RFPA3807 SOIC8 10 0 3.4 5V http://www.digikey.com/product-detail/en/RFPA3807TR13/689-1073-1-ND/2567207
I have a python code which does split of those lines.
def removeEmptyLines(inputFile):
with open(inputFile, 'rb') as f:
d = f.readlines()
k = []
for i in d:
k.append(i.split())
print (k)
if __name__=="__main__":
parts_database_file = "Mrinq_Parts_Available.txt"
removeEmptyLines(parts_database_file)
But the output is shown like this:
[b'Source', b'Date', b'Category', b'SubCategory', b'Present', b'Description', b'Value', b'Units', b'Vendor', b'Part', b'No', b'Package', b'Box', b'Name', b'Location', b'Quantity', b'Ordered', b'Used', b'MOQ=1', b'MOQ=100', b'MOQ=1000', b'Comments', b'Link']
[b'Digikey', b'29-May-15', b'RF', b'Amplifier', b'No', b'0.5', b'W', b'RFMD', b'RFPA3807', b'SOIC8', b'10', b'0', b'3.4', b'5V', b'http://www.digikey.com/product-detail/en/RFPA3807TR13/689-1073-1-ND/2567207']
How do I remove the 'b' preceding each parsed data?
Your file is clearly an ASCII file, so you should open it as an ASCII :
with open(inputFile, 'r') as f:

Categories

Resources