1. Editing a hex file?

I have a bin file which is a hex dump from an eeprom. Please see image https://dl.dropboxusercontent.com/u/8952255/ScreenClip.png

I want to replace the highlighted characters with 6 digits entered into a text box. So if a users enters 323453 for example then these character must be in the highlighted section. File size and location will never change.

Here is how I have read the file but i'm not sure how to replace the parts of the file I want.

integer fn 
integer len 
integer test 
sequence tt 
atom aa 
 
fn = open("Lagard.bin", "rb")  -- an existing file 
 
sequence whole_file 
whole_file = {} 
 
sequence chunk 
 
while 1 do 
    chunk = get_bytes(fn, 16) -- read 100 bytes at a time 
    whole_file &= chunk        -- chunk might be empty, that's ok 
    if length(chunk) < 16 then 
        exit 
    end if 
end while 
 
close(fn) 
new topic     » topic index » view message » categorize

2. Re: Editing a hex file?

tonysteward said...

I have a bin file which is a hex dump from an eeprom. Please see image https://dl.dropboxusercontent.com/u/8952255/ScreenClip.png

I want to replace the highlighted characters with 6 digits entered into a text box. So if a users enters 323453 for example then these character must be in the highlighted section. File size and location will never change.

I assume you've already converted the 6-digits into three hex digits so I won't go into that part of it.

You don't have to read the entire file in to update it. Try this ...

include std/io.e 
integer fh 
constant DigitPos = ??? --- This is the offset into the file where the digits are. 
sequence HexDigits --- This is where you have converted the 'text' 6-digits into a 3-byte hex format. 
 
fh = open(TheFile, "ub") -- Open for Updating 
seek(fh, DigitPos)       -- Move the 'cursor' to the spot in the file to update 
puts(fh, HexDigits)      -- Write out the 3 bytes of hex data to the file 
close(fh)                -- close the file (and thus save the update) 
 
new topic     » goto parent     » topic index » view message » categorize

3. Re: Editing a hex file?

Absolutely have no idea what I am doing and have been using Euphoria for all of 48 hours since last using it 5 years ago. Also don't know why I would or how to convert to 3 hex digital. If you look at the image file the number (to me at least) appear in plain text. The existing code in this dump is 123456

new topic     » goto parent     » topic index » view message » categorize

4. Re: Editing a hex file?

Hi,
hexadecimal is a 2 char representation of a single byte (8 bits) represented as 00 to FF.
With Derek's sample, this should be straightforward, but if a user is expected to enter the replacement values,
I suggest that you validate the permissible hex digits "000000" to "FFFFFF" and let the user know when
he has attempted something outside of that range, before attempting the replace.
HTH!

new topic     » goto parent     » topic index » view message » categorize

5. Re: Editing a hex file?

All ok I figured it out. Thank you for showing me how to edit only part of the file. Awesome.

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu