This code accepts a bitstring and converts it to a character string. The bitstring comes from a txt file that's read into the client, converted to bitstring then sent to the server.
def getChar(charstr):
char = []
for byte in charstr.split():
char.append(chr(int(''.join([str(bit) for bit in byte]), 2)))
return ''.join(char)
The bitstring in question is:
10010011101101111000011001011100001110001111010001101101110010111011101110100100000110010110000110010110000100001
which calls out this line
char.append(chr(int(''.join([str(bit) for bit in byte]), 2)))
and gives the error:
OverflowError: Python int too large to convert to C long.
How would I fix this?
Full Server Code:
import socket
import random
import sys
def getCaesar(message, key):
enc = ""
for char in message:
if char == ' ':
enc = enc + char
elif char.isupper():
enc = enc + chr((ord(char)+key-65)%26+65)
elif char.islower():
enc = enc + chr((ord(char) + key - 97) % 26 + 97)
else:
enc = enc +chr((ord(char) + key - 33) % 32 + 33)
return enc
def getBinary(bitstr):
bit=' '.join(format(ord(char), 'b') for char in bitstr)
return bit
def getChar(charstr):
char = []
for byte in charstr.split():
char.append(chr(int(''.join([str(bit) for bit in byte]), 2)))
return ''.join(char)
def getBitstr(k):
result=""
for num in range(0,k):
result=''.join(str(random.randint(0,1))for num in range(k))
return result
def getXor(a,b):
a=a.replace(" ","")
b=b.replace(" ","")
result = int(a,2) ^ int(b,2)
return '{0:b}'.format(result)
def Decrypt(message, key):
enc = ""
for char in message:
if char == ' ':
enc = enc + char
elif char.isupper():
enc = enc + chr((ord(char)-key-65)%26+65)
elif char.islower():
enc = enc + chr((ord(char) - key - 97) % 26 + 97)
else:
enc = enc +chr((ord(char) - key - 33) % 32 + 33)
return enc
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host=socket.gethostname()
port=4000
s.bind((host,port))
s.listen(5)
print("Listening...")
while True:
c,addr=s.accept()
print("Got connection from ",addr)
print("Receiving...")
l = c.recv(4096).decode()
a=str(l)
cipherF=open("ciphertext.txt",mode='r')
caesar=cipherF.read()
key=Decrypt(caesar,4)
key=''.join(key)
b=str(key)
content=getXor(a,b)
dec=getChar(content)
if not l:
break
print("Decrypting...")
print(dec)
s.close()
I can't tell for sure because you haven't posted a Minimal, Complete, and Verifiable Example along with the expected/desired output in order to be able to tell if this what you want—so it's basically just a guess—but at least it doesn't raise an OverflowError
exception...
Note: This worked in both Python 2 and 3 (or at least did the same thing).
#!/usr/bin/env python2
def getChar(charstr):
char = []
# for byte in charstr.split(): # Not needed.
for byte in charstr:
char.append(chr(int(''.join([str(bit) for bit in byte]), 2)))
return ''.join(char)
teststr = ('100100111011011110000110010111000011100011110100011011011'
'10010111011101110100100000110010110000110010110000100001')
result = getChar(teststr)
print(repr(result))
Output:
'\\x01\\x00\\x00\\x01\\x00\\x00\\x01\\x01\\x01\\x00\\x01\\x01\\x00\\x01\\x01\\x01\\x01\\x00\\x00\\x00\\x00\\x01\\x01\\x00\\x00\\x01\\x00\\x01\\x01\\x01\\x00\\x00\\x00\\x00\\x01\\x01\\x01\\x00\\x00\\x00\\x01\\x01\\x01\\x01\\x00\\x01\\x00\\x00\\x00\\x01\\x01\\x00\\x01\\x01\\x00\\x01\\x01\\x01\\x00\\x00\\x01\\x00\\x01\\x01\\x01\\x00\\x01\\x01\\x01\\x00\\x01\\x01\\x01\\x00\\x01\\x00\\x00\\x01\\x00\\x00\\x00\\x00\\x00\\x01\\x01\\x00\\x00\\x01\\x00\\x01\\x01\\x00\\x00\\x00\\x00\\x01\\x01\\x00\\x00\\x01\\x00\\x01\\x01\\x00\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x00\\x01'
The chr
function converts an integer to the associated character. Its behavior differs between python versions, as follows:
In python 2.x, chr(n)
will return a one-character string consisting of the ASCII character with value n
if 0<=n<=255
. If n
is outside of [0, 255]
but fits in a 64-bit signed integer, chr(n)
raises a ValueError
complaining that the arg isn't in range(256)
. If n
doesn't fit in a 64-bit signed integer, chr(n)
raises an OverflowError
complaining that "Python int too large to convert to C long" (as observed in the question).
In python 3.x, chr(n)
will return a one-character string consisting of the unicode character with value n
if 0<=n<0x110000
. If n
is outside of [0, 0x110000)
but fits in a 32-bit signed integer, chr(n)
raises a value error complaining that the arg isn't in range(0x110000)
. If n
doesn't fit in a 32-bit signed integer, chr(n)
raises an OverflowError
complaining that "signed integer is greater than maximum" or "signed integer is less than minimum" (depending on the sign of n
).
The reason this is happening in the posted code is that string.split()
splits a string at whitespace, so the for byte in charstr.split()
loop executes exactly once, with byte == charstr
. Similarly, ''.join([str(bit) for bit in byte]
is just a long way of saying byte
(ie "the string generated by concatenating each character of byte
"). So the code in question is converting the entire 113-bit string to an integer. The first bit is a 1, so it takes at least 113 bits to store it, which means it definitely won't fit in either a 32- or 64-bit signed integer, so it falls into the OverflowError
case on both 2.x and 3.x.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.