Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7877249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:18:47+00:00 2026-06-03T03:18:47+00:00

I have a binary file that need to be showed in hexadecimal. The code

  • 0

I have a binary file that need to be showed in hexadecimal. The code as follows :

file=open('myfile.chn','rb')  
for x in file:     
    i=0
    while i<len(x):
        print ("%0.2X"%(x[i]))
        i=i+1
        if (i>=10): 
            i=0
            break
file.close()

and the result i get as follows :

FF  
FF  
01  
00  
01  
00  
35  
36  
49  
EC      
.   
.   
. 

Which part of the code that i need to change in order to show the result just as follows?

FF FF 01 00 01   
00 35 36 49 EC  
.  
.  

(with a space between each byte)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T03:18:49+00:00Added an answer on June 3, 2026 at 3:18 am

    As you take only 10 elements I’d use:

    print(" ".join("%0.2X" % s for s in x[:10]))
    

    or if you want to include the whole line:

    print(" ".join("%0.2X" % s for s in x))
    

    There is still a bug from your initial version. Your input is read as one string per line. The type conversion “%0.2X” fails (“%s” works). I think you cannot read binary file per line. \n is just another byte and cannot be interpreted as newline.

    When you have a sequence of int values you can create partitions of n elements with the group method. The group method is in the itertools recipies.

    from itertools import zip_longest
    
    def grouper(n, iterable):
        args = [iter(iterable)] * n
        return zip_longest(fillvalue=None, *args)
    
    width=10
    x = range(1,99)
    for group in grouper(width, x):
        print((" ".join("%0.2X" % s for s in group if s)))
    

    Output:

    01 02 03 04 05 06 07 08 09 0A
    0B 0C 0D 0E 0F 10 11 12 13 14
    15 16 17 18 19 1A 1B 1C 1D 1E
    1F 20 21 22 23 24 25 26 27 28
    29 2A 2B 2C 2D 2E 2F 30 31 32
    33 34 35 36 37 38 39 3A 3B 3C
    3D 3E 3F 40 41 42 43 44 45 46
    47 48 49 4A 4B 4C 4D 4E 4F 50
    51 52 53 54 55 56 57 58 59 5A
    5B 5C 5D 5E 5F 60 61 62
    

    To bytes_from_file reads bytes as generator :

    def bytes_from_file(name):
        with open(name, 'rb') as fp:
            def read1(): return fp.read(1)
            for bytes in iter(read1, b""):
                for byte in bytes:
                    yield byte
    
    x = bytes_from_file('out.fmt') #replaces x = range(1,99)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a binary file of doubles that I need to load using C++.
I have a binary file of unknown format that I need to be able
I have a binary file (about 100 MB) that I need to read in quickly.
I have a binary string that I need to write to a file. I
I have a binary file that I need to insert a header at the
I have a binary file that I would like to regex search/replace hex bytes
I have response stream from a ftp web request that returns binary file. I
1) I have the binary file that I wanted to replace with a newer
So here's my issue. I have a binary file that I want to edit.
I need to convert a binary file (a zip file) into hexadecimal representation, to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.