How to read the bytes of a file?


#1

Though each char in a Document's value (or recentValue), after it’s been set, appears to be using a byte of its two bytes (i.e., value.length == number of bytes), any byte that is > 0x7f gets stored in value as 0xfffd, for some reason. Am I missing something?

$ echo -en "\x80" > foo
doc.on("ready", function() { // where doc is foo
    console.log(doc.recentValue.charCodeAt(0).toString(16)); // prints fffd instead of 80
});

#2

Are you asking this in relation to the hex editor plugin?
The value in document is a utf8 encoded string and it can be used only if file is a valid utf8 file


#3

@harutyun, yes, this is for the hex plugin. Any other ways/thoughts then?


#4

sorry half of my answer didn’t go through. for files that are not open in the ide you can use xhr the way it is shown in the jsbin in my other answer, for files that are open in ace and don’t contain unicode error marks you can use a library to decode utf8 string to bytes, (see http://stackoverflow.com/a/1543616/1743328 for an explaination). Btw what do you think about ace based hex editor from http://jsbin.com/canihuteca/edit?html,output ?


#5

That seems to work. Thanks, @harutyun!

Re ace, afraid haven’t tried yet. Focusing of getting things to work first. Will keep you posted when I try! Thanks again!