Bitcoin Forum
December 11, 2018, 11:55:21 PM *
News: Latest Bitcoin Core release: 0.17.0 [Torrent].
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Hex to Binary script  (Read 45 times)
whohackedme
Newbie
*
Offline Offline

Activity: 30
Merit: 0


View Profile
August 22, 2018, 05:31:41 PM
 #1

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!
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
HCP
Hero Member
*****
Offline Offline

Activity: 812
Merit: 989

<insert witty quote here>


View Profile
August 23, 2018, 11:44:24 PM
Merited by theymos (5)
 #2

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 library...

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

Pages: [1]
  Print  
 
Jump to:  

Sponsored by , a Bitcoin-accepting VPN.
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!