Can MS write hex bytes into a file

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
tyee
Newbie
Posts: 2
Joined: Wed Mar 18, 2009 2:44 am

Can MS write hex bytes into a file

Post by tyee » Wed Mar 18, 2009 2:49 am

Newbie here, and yes I searched and couldn't find an answer

After I find the filesize of a file, I will divide it by a number, then convert that answer to a hex value (4 bytes) to be written into an external binary file. Is this possible with MS??

gdyvig
Automation Wizard
Posts: 447
Joined: Fri Jun 27, 2008 7:57 pm
Location: Seattle, WA

Decimal to Hex converter

Post by gdyvig » Wed Mar 18, 2009 5:06 pm

Hi,

I saw a vbscript example in the forums.
Here is a pure Macro Scheduler method:

Code: Select all

//Digit to HexDigit array
Let>aHex_0=0
Let>aHex_1=1
Let>aHex_2=2
Let>aHex_3=3
Let>aHex_4=4
Let>aHex_5=5
Let>aHex_6=6
Let>aHex_7=7
Let>aHex_8=8
Let>aHex_9=9
Let>aHex_10=A
Let>aHex_13=B
Let>aHex_12=C
Let>aHex_13=D
Let>aHex_14=E
Let>aHex_15=F
SRT>DecToHex
  //Convert Decimal Base10 to Hexidecimal Base16
  //Up to 65535(dec)=FFFF(hex)
  Let>nDec={%DecToHex_Var_1%+0}
  Let>nBase=16

  Let>nTmp1={Trunc(%nDec%/%nBase%)}
  Let>nHex1={%nDec% MOD %nBase%}

  Let>nTmp2={Trunc(%nTmp1%/%nBase%)}
  Let>nHex2={%nTmp1% MOD %nBase%}

  Let>nTmp3={Trunc(%nTmp2%/%nBase%)}
  Let>nHex3={%nTmp2% MOD %nBase%}

  Let>nTmp4={Trunc(%nTmp3%/%nBase%)}
  Let>nHex4={%nTmp3% MOD %nBase%}

  MessageModal>%nHex4%-%nHex3%-%nHex2%-%nHex1%
  Let>nHex1=aHex_%nHex1%
  Let>nHex2=aHex_%nHex2%
  Let>nHex3=aHex_%nHex3%
  Let>nHex4=aHex_%nHex4%
  Let>nHex=%nHex4%%nHex3%%nHex2%%nHex1%
END>DecToHex

GoSub>DecToHex,
MessageModal>%nDec%(dec)=%nHex%(hex)
Gale

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Wed Mar 18, 2009 5:21 pm

The real question is whether writing those four ascii characters to a file and calling it a binary file is the same as writing the hex values. Somehow I doubt it but I don't know.

gdyvig
Automation Wizard
Posts: 447
Joined: Fri Jun 27, 2008 7:57 pm
Location: Seattle, WA

ExportData command

Post by gdyvig » Wed Mar 18, 2009 5:55 pm

The help for the ExportData command shows an example of exporting a ascii text string representing a string of hex characters to a dll file.

I guess it depends on how the data needs to be used.

Gale

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Wed Mar 18, 2009 6:11 pm

That sounds good :)

tyee
Newbie
Posts: 2
Joined: Wed Mar 18, 2009 2:44 am

Post by tyee » Thu Mar 19, 2009 2:11 am

Thanks for the info guys.

The external binary file will already exist so what I wanted MS to do is change the bytes in that file starting at a certain address, not create a brand new file.

gdyvig
Automation Wizard
Posts: 447
Joined: Fri Jun 27, 2008 7:57 pm
Location: Seattle, WA

Binary file support

Post by gdyvig » Thu Mar 19, 2009 3:59 pm

Macro Scheduler has no native commands to update binary files.

It can import a binary file into a text script from the editor, but that is a manual operation. You could then update the text data and use ExportData to make an updated copy of your binary file.

Do do this all within a script we would need an ImportData command to complement the new ExportData command made available in v11. That would still be clunkier than the direct binary update you want.

Gale

User avatar
Marcus Tettmar
Site Admin
Posts: 7391
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Thu Mar 19, 2009 4:47 pm

You can manipulate binary data with VBScript and ADODB.Stream:
http://www.mjtnet.com/forum/viewtopic.php?t=1170#5117
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
JRL
Automation Wizard
Posts: 3518
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Mon Mar 23, 2009 4:04 am

Here is a method where Macro Scheduler can be used to modify any file whether it is binary or text. When modifying a binary file use great caution as you risk making the file unusable. Running an improperly altered executable file might even cause damage to your computer.

Code: Select all

//Using Notepad as the binary since everyone will probably have it.
Let>OriginalBinaryFile=%Win_Dir%\notepad.exe
Let>NewTempBinaryFile=%Temp_Dir%NotNotePad.exe

/*
//Unremark this block if necessary.
//The built in null character variable "NullChar" was
//introduced in Version 11.1.05 March 3, 2009.
//The following three VBScript lines will create a
//null character variable in all earlier versions.
VBSTART
VBEND
VBEval>CHR(0),NullChar
*/

