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

  • Home
  • SEARCH
  • 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 6676741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:01:48+00:00 2026-05-26T04:01:48+00:00

I am primarily a Python programmer, but I’ve been working with C because Python

  • 0

I am primarily a Python programmer, but I’ve been working with C because Python is too slow for graphics (20 fps moving fractals FTW). I’m hitting a sticking point though…
I wrote a little file in a hex editor to test with. When I try reading the first byte, 5A, it correctly gives me 90 with a program like this…

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
FILE *data;
int main(int argc, char* argv[])
{
    data=fopen("C:\\vb\\svotest1.vipc","r+b");
    unsigned char number;
    fread(&number,1,1,data);
    printf("%d\n",number);
}

But when I try reading the first four bytes, 5A F3 5B 20, into an integer I get 542896986

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
FILE *data;
int main(int argc, char* argv[])
{
    data=fopen("C:\\vb\\svotest1.vipc","r+b");
    unsigned long number;
    fread(&number,1,4,data);
    printf("%d\n",number);
}

It should be 1525898016!!!
The problem is it has reversed the byte order. GAH! Of course! The way this program works will depend on the machine. And now that we are on the subject, even the byte won’t work on every machine!

So I need help… In Python I can use struct.pack and struct.unpack to pack data into bytes using any format (long, short, single, double, signed, unsigned, big endian, little endian) and unpack it. I need something like this in C… I could write it myself but I don’t know how.

  • 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-05-26T04:01:48+00:00Added an answer on May 26, 2026 at 4:01 am

    The easiest way to handle this portably is probably to use htonl on the data before you write it to the file (so the file will be in network/big endian order) and ntohl when you read (converts the network order data to the local convention, whatever that is).

    If you have to do much more than a few values of one type, you may want to look into a more complete library for the purpose, such as Sun XDR or Google Protocol Buffers.

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

Sidebar

Related Questions

I use Emacs primarily for coding Python but sometimes I use IDLE. Is there
Primarily, I'm a C/C++ programmer on Unix machines... But I'd kill for browser based
I have been developing web apps (primarily) using php and a bit of python,
I'd like to calculate Hebrew dates (primarily the current Hebrew date) in Python. Which
Primarily I've done basic (novice level) software development on a Windows machine, but I've
I now primarily write in python, however I am looking for a language that
I am primarily a web developer (ruby, python) that is new to iPhone development.
I have been learning Python by following some pygame tutorials . Therein I found
I've been an avid learner of the Python language for quite some time. Having
I'm currently primarily a D programmer and am looking to add another language 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.