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 8737423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:31:08+00:00 2026-06-13T10:31:08+00:00

I am reading a file (disk images and /dev/sda and likes) in python using

  • 0

I am reading a file (disk images and /dev/sda and likes) in python using binary mode. I am reading the first sector (512 bytes) and am trying to print disk information (ref: Wikipedia Articles).

To interpret things like the 32bit LBA of first absolute sector in the partition, I am doing things like –

def int32(bytes):
    return int(bytes[3]<<24|bytes[2]<<16|bytes[1]<<8|bytes[0])

def int16(bytes):
    return int(bytes[1]<<8|bytes[0])

print('LBA:',int32(partitionEntry[8:12]))

Is there native wat to do this in python? As typecasting intVar=(int*)someBasePointer in C


Added after marking as answered:

Is there a way to do this for odd bit structure? Like the CHS. C (cylinders) are 10 bits 8 bits from one byte and 2 from the other byte. My current approach for this is

def getCHS(bytes):
    c=bytes[1]&3<<8|bytes[2]
    h=bytes[0]
    s=bytes[1]&63
    return {'c':c,'s':s,'h':h}
  • 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-13T10:31:09+00:00Added an answer on June 13, 2026 at 10:31 am

    You want the struct.unpack() function.

    import struct
    
    def int32(some_bytes):
        return struct.unpack("i", some_bytes)[0]
    
    def int16(some_bytes):
        return struct.unpack("h", some_bytes)[0]
    

    If you’re reading from a system that uses different endian values than the current system, you may need to specify the endianness in the format string as well.

    (i and h are the signed int versions; if you want unsigned, use I and H)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm reading a file using bufferedreader, so lets say i have line = br.readLine();
I am reading .csv file through PHP using fgetcsv(). Now I am able to
I am reading one file using ruby in which I have written regexp in
Does anyone know how to read an image from .res file on disk, using
My code copies files from ftp (using text transfer mode) to local disk and
I'm reading a file from a SQL server and writing it to disk temporarily
I am having a problem reading png images from STDIN using PIL. When the
I'm using ImageMagick's convert program to split a pdf file into individual images. It
i just create reading file feature. In general, what kind PHP class that usually
I am reading a file by character but I want to skip number of

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.