Bitcoin Forum

Other => Beginners & Help => Topic started by: whohackedme on August 22, 2018, 05:31:41 PM



Title: Hex to Binary script
Post by: whohackedme on August 22, 2018, 05:31:41 PM
Can someone help me with some simple python code?

I am trying to use a python script to read from a file with hex keys and outputting the binary keys to another file.

Trying to get this working, I have messedup my original code that does not work now.

Here is the code I was using to convert hex to binary one at a time:
Code:
print"====================================================="
print" HEXADECIMAL TO BINARY CONVERSION"
print"====================================================="
hex=input("Enter long hexadecimal to be converted: ")
s=hex
print "The binary of the hexadecimal is [",format(s, '0256b'),']'
print"====================================================="

I have messed this up so that it gives the following error:
SyntaxError: unexpected EOF while parsing

I would like to modify this to read from input file
hex_keys.txt
contain one hex key per line

Then output to binary_keys.txt
containing one binary key per line.

Thanks!


Title: Re: Hex to Binary script
Post by: HCP on August 23, 2018, 11:44:24 PM
I have a small script that reads HEX and outputs WIF... Fairly sure I had a similar EOF issue before I started using the "line.strip()" to strip out stray EOL and other whitespace characters (like an empty line at the end of the file etc).

it should be fairly easy to modify this code so instead of outputting WIF, it simply converts to binary.
Code:
import bitcoin

file = open("hex.txt","r")
wif = open("wif.txt","w")
wifc = open("wifc.txt","w")
for line in file:
  print(line)
  wif.write(bitcoin.encode_privkey(line.strip(),'wif') + "\n")
  wifc.write(bitcoin.encode_privkey(line.strip(),'wif_compressed') + "\n")

file.close()
wif.close()
wifc.close()
NOTE: I believe that this script is using the deprecated pybitcointools (https://github.com/vbuterin/pybitcointools) library...

Anyway, are you able to show your code here so myself (and others) can try and debug why you're getting that error? ???