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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:46:06+00:00 2026-05-26T10:46:06+00:00

I want to write signed integer values into a file in a platform independent

  • 0

I want to write signed integer values into a file in a platform independent way.

If they were unsigned, I would just convert them from host byte order to LE (or BE) with the endian(3) family of functions.

I’m not sure how to deal with signed integers though. If I cast them to unsigned values, I loose the sign, since the C standard does not guarantee that

(int) ((unsigned) -1)) == -1

The other option would be to I cast a pointer to the value (i.e., reinterpret the byte sequence as unsigned), but it I’m not convinced that converting endianness after that is going to give anything sensible.

What is the proper way for platform independent signed integer storage?

Update:

  • I know that in practice, almost all architectures use two-complement representation, so that I can losslessly convert between signed and unsigned integers. However, this is question is meant to be more theoretical.

  • Just rolling out my own integer representation (be that storing the decimal letters as ascii characters, or separately storing the sign bit) is of course a solution. However, I’m interested if there is a way that works without completely abandoning the native binary representation.

  • 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-26T10:46:07+00:00Added an answer on May 26, 2026 at 10:46 am

    A platform-independent way? If you truly want this, you should consider writing it as text rather than binary (and taking into account that even that is not fully platform-independent since you may want to move it from an ASCII to an EBCDIC platform).

    It all depends on how platform-independent you need it to be. C allows for three different signed encodings: two’s complement, one’s complement and sign/magnitude. But, by far, most machines will use the first one.

    Work out first what you actually mean by that term. If you mean you only want to handle two’s complement, then casting it to an unsigned is fine.

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

Sidebar

Related Questions

I want to write a normalization function that will take a signed integer and
I want write an win-form application to convert data from foxpro table into sql
I want to write a function in Python that returns different fixed values based
I want to write a raw byte/byte stream to a position in a file.
I use trap to write errors to file, and want write line number where
I want write a repeating pattern of bytes into a block of memory. My
I have this structure which I want to write to a file: typedef struct
I have a signed applet and I want to write out dll files which
i want write a mapping File for following Class, but i dont know how.
Hello I want write my own desktop sharing application in Java. The application should

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.