/*
The next couple of lines define the characters we're going to find in the
Notepad.exe file and the characters we're going to replace them with.
The next line is essentially Let>OldChars=N o t e p a d
Where the spaces are actually null characters (ASCII character 0)
*/
Let>OldChars=N%NullChar%o%NullChar%t%NullChar%e%NullChar%p%NullChar%a%NullChar%d
/*
The next line is essentially Let>NewChars=d a p e t o N (Notepad backwards)
*/
Let>NewChars=d%NullChar%a%NullChar%p%NullChar%e%NullChar%t%NullChar%o%NullChar%N

/*
There are Macro Scheduler functions that will work with ANY file
no matter what characters are involved.  There are also
Macro Scheduler functions that will only work with the characters
that are considered "text" characters.  I don't have a comprehensive
list of which functions can work with which characters.  I only know
what I have tested for previous scripts. I do know that
StringReplace> does not work with all characters.  I also know that the
functions used in this script WILL work with all characters. 
The functions that this script uses are:
ReadFile>
Separate>
ConCat>
WriteLn>
ReadFile> saves a file's contents to a variable. Separate> finds and
isolates the searched character string. ConCat> reconstructs the file
using the new character string. And WriteLn> creates a new file using
our chosen characters.
*/
ReadFile>OriginalBinaryFile,File
Separate>file,%OldChars%,item
Let>kk=1
Repeat>kk
  Add>kk,1
  Let>value=item_%kk%
  Concat>item_1,%NewChars%%value%
Until>kk,%item_count%
//The next line is very important. We don't want to introduce any
//extra characters into our file.
Let>WLN_NOCRLF=1
WriteLn>NewTempBinaryFile,wres,item_1
/*
The previous ten lines do all the necessary work reading the file,
parsing the file, reconstructing the file and writing the file.
The following concat lines construct a message to be displayed in
Notepad or rather dapetoN.
*/
Let>DTxt=Congratulations.%crlf%
ConCat>DTxt,You have successfully altered the "binary file" %OriginalBinaryFile%%crlf%
ConCat>DTxt,using Macro Scheduler.  Notice the title block has been changed.%crlf%
ConCat>DTxt,As have NotePad message boxes.%crlf%%crlf%
ConCat>DTxt,Basically, every occurance of the characters "N o t e p a d" in the %crlf%
ConCat>DTxt,Notepad.exe file are changed to "d a p e t o N" (Notepad spelled backwards)%crlf%%crlf%
ConCat>DTxt,The Help menu is slightly altered but since the help file is a separate file%crlf%
ConCat>DTxt,the contents found under  "Help Topics" was not altered.%crlf%
ConCat>DTxt,Menu item "Help" > "About dapetoN" also looks different.%crlf%%crlf%
ConCat>DTxt,%crlf%%crlf%
ConCat>DTxt,The newly created file has been deleted.%crlf%%crlf%

/*
The following lines open the newly created executable file, display
a message in the text area and then, the new executable is deleted. If
the original file is to be replaced, the new file would be copied
over the original file before the new file was deleted.  Obviously,
we don't want to do that to Notepad.
*/
Run>NewTempBinaryFile
WaitWindowOpen>Untitled*
SetFocus>Untitled*
Send>DTxt
DeleteFile>NewTempBinaryFile
"The above script will create a copy of Notepad.exe with the text Notepad reversed. You can run it on any system and it will not alter the existing copy of Notepad so give it a go. Just copy it to a new macro and hit Run".

A short lecture on file composition.

"Decimal" is a system of number representation wherein numbers are written in powers of 10. "Binary" is a system of number representation wherein numbers are written in powers of 2. "Hexadecimal" is a system of number representation wherein numbers are written in powers of 16. These numbering systems are sometimes called base 10, base 2 and base 16. Since most of us think in terms of base 10, we think of numbers expressed in base 10 as being actual numbers and other bases as mathematical aberrations. In reality all numbering systems are equally valid representations of explicit and unchanging number values.

As we all know, computers process in binary. They do all their "thinking" in ones and zeros

In the DOS/Windows environment, all files are made from 256 building blocks called bytes. Each byte is made of 8 bits. Each bit is a binary number, one or zero. In math terms 8 (bits) to the power of 2 (binary) is 256 (bytes) possible characters.

The 8 bit binary number that represents the first of the 256 possible bytes is 8 zeros "00000000". The 8 bit binary number that represents the last of the 256 possible bytes is 8 ones "11111111". The 8 bit binary number that represents the 78th byte (ASCII character 77 also seen on your keyboard as the letter "M") is "01001101".

The hexadecimal representations of these same decimal numbers (0, 255, 77) would be 0, FF, 4D.

The point here is that there are 256 and only 256 building blocks that make up all the files on every computer that can use Macro Scheduler. Those building blocks can be referred to as binary characters, as hexadecimal characters, as decimal characters or even as octal characters. No matter what math base you reference them from, they are the same building blocks. Calling them binary versus hexadecimal does not alter their value nor does it create a separate set of characters.

Macro Scheduler is capable of reading, writing and manipulating all 256 characters. What a great program. Who could ask for more?

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts