0

I am reading a file into byte array, i have inserted some control command into it, which I need to decode in some other class. But how to identify and separate these information from rest of the content of file being read. I need to use some unique character as a delimiter to identify the start and end of this control command in the byte array. Is there any unique character which i can use for this purpose ? That particular character should not be present in any normal binary file of any format.

3 Answers 3

2

No, if it's a binary file, then in general, a byte can take on any value between 0 and 255.

One possible solution is to reserve one particular byte value as an "escape" character; let's say that you choose '\' for that purpose. Then to represent a delimiter, you could insert the sequence "\d", for instance. Of course, to represent an actual '\', you'll need to delimit that too; "\\", for instance.

1
  • @Charlesworth The byte array will be something like this [<filename>file_1_contents<filename2>file_2_contents.... <file_n_name>file_n_contents] can i use `\` as a delimiter ? because even the file contents also can contain `\`
    – Rakesh
    Sep 5, 2011 at 15:50
1

No, by definition there is no such a control character. But what you can do is store the array in a format that follows: [length of byte array] [byte array]. All you have to do then is read in an integer <- the length, create a new array of that length and read it fully.


EDIT:

Okay so use the following format:

The file name is null terminated so you know the delimiter there (this works because a file name is ASCII only!). Then you have the length (an integer/long) and then the byte array.

All of the other methods mentioned above are unsafe and if you use a complex delimiter then: it's not perfect and takes up more space then using the a simple length variable...

1
  • I am following kinda same format, but i am not worried about the length of the byte array. Only thing is i need to save the file name in the first field instead of the byte array so that the byte array looks like this [<filename>file_1_contents<filename2>file_2_contents.... <file_n_name>file_n_contents]
    – Rakesh
    Sep 5, 2011 at 16:05
1

you can use come character sequence (or a byte sequence) and when you're decoding, if you find that sequence that it is delimiter.

1
  • but i will have to use some byte sequence which should not appear in the file contents. The file can be a file with any extension viz .exe .txt .dat etc
    – Rakesh
    Sep 5, 2011 at 15:41

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.