Skip to content

Instantly share code, notes, and snippets.

@irsdl
Last active March 19, 2018 10:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save irsdl/bfab25be3f236bcb6a244b5a022225be to your computer and use it in GitHub Desktop.
Save irsdl/bfab25be3f236bcb6a244b5a022225be to your computer and use it in GitHub Desktop.
Convert from iso-8859-1 to binary
# Convert from iso-8859-1, utf-8ed to binary!
# Useful for file disclosure when encoding can be controlled
# The following C# code shows an example (result is iso-8859-1, utf-8ed!):
###string encoding = "iso-8859-1";
######string sourceFile = @"Newtonsoft.Json.dll";
######
######public void test()
######{
#########System.Text.Encoding myEncoding = Encoding.GetEncoding(encoding);
#########String sourceFilePath = Directory.GetCurrentDirectory() + @"\" + sourceFile;
#########String targetFilePath = Directory.GetCurrentDirectory() + @"\" + sourceFile+"_"+encoding+".txt";
#########StreamReader myStreamReader = new StreamReader(sourceFilePath, myEncoding);
#########Object created = myStreamReader.ReadToEnd();
#########using (StreamWriter outputFile = new StreamWriter(targetFilePath))
#########{
############outputFile.WriteLine(created);
#########}
######}
out_file = open('result.bin', 'wb') # Output file
with open("file.txt", "rb") as binary_file:
# Read the whole file at once
#data = binary_file.read()
#print(data)
# Seek position and read N bytes
binary_file.seek(0, 2)
num_bytes = binary_file.tell() # Get the file size
print num_bytes
pos = 0
while (pos < num_bytes):
binary_file.seek(pos) # Go to beginning of where we should be!
first_byte=binary_file.read(1)
if (num_bytes == pos + 1): # identify the last byte
out_file.write(first_byte)
break
second_byte=binary_file.read(1)
result = first_byte # for now...
if (bytearray(second_byte)[0] > 127):
if(first_byte==b"\xC2"):
# This byte is unnecessary and can be ignored! the next byte is intact
result=second_byte
pos = pos + 1
elif(first_byte==b"\xC3"):
# Now we the next byte needs be added by 40 in hex or 64 decimal
result=chr(bytearray(second_byte)[0]+64)
pos = pos + 1
pos = pos + 1
out_file.write(result)
out_file.close()
print "done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment