2

My requirement is to convert a number to binary format and read the binary data and play with it (like reverse of string etc).

My input is int i=12937.

Below is my code to create binary data:

int i = 12937
DataOutputStream os = new DataOutputStream(new 
FileOutputStream("D:\\binout.dat"));
os.writeInt(i);
os.close();

Output is: 2‰

I was reading the file data and trying to print it then it is printing this value: 2?

DataInputStream d = new DataInputStream(new 
FileInputStream("D:\\binout.dat"));
String count;
while((count = d.readLine()) != null){
    System.out.println(count);
}

Output: 2?

Java code is reading "‰" this symbol and converting it to "?"

Is there any way that I could directly read "‰" the symbol and print it on the console?

5
  • What exactly do you mean by "convert to binary format"? Binary as opposed to text (i.e. store something in a file exactly as it is represented in memory)? Or do you mean write a text file which contains only the characters "0" and "1"? Commented Aug 3, 2017 at 16:00
  • my requirement is to convert and save the integer values to binary file format in this way "‰2 ð8k" etc... and i have to read the data again in string format and perform some operations on the binary string like string reverse etc then save it back to the same file. the DataInputStream class is not working for my requirement and the Scanner class works for me . Commented Aug 4, 2017 at 9:51
  • Then I suggest you read the file into a byte[] array instead of a string. If you read into a string, you are doing character set decoding which you don't want. Commented Aug 4, 2017 at 10:07
  • Ok. thanks. i will try it and let you know Commented Aug 4, 2017 at 10:36
  • this is a better idea and approach. thanks @KlitosKyriacou Commented Nov 29, 2017 at 16:11

2 Answers 2

2

You are writing your data in a binary format designed to store Java primitives, but ultimately reading it as text through the deprecated readLine method of DataInputStream.

This causes the bizarre characters in your "output".

Persisting data as binary does not equate to converting a decimal integer to binary.

The persistence kind you choose (binary, text) is up to you, but you should document yourself on the proper practices (think Stream for binary, Reader/Writer for text, and be consistent).

To simply display a decimal integer as binary you can use Integer.toBinaryString.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the answer, i think the readLine method is deprecated and the dataInputStream is not solved my problem. i switched to scanner class then it worked fine for me.
codeFile file = new File("D:\\binout.dat"); Scanner scnr = new Scanner(file);String line = scnr.nextLine(); StringBuilder scb = new StringBuilder(); reverseString = new StringBuffer(line).reverse().toString(); System.out.println("Output is "+line); System.out.println("Reverse of string is "+reverseString.toString());
@kumar yes so you are now parsing as text.
0

In Java you have the following methods that might be useful:

Integer#toBinaryString(int i) - returns a String binary representation of a number
Integer#parseInt(String s) - returns int from a String.
StringBuilder#reverse() - reverses the String in StringBuilder

You can start with a Scanner using:

Scanner sc = new Scanner(System.in);

These things together should let you solve the problem.

Edit:

Code which solved the issue:

File file = new File("D:\\binout.dat"); 
Scanner scnr = new Scanner(file);
String line = scnr.nextLine(); 
StringBuilder scb = new StringBuilder(); 
reverseString = new StringBuffer(line).reverse().toString(); 
System.out.println("Output is "+line); 
System.out.println("Reverse of string is "+reverseString.toString()); 

2 Comments

code File file = new File("D:\\binout.dat"); Scanner scnr = new Scanner(file);String line = scnr.nextLine(); StringBuilder scb = new StringBuilder(); reverseString = new StringBuffer(line).reverse().toString(); System.out.println("Output is "+line); System.out.println("Reverse of string is "+reverseString.toString()); Output is ‰2
@kumar I added your code to the answer, so it's easier to find for someone in the future

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.