built-in hex editor?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting built-in hex editor?
# 1  
Old 07-24-2004
built-in hex editor?

I'm using Mac OS 10.2.2 and I need to find a command line hex editor. I figure there's already one built-in, but I just don't know enough about Unix, yet. Basically, what I want to do is tell the hexeditor to open a file, replace a specific offset with a new value, then put the resulting file somewhere. For example:

hexeditor inputfile offset newvalue > outputfile

It would also be nice to be able to specify the block size, since I am used to looking at the code in byte-sized chunks (pun not intended). Can anyone tell me which command(s) would get this done?
# 2  
Old 07-25-2004
As for a command, take a look at the od command. od is octal dump. There are more than likely other utilities, but not having much use for these sorts of commands on a daily basis, I do not know of them. Sorry I cannot help further.
# 3  
Old 07-25-2004
Thanks for taking the time to reply. Unfortunately, od won't help here because 1) it's deprecated in favor of hexdump (at least in Darwin), and 2) it only displays the file in hex, not let you edit it. For instance:

hexdump -C myfile // prints myfile in blocks of 1 byte to the standard output
hexdump -C myfile > outputfile // creates a text file with the results

It's the perfect viewer, and I could check my work quickly with hexdump, but first I need to actually edit the file. At first look, dd looked like it might provide a solution for me, but now I'm not so sure. Here's what I tried:

echo 123 > insert
dd if=insert bs=1 count=3 seek=0x03 conv=notrunc of=myfile
hexdump -C myfile > hexdump
cat hexdump

The results are that 0x03 through 0x05 were changed to 31 32 33, respectively. I'm not sure, but I believe that's the ASCII version of what I wanted to do. I really don't know enough about this to know what to try. I know enough to delete my hard drive or otherwise get myself into a terrible mess! Any more ideas?
# 4  
Old 07-25-2004
There are many different echo statements. You're going to need to check your man page. To build an octal constant with the ksh built-in echo it's

echo \\0123

And you are replacing one byte, not three. The count on your dd statement needs to be 1.

Also, do you have adb available? It might be another approach.
# 5  
Old 07-26-2004
@Perderabo
I don't think I have ksh. The default shell when opening the Terminal app is tcsh, but I'm most familiar with sh (which is bash in Darwin, I think). Anyways, I couldn't get echo \\0123 to work for me. The man pages in Darwin are a bit on the sketchy side. The man page for echo, for instance, tells me the format to use, describes the -n option briefly, and that it writes to the standard output. Not very informative, which might not matter for echo, but that's not the point. That's why all my learning has happened here and in Google groups. I also don't have adb. They might have it in a later release (I'm using Mac OS 10.2.2).

@Driver
That tip on how to write the hexadecimal values with printf is exactly what I needed. I used printf to pipe to dd and the patch worked perfectly.

@Anyone looking for a command line hex editor for Mac OS X
Here's the code I used if anyone's interested:

Code:
printf "\x1\x2\x3" | dd bs=1 count=3 seek=0x03 conv=notrunc of=myfile
hexdump -C hexdump-myfile

Explanation for newbies like me:
1) print 0x01 0x02 0x03
2) pipe to dd (block size = 1 byte, replace 3 blocks, skip to offset 0x03, do not truncate the file, apply output to myfile)

Viewing hexdump-myfile shows that myfile was patched:
Code:
00000000    00 00 00 01 02 03 00 00    00 00 00 00 00 00 00 00

Thanks for helping the newbie, guys!
# 6  
Old 08-17-2004
Hi, it's me again. Now that I have the basics down for this, I am trying to make a versatile patching script that can dynamically patch at different offsets.
Code:
off1=0x008
off2=0x010
input1=01
input2=00\ 01\ 02\ 03
outfile="myfile1 myfile2"
count1=1
count2=4

if [ blah ]; then
  off=$off1
  in=$input1
  num=$count1
else
  off=$off2
  in=$input2
  num=$count2
fi

for file in $outfile; do
  printf "\x$in" | dd bs=1 count=$num seek=$off conv=notrunc of=$file
done

