
October 21st, 2009, 10:03 PM
|
|
Contributing User
|
|
Join Date: Jul 2007
Posts: 33
  
Time spent in forums: 9 h 27 m 59 sec
Reputation Power: 5
|
|
|
Dealing with bytes
I am creating a file byte viewer/editor. The viewing part is pretty simple, using ADODB.Stream LoadFromFile and so on.
The problem is when it comes to writing back to the byte stream. I tried creating a Byte String (if that makes sense to you) using vbscript's chrB. But, that is, according to ADODB.Stream, only a kind of string, and not a byte array. Next, I tried changing the stream type to text, add the characters, and switch the type back bytes. The problem with that is that the stream changes those non regular characters to what it feels appropriate.
Then I tried Scripting.FileSystemObject. But when I entered those non regular characters into the write method it gave me an error.
Eventualy, I sort of settled on ADODB.Stream, and set the charset to unicode. This seemed to have retained the bytes I've entered. But, as you might have guessed, you get all those 00 bytes between them.
So, the work around for this is obvious: The text is entered into a new stream object. Then, you set the position to 2, to skip the first two bytes that are markers showing that it contains text. After that you make a loop for the length of the string, and at each loop you copy 1 byte to the original stream, and move position ahead one byte.
I'm non sure that it still doesn't mess up any characters. But if it does cause problems, there are still a couple of more tricks up my sleeve.
If anyone has a more straight forward way, please let me know.
I'm really sorry for not showing examples, but I wrote this from a phone.
|