Editing a Hex file using a Batch script

June 11, 2009 at 17:33:00
Specs: Windows XP
Hi I have a txt file which holds a bunch of of HEX values. I need to write a batch files which can modify certain HEX values. My txt file looks like this

01 00 00 00 FF FF FF FF FF FF FF

I need to modify the HEX values( in BOLD) to some other values using a batch script.
Any ideas as tohow this can be achieved??


See More: Editing a Hex file using a Batch script

Report •


#1
June 11, 2009 at 18:13:21
is that all you have for the text file? what is the criteria for search? is it always on a particular line? are they always same position on the file? more information is needed

Report •

#2
June 11, 2009 at 18:33:06
Hi,
this is the format of the file, its just one line and the position of the values that need to be changed will be static. Since the values in these positions are dynamic, I dont think we can to a search and modify operation.

Any suggestions??


Report •

#3
June 11, 2009 at 18:58:39
i don't do batch. what i do is i use good string processing tools to do the job. If you can download gawk for windows. here's how you do it
C:\test>gawk "{$1=\"XX\" ; $5=\"YY\"}1" file.txt
XX 00 00 00 YY FF FF FF FF FF FF

Python is another one of tools i use

for line in open("file.txt"):
    line = line.strip().split()
    line[0] = "XX"
    line[4] = "YY"
    print ' '.join(line)

output
C:\test>python test.py
XX 00 00 00 YY FF FF FF FF FF FF

if you rather have batch, wait for someone to show it to you.


Report •

Related Solutions

#4
June 12, 2009 at 02:04:03
Batch script:

:: Code begins...
@echo off
setlocal enabledelayedexpansion
cls

set inputfile=path\filename.ext

set /p hex=<%inputfile%

echo.&echo.&echo.&echo.

set /p one= Enter new value for position 1:
cls
echo.&echo.&echo.&echo.

set /p two= Enter new value for position 5:
cls


for /f "tokens=1-5* delims= " %%A in ("%hex%") do (
set newhex=!one!
set newhex=!newhex! %%B
set newhex=!newhex! %%C
set newhex=!newhex! %%D
set newhex=!newhex! !two!
set newhex=!newhex! %%F
)


echo !newhex!>!inputfile!

echo.&echo.&echo.&echo.
echo Old hex file = !hex!

echo.
echo New hex file = !newhex!

exit /b
:: Code ends...

Canceled with extremely red face. The request is not to edit a text file, as I had interpreted it to be, but to edit a binary file which I now don't think is possible using a batch script. Apologies.


Report •

#5
June 12, 2009 at 02:48:07
@ Wahine
so how easy is it to change your code, such that OP can specify different positions to change and not just position 1 and 5 ?

Report •

#6
June 12, 2009 at 03:22:56
@ Ghostdog

The script is not meant to alter any other than positions 1 and 5. Is that not what the OP requested?

<quote>
01 00 00 00 FF FF FF FF FF FF FF

I need to modify the HEX values( in BOLD) to some other values using a batch script.
<endquote>

On my screen only positions 1 and 5 are "in BOLD".

I might have a look at a script to change any value if that's what the OP wants.

W.


Report •

#7
June 12, 2009 at 04:10:59
@W. yes that's the OP's requirement. That's my own personal question to you. the script can be made better and more flexible so that if requirement changes, its easy to change (or no need to change at all).

Report •

#8
June 12, 2009 at 04:59:25
W,

I wonder what you expect to have typed in when prompted.

Hint: ff hex is *NOT* "two letters f".


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#9
June 13, 2009 at 19:42:24
M2 - I let down my guard and you, quite rightly, delivered the sucker punch. As you will see I have edited my first post in this thread. Your thoughts on editing binary files using batch scripting please, possible?

Thanks.

W.


Report •

#10
June 13, 2009 at 23:25:54
@echo off > d & setLocal EnableDelayedExpansion

set /p B=byte to place at 5th position :

(
echo n myfile
echo L 100
echo e 104 !B!
echo w
echo q
) >> d

debug < d > nul


=====================================
If at first you don't succeed, you're about average.

M2


Report •


Ask Question