Deleting zeros at the end of the file

Help on how to use HxD.
Post Reply
jun
Posts: 4
Joined: 10 Oct 2018 03:54

Deleting zeros at the end of the file

Post by jun » 10 Oct 2018 04:29

Hello!

Firstly, thanks for developing. The tool is very useful for me. Especially the search part is great.

Sorry for my English, maybe I couldn't choose the right terms. So please change the title, if require. I have this problem very often. It's happening in image files. Sometimes I want to delete the zeros at the end of the file. More precisely, I have to shrink the file. I can't choose the right term: Shrink? Reduce? Crop? Trim? Compact?

Code: Select all

4A 75 73 74 20 74 6F 20 67 69 76 65 20 6F 6E 65
20 65 78 61 6D 70 6C 65 2C 20 70 65 61 63 65 20
61 6E 64 20 6C 6F 76 65 00 00 00 00 00 00 00 00
.. .. ..
.. .. ..
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
to

Code: Select all

4A 75 73 74 20 74 6F 20 67 69 76 65 20 6F 6E 65
20 65 78 61 6D 70 6C 65 2C 20 70 65 61 63 65 20
61 6E 64 20 6C 6F 76 65 

Of course I can do it using select and delete. But it's manual and I could make error.

Can you show me a cool way? Maybe a cmd script or anything else? And can you also add it as a feature under File Tools menu. (Though, it may be dangerous for some files, the trailing zeros can be important on some files, technically I don't know exactly.)

Best regards,
Jun.

Maël
Site Admin
Posts: 945
Joined: 12 Mar 2005 14:15

Re: Deleting zeros at the end of the file

Post by Maël » 10 Oct 2018 09:38

Hi,

Once the search supports regular expressions (which it doesn't do yet), you could use that to search backwards for the first non-zero byte.

From there use Ctrl+Shift+End and then press delete.

Currently, there is no more direct way to do it.

In general what you are thinking of would require scripting support, for which there is some feature request somewhere.

jun
Posts: 4
Joined: 10 Oct 2018 03:54

Re: Deleting zeros at the end of the file

Post by jun » 10 Oct 2018 22:10

Thank you for reply. Finally I found the right title "Remove null bytes from the end of a large file" This is exactly what I want... Regular expression was used in the response. I guess there are no commands like "sed" in Windows. On the other hand, they said that this process could be dangerous. I always thought everyone would want it, so I wanted it to be a feature in HxD. But it was unnecessary. Inexperienced users may corrupt their files due to misunderstanding.

For now, I do a simple backward search at the end of the file and easily select to end with Ctrl + Shift + End. Thanks for the tip.

Maël
Site Admin
Posts: 945
Joined: 12 Mar 2005 14:15

Re: Deleting zeros at the end of the file

Post by Maël » 11 Oct 2018 01:40

There are some grep tools for Windows, and common unix tools compiled for Windows.

https://en.wikipedia.org/wiki/GnuWin32
or CygWin is a possible source of such tools.
GnuWin32 seems to be unmaintained, though.

Win10 also has a subsystem for Linux, so you can use Ubuntu or similar.

Finally, the PowerShell is pretty flexible and part of recent Windows versions (or you can install it manually).

These are not complete solutions, but might give you some ideas:
https://stackoverflow.com/questions/151 ... -to-grep-f
https://stackoverflow.com/questions/986 ... powershell

Good luck.

jun
Posts: 4
Joined: 10 Oct 2018 03:54

Re: Deleting zeros at the end of the file

Post by jun » 11 Oct 2018 15:19

Actually I'd tried with Powershell:

Code: Select all

(get-content input.img) -replace '\x00+$','' | set-content output.img
But it leaves unnecessary bytes at the end: 0D 0A.

Today a Powershell user, chrisdent, help me and write this script:

Code: Select all

Add-Type '
using System.IO;

public class File
{
    public static void TrimNull(string path)
    {
        FileStream stream = new FileStream(path, FileMode.Open);
        stream.Seek(-1, SeekOrigin.End);
        while (stream.ReadByte() == 0)
        {
            stream.Seek(-2, SeekOrigin.Current);
        }
        stream.SetLength(stream.Position);
        stream.Close();
    }
}
'

[File]::TrimNull('c:\temp\50MBtest.img')
Works like a charm. Hope it helps someone else in the future.

jun
Posts: 4
Joined: 10 Oct 2018 03:54

Re: Deleting zeros at the end of the file

Post by jun » 11 Oct 2018 16:46

jun wrote:
11 Oct 2018 15:19

Works like a charm. Hope it helps someone else in the future.
Yes it works, only for small files. For large sized files (over 100 mb), select and delete in HxD is still best-fast way :)

Post Reply