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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:29:03+00:00 2026-06-16T02:29:03+00:00

When writing to a binary file like this: byte[] content = {1,2,3}; FileOutputStream output

  • 0

When writing to a binary file like this:

byte[] content = {1,2,3};
FileOutputStream  output =   new FileOutputStream("data.bin");
DataOutputStream fileName = new DataOutputStream(output);
fileName.writeInt(content.length);
for (int i = 0; i < content.length; i++)
{
fileName.writeInt(content[i]);
System.out.println(content[i]);
}
fileName.close();

When reading it back using FileInputStream/DataInputStream and .readInt() everything is ok.
(If i use .skip(4); because the first one seems to contain a value wich is the number of digits written)

However, if the byte[] content is replaced with input using scanner.

java.util.Scanner in = new java.util.Scanner(System.in);
String inputAsString = in.nextLine();
byte[] content = inputAsString.getBytes();

I noticed it is written to the binaryfile in decimal. 1 becomes 49, 2 is 50, 3 is 51 …
My question is how can i read it back to 1, 2, 3 just like the first example with the hardcoded byte array.

  • 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-16T02:29:04+00:00Added an answer on June 16, 2026 at 2:29 am

    When you read in data from input it’s a string. So you’re getting ascii/utf-x bytes. So the value you’re writing is the byte. You want to turn the input you read into an int:

    int toWrite = parseInt(inputAsString);
    fileName.writeInt(toWrite);
    

    Note that 51 is ascii value assigned to the character ‘3’

    If you write it out as an int you should be able to read it in as such as well.

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

Sidebar

Related Questions

Currently, I am reading data from a binary file (File.ReadAllBytes), converting this byte array
I'm writing an obj-c app and would like to upload a binary file a
I am writing a program like this to write some bytes into a file.
I have this code wich is working when writing a binary file: using (BinaryWriter
I am writing to binary file using fstream and when open the file using
I am writing a binary file in C++, where I traverse to a particular
I am writing the below structure in to the file using Binary Writer. File
I'm writing binary serialiser/deserialser to convert a number of object types to/from a byte
What are the best practices for reading and writing binary data in Ruby? In
I'm writing a simple function that split binary/post-data/html from a HTTP response. The HTTP

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.