1

Say I open a text file like this:

 public static void main(String[] args) throws IOException {
    String file_name = "file.txt";

    try {
        Read file = new ReadFile(file_name);
        String[] Lines = file.openFile();

        for (int i = 0; i < es.length; i++) {
            System.out.println(Lines[i]);
        }
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
}

Now, I want to change the result to binary (for further conversion into AMI coding), and I suppose that firstly I should turn it to ASCII (though I'm also not 100% certain if that's absolutely necessary), but I'm not sure if I should better change it to chars, or perhaps is there an easier way?

Please, mind that I'm just a beginner.

1 Answer 1

1

Do you happen to know for sure that the files will be ASCII encoded? Assuming it is, you can just use the getBytes() function of string:

byte[] lineDefault = line.getBytes();

There is a second option for .getBytes() as well if you don't want to use the default encoding. I often am using:

byte[] lineUtf8 = line.getBytes("UTF-8");

which gives byte sequences which are equivalent to ASCII for characters whose hex values are less than 0x80.

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

2 Comments

Very good point about the encoding. On the other hand, the file could be treated as binary; Then only the file sender and the receiver would need to agree on the encoding.
Yeah, that was why I was asking about the file being encoded that way. But I guess if you want to separate on new lines, you need to comprehend the file in an encoded fashion.

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.