0

I need to parse a binary file created by C++ and overwrite a 4 char long char array in that file, for example change the original char array of ABCD to WXYZ.

I know exactly the position in terms of bytes of the that char array. I tried RandomAccessFile which let me go to the position easily. But I cannot make the rest work for me right now.

Is the RandomAccessFile a right way to go?

I know I have to do some conversion from 2 bytes char to one byte char.

Anybody has a good way to do this?

2 Answers 2

1

Fine: always try the JavaDoc RandomAccessFile.

long position = ...;
byte[] bytes = new byte[] { (byte)'W', ... };
raf.seek(position);
raf.write(bytes);
Sign up to request clarification or add additional context in comments.

Comments

1

RandomAccessFile is fine. As you have already figured out, in C++ char is a single byte, whereas Java uses UTF-16.

The easiest option might be to use byte[4] in your code to represent the 4-character ASCII string.

Comments

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.