1

I know that using a hexadecimal editor, one can edit binary files and change 4 bits with each hexadecimal value, But I am kind of thinking of a project that requires to modify a single bit rather than 4-bits.

So Is there a way to read something (e.g. ASCII coded plain text-file) in bits and manipulate single bits in e.g. Java?

As a noob, I can think of loading each bytes and generating a string containing each 8-bit representation of each byte, but that is kind of quite a complex way and will waste a lot of space. Also, this approach would require me to keep a list containing each available byte's 8-bit representation to look it up.

  • 1
    Stackoverflow is not a great place to ask broad questions that can be easily and better answered by off-site resources and tutorials. In your case, simply searching for "bit manipulation in java" and "bit manipulation in python" would probably get you better and more complete answers. – President James K. Polk Jul 29 '17 at 00:17
  • https://security.stackexchange.com/questions/18197/why-shouldnt-we-roll-our-own – Mooing Duck Jul 29 '17 at 00:18
  • Also "bits" vs "hex" vs "bytes" are literally different ways to view data that does not factor into literally anything except how hard it is to read to a human. – Mooing Duck Jul 29 '17 at 00:20
  • If you can edit blocks of bits, like say, 4 bits at a time, to arbitrary values, that is effectively a way to modify individual bits, and is indeed, what professional programmers use. – Mooing Duck Jul 29 '17 at 00:21
  • @JamesKPolk That is why I marked out the question in the end. The actual question is regarding reading a file in bits within java and manipulating bits afterwards. I can rephrase the question and remove the encryption part to prevent confusion, though. – christopher westburry Jul 29 '17 at 00:28
  • You can write such a thing but you would find it unusable. Hex editors exist for a reason: they reduce the scale of the problem by 15/16. Too broad. – user207421 Jul 29 '17 at 01:20
  • @christopherwestburry If an user answered your question please also accept his answer ([Accepting Answers: How does it work?](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)). If not than please specify what remains unanswered, this is a really crucial part of StackOverflow, thank you very much. – Zabuzard Sep 24 '17 at 15:40

1 Answers1

0

Others have already hinted that your question is broad and can probably be better answered by other sites, ressources or search.


However I'd like to show you some small snippet with which you can start.

// Read a file into a byte array, we are not interested
// in interpreting encodings, just plain bytes
final byte[] fileContent = Files.readAllBytes(Paths.get("pathToMyFile"));

// Iterate the content and display one byte per line
for (final byte data : fileContent) {
    // Convert to a regular 8-bit representation
    System.out.println(Integer.toBinaryString(data & 255 | 256).substring(1));
}

You can also easily manipulate the bytes and also the bits by simply accessing the array contents and using simple bit operators like &, |, ^.

Here is a snippet showing you how to set and unset a bit at a given position:

byte data = ...;

// Set it (to 1)
data = data | (1 << pos);

// Unset it (to 0)
data = data & ~(1 << pos);

The conversion gets explained here: how to get the binary values of the bytes stored in byte array
The bit manipulation here: Set specific bit in byte

Here are some relevant Java-Docs: Files#readAllBytes and Integer#toBinaryString


Note that from a view of efficiency the smallest you can go in Java is byte, there is no bit data type. However in practice you will probably not see any difference, the CPU already loads the whole neighboring bits and bytes into the cache regardless of you want to use them or not. Thus you can just use the byte data type and use them to manipulate single bits.

Zabuzard
  • 25,064
  • 8
  • 58
  • 82