This script works as long as blah is true. If blah is not true, however, I'm obviously going to have a problem since what I want printf to output is \x00\x01\x02\x03, not \x00 01 02 03. I thought that perhaps using sed would work, but it appears that sed likes to output the ascii character as opposed to the hexadecimal output I'm trying to achieve. To expand on this example, for instance:
Code:
printf "$in" | sed s/\ /'\\'x/g | dd bs =1 count=$num seek=$off conv=notrunc of=$file

...produces...
Code:
00000010   5c 78 00 00 00 00 00 00   00 00 00 00 00 00 00 00

...which is the ascii character version of the first byte of my input (\x00). Anyone got any ideas on how I can splice the \x into the bytes I'm trying to input?
# 7  
Old 08-17-2004
Ouch. Now I feel really sub-newby-ish. I was working out an elaborate solution with arrays and all sorts of junk when it dawned on me that the answer was easier than I could have imagined...

Apparently all I have to do to get the \x in there is this:
Code:
input='\x'00'\x'01'\x'02'\x'03

I think I'm going to go hide now...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to replace with "sed" some hex values by other hex values?

Assume I have a file \usr\home\\somedir\myfile123.txt and I want to replace all occurencies of the two (concatenated) hex values x'AD' x'A0' bytwo other (concatenated) hex values x'20' x'6E' How can I achieve this with the gnu sed tool? Additional question: Is there a way to let sed show... (1 Reply)
Discussion started by: pstein
1 Replies

2. Programming

What is the difference between ios::hex and std::hex?

Hi, Is there really a difference between these two, std::hex and ios::hex?? I stumbled upon reading a line, "std::ios::hex is a bitmask (8 on gcc) and works with setf(). std::hex is the operator". Is this true? Thanks (0 Replies)
Discussion started by: royalibrahim
0 Replies

3. Shell Programming and Scripting

bash built-in

Is there any command or VARIABLE in unix to display only bash builtin commands?. Some days back I worked on that, but now I do not remember. Can anyone please reply for this?... (4 Replies)
Discussion started by: gwgreen1
4 Replies

4. Programming

After converting the hexstr to Hex and storing the Hex in a char*

Hi All, My main intension of is to convert the Hexstring stored in a char* into hex and then prefixing it with "0x" and suffix it with ',' This has to be done for all the hexstring char* is NULL. Store the result prefixed with "0x" and suffixed with ',' in another char* and pass it to... (1 Reply)
Discussion started by: rvan
1 Replies

5. Solaris

Epic Editor was not able to obtain a license for your use. Feature Epic Editor :Licen

Epic Editor was not able to obtain a license for your use. Feature Epic Editor :License server is down (1 Reply)
Discussion started by: durgaprasadr13
1 Replies

6. Shell Programming and Scripting

set EDITOR=vi -> default editor not setting for cron tab

Hi All, I am running a script , working very fine on cmd prompt. The problem is that when I open do crontab -e even after setting editor to vi by set EDITOR=vi it does not open a vi editor , rather it do as below..... ///////////////////////////////////////////////////// $ set... (6 Replies)
Discussion started by: aarora_98
6 Replies

7. UNIX for Dummies Questions & Answers

Pasting text in VI editor from a different editor

Hi, I knw its a silly question, but am a newbie to 'vi' editor. I'm forced to use this, hence kindly help me with this question. How can i paste a chunk 'copied from' a different editor(gedit) in 'vi editor'? As i see, p & P options does work only within 'vi'. (10 Replies)
Discussion started by: harishmitty
10 Replies

8. Shell Programming and Scripting

ksh built-in function

Does anyone know why the following expression return an error ] while the following one not ?? Thanks (1 Reply)
Discussion started by: solea
1 Replies

9. Shell Programming and Scripting

BUILT-IN command scripts

I am new to Unix scripting. I would like to know if someone can point me to a site which lists any built-in commands in there scripted form. I thought to start with the basics and learn from example. Thanks JSP (2 Replies)
Discussion started by: JSP
2 Replies

10. UNIX for Dummies Questions & Answers

awk built in variables

Dear experts I am learning awk command through some books on Solaris 8. I have tested the folloing command awk 'BEGIN { print match ("And" , /d/)}' then the result is as following awk: syntax error near line 1 awk: illegal statement near line 1 Could you please help on this and just... (4 Replies)
Discussion started by: Reza Nazarian
4 Replies
Login or Register to Ask a Question