Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to read byte values from file?

Status
Not open for further replies.

pbb72

Programmer
Mar 27, 2004
38
NO
Can anybody tell me how to read byte values from binary files?
I have gotten quite far by using

Code:
var ts = file.OpenAsTextStream();
alert(ts.Read(1).charCodeAt(0));

This works most of the time, but not for a few byte values! For example, byte value 128 (0x80) returns 8364 (0x20AC), 149 (0x95) returns 8226 (0x2022), etc. Does anybody know a proper way to read byte values from a file?
 
Use fso to read binary files is unreliable and is not designed for.
Should you not specify what environment do you intend to do this?
 
The environment used is HTA (HTML Application).
 
Use adodb stream object to read the file. For jscript, you need additionally use microsoft.xmldom to translate the byte array to array of variant.
[tt]
var binfilespec="ipconfig.exe";
var baseposition=0; //zero-based
var ibytetoread=10; //to read 10 bytes from base inclusive

var adodbstrm=new ActiveXObject("adodb.stream");
with (adodbstrm) {
type=1;
open();
loadfromfile(binfilespec);
position=baseposition;
var bstrm=read(ibytetoread);
}
var oelem=new ActiveXObject("microsoft.xmldom").createElement("belem");
with (oelem) {
dataType="bin.hex";
nodeTypedValue=bstrm;
}
var hexstrm=oelem.text;
var s_charcode="";
var s_ascii="";
var stmphex, stmpascii;
for (var i=0;i<hexstrm.length/2;i++) {
stmphex=hexstrm.substr(i*2,2);
stmpascii=String.fromCharCode(parseInt(stmphex,16));
s_charcode+=(i!=hexstrm.length-1)?stmphex+" ":stmphex;
s_ascii+=stmpascii;
}
oelem=null;
adodbstrm=null;
//result are contained in s_charcode and s_ascii in two representations.
//do whatever with them hereafter
[/tt]
- tsuji
 
Wow, that is quite extensive code... If I understand it correctly, you use ADODB instead of FileSystemObject to read the file, XMLDOM to convert the binary stream to a string of hex values, parseInt to convert the hex values back to integers, and then fromCharCode to convert them to characters.

What is the reason you use ADODB instead of FSO to read the file? I see you put the data in a variable, just like you would with the TextStream read data.

The code I've come up with so far is a bit shorter. It has been build up by trial and error, and I have no idea how bullet-proof this is. Can you comment on it?

Code:
String.prototype.asciiCodeAt = function(i) {
	// charCodeAt returns some bytes translated to unicode characters. 
	// this function means to counteract that.
	switch(this.charCodeAt(i)) {
		case 0x20AC: return 0x80; break;
		case 0x201A: return 0x82; break;
		case 0x0192: return 0x83; break;
		case 0x201E: return 0x84; break;
		case 0x2026: return 0x85; break;
		case 0x2020: return 0x86; break;
		case 0x2021: return 0x87; break;
		case 0x02C6: return 0x88; break;
		case 0x2030: return 0x89; break;
		case 0x0160: return 0x8A; break;
		case 0x2039: return 0x8B; break;
		case 0x0152: return 0x8C; break;
		case 0x017D: return 0x8E; break;
		case 0x2018: return 0x91; break;
		case 0x2019: return 0x92; break;
		case 0x201C: return 0x93; break;
		case 0x201D: return 0x94; break;
		case 0x2022: return 0x95; break;
		case 0x2013: return 0x96; break;
		case 0x2014: return 0x97; break;
		case 0x02DC: return 0x98; break;
		case 0x2122: return 0x99; break;
		case 0x0161: return 0x9A; break;
		case 0x203A: return 0x9B; break;
		case 0x0153: return 0x9C; break;
		case 0x017E: return 0x9E; break;
		case 0x0178: return 0x9F; break;
		default: return this.charCodeAt(i); break;
	}
}
 

I'm curious... why do you need to read binary files with JavaScript? Are you running this code in a web browser?

Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
As long as your custom function works for you and within your comprehension, that's fine. I must say in what sense the comparison you made, shorter or longer, in beyond my comprehension. But, it does not matter. I never view the thing as a competition of parsimony of alphabets.

The reason of ... is what I said. fso is not appropriate binary file is probably it misleads itself in reading the memory blocks it loads the file. But, I cannot say anything for sure as I have no insider-info of it nor adodb. I would mislead rather than really helpful talking conjectures.
 
@Dan: The code is run in a webbrowser, but not as a website. It's an HTA (HTML Application) which basically means nothing more than a Windows Script file with an interface. I choose HTA because I wanted to write a simple application and did not feel like installing an IDE.
@Tsuji: Code-length is not per se an important factor for me, but I do like to keep my code readable, so that at a later time I can make some modifications. And shorter code is just *generally* more readable (*not always*). Thanks anyway for you help so far!
 
Okay if you insist. What does your function do? and what do you think mine does? Can we compare them? in what sense.
 
Hmmm, I didn't mean to be insistent.
My main worry with my routine, is that Windows seems to do some sort of ASCII to ANSI translation, and I am not sure if that translation will be the same with other regional settings.
Since you ask me to compare it to your code, my main worries with that are speed (since ADODB and XMLDOM seem to be quite heavy objects to me) and code readability (since the code has nothing to do with databases).
But if someone can tell your code is more relyable (for example because is my dependency on regional settings) then you did a great job.
 
pbb72, I would not be of any helfful. I can see a state of total confusion that I would be helpless to rectify. Must be some very deep issue that your function helped you resolved. Good luck.
- tsuji
 
Hi Tsuji. Your post got me puzzled now. I was not aware of any confusion? I am getting confused now... ;-)
And this about the very deep issue? Are you referring to the ASCII to ANSI conversion? What it means is just that Windows does not use the standarized ASCII table. For example the euro symbol is bytevalue 127/0x7F in ASCII while it is 0x20AC in Windows' character sets. I thought the Windows character sorting was called ANSI, but I might be mistaken about that.
I am sorry for getting you so confused, but thanks for the help so far anyway!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top