0

here is my output.txt file

4f337d5000000001
4f337d5000000001
0082004600010000
0082004600010000
334f464600010000
334f464600010000
[... many values omitted ...]
334f464600010000
334f464600010000
4f33464601000100
4f33464601000100

how i can change these values into decimal with the help of python and save into a new .txt file..

holdenweb
  • 33,305
  • 7
  • 57
  • 77
  • Welcome to Stackoverflow. Please show your effort by posting your code and what you have tried to solve the problem. Before doing so, please take the time to go through the [How to ask a good question](https://stackoverflow.com/help/how-to-ask) section. Additionally read the [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – Roan Aug 28 '17 at 10:20
  • Possible duplicate of [Convert hex string to int in Python](https://stackoverflow.com/questions/209513/convert-hex-string-to-int-in-python) – holdenweb Aug 28 '17 at 10:37
  • There are many different ways that a hex string might represent a numerical value. Can you tell us anything more about how the values were encoded in the first place? Or perhaps give an example of an input and output for the conversion? – Mark Dickinson Aug 28 '17 at 11:58
  • in the first string 4f = 79 in decimal, 33 = 51, 7d = 125, 50 = 80 in decimal i want output as 2-2 digits. – user8523601 Aug 28 '17 at 12:06

3 Answers3

2

Since the values are 16 hex digits long I assume these are 64-bit integers you want to play with. If the file is reasonably small then you can use read to bring in the whole string and split to break it into individual values:

with open("newfile.txt", 'w') as out_file, open("outfile,txt") as in_file:
    for hex in in_file.read().split():
        print(int(hex, 16), file=out_file)

should do this for you.

holdenweb
  • 33,305
  • 7
  • 57
  • 77
1

You can do this:

with open('output.txt') as f:
    new_file = open("new_file.txt", "w")
    for item in f.readlines():
        new_file.write(str(int(item, 16)) + "\n")
    new_file.close()
RaminNietzsche
  • 2,683
  • 1
  • 20
  • 34
0

One of the comments mentioned a shell command. This is how a Python one-liner can be invoked from the shell command line (Linux bash in this example). I/O redirection is handled by the shell.

$ python -c  $'import sys\nfor line in sys.stdin: print(int(line,16))' <hex.txt >dec.txt
VPfB
  • 14,927
  • 6
  • 41
  • 75