Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - bytes and binary

Tags:

java

This is a basic question.

When I use a byte stream to write bytes to a file, am I creating a binary file?

For example: I use a byte stream to write text data to a notepad and when I open the notepad in a HEX viewer I see the corresponding hex value for each character. But why not the binary values (i.e 0s and 1s).

I also learned that using a dataoutput/input stream I read/write binary file.

I guess my confusion is with what does it mean to write bytes and what does it mean to write a binary data.

like image 381
user547453 Avatar asked Sep 04 '12 18:09

user547453


2 Answers

When I use a byte stream to write bytes to a file, am I creating a binary file?

You write the bytes as is, e.g., as the ones and zeroes they are. If these bytes represents characters then commonly no, it's just a text-file (everything is ones and zeroes after all). Otherwise the answers is it depends. The term binary file is missleading, but is usually referers to as a file which can contain arbitrary data.

when I open the notepad in a HEX viewer I see the corresponding hex value for each character. But why not the binary values

HEX is just another representation of bytes. The following three are equal

10       (Decimal value 10)
0xA      (Hex value 10)
00001010 (Binary value 10)

A computer only stores binary values. But editors may choose to represent (display) those in another way, such as Hex or decimal form. Given enough bytes, it can even be represented as an image.

what does it mean to write bytes and what does it mean to write a binary data

Binary data means ones and zeroes, e.g., 00001010 which are 8 bits. 8 bits makes a byte.

like image 63
Johan Sjöberg Avatar answered Oct 04 '22 02:10

Johan Sjöberg


The confusion could be caused by the application you are using. If you open something in HEX viewer, it should be represented in HEX not BIN.

like image 43
Damian Leszczyński - Vash Avatar answered Oct 04 '22 02:10

Damian Leszczyński - Vash