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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:58:24+00:00 2026-06-04T13:58:24+00:00

In java, how do I write integers to a file so that they are

  • 0

In java, how do I write integers to a file so that they are unsigned 8 bit integers (within range 0 to 255) ? Is it enough that the numbers are of type int and within 0-255, or do I need to convert them to some other type first? I think it’s just the same thing? But I’m not sure if something funny happens when they get placed in the file…

  • 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-04T13:58:26+00:00Added an answer on June 4, 2026 at 1:58 pm

    Signed vs. unsigned is only a matter of how the bit patterns are interpreted, not the bit patterns themselves.

    So if as long as your integers are in the range 0 to 255, you can just jam them into bytes and write the bytes to a file.

    However, because Java interprets byte bit patterns as being signed, you have to be careful when reading them back in.

    For example, say you have the integer 253. This is (in full 32-bit form):

    00000000000000000000000011111101
    

    If you do:

    int i = 253;
    byte b = (byte) i;
    

    then b will contain the bits:

    11111101
    

    If you ask Java to use this, Java will claim it is -3 because when interpreting bits as signed, -3 is represented as the pattern 11111101. And so if you write that byte to a file, the bits 11111101 will be written out, as desired.

    Likewise, when you read it back in to a byte when reading the file, the byte you read it into will be filled with the bit pattern 11111101 which, again, Java will interpret as -3. So you cannot just do:

    byte b = readByteFromFile();
    int i = b;
    

    because Java will interpret b‘s contents as -3 and give you the integer that represents -3 as a 32-bit integer, which will be:

    11111111111111111111111111111101
    

    Instead, you need to mask things off to make sure the integer you end up with is:

    00000000000000000000000011111101
    

    So,

    byte b = readByteFromFile();
    int i = (0x000000FF) & b;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a Java program that reads an input file consisting
how to write a Java class to sort 10 billion integers, assuming we can
Given Java's write once, run anywhere paradigm and the fact that the Java tutorials
I want to write a java program that acts as a user interface to
I'm trying to write a Java application that manipulates high resolution .wav files. I'm
I am trying to write a function that takes File object, offset and byte
I'm trying to write a servlet, that can upload a file from client to
*My assignment is to write a program that stores an array of five int
I have a java server, that can send any kind of file, but I
I want to write the numbers to a file. The programs runs without any

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.