binary/hex output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting binary/hex output
# 1  
Old 08-13-2008
binary/hex output

i want to output something like
2f 00 00 00

but i can't seem to escape characters like i'm used to in some programming languages, so with this:

echo "/\0\0\0" >> outputfile

i actually get
2f 5c 30 5c 30 5c 30 0a

ie the \0 isn't giving me the 00 i need, and in addition it has got an extra couple of bytes on the end - 0a (return carriage?).

obviously i'm going about this the wrong way - any suggestions? thanks.
# 2  
Old 08-13-2008
Code:
/home/jmcnama> printf "%c%c%c" 0 0 0 | od -c
0000000   0   0   0
0000003

does generate three ascii nul characters...
# 3  
Old 08-13-2008
Try without the quotes.

Code:
echo 2f \0\0 \0\0 \0\0
2f 00 00 00

# 4  
Old 08-15-2008
hm, without the quotes it still seems to just give the character values

2f 00 00 00

writes a file like this

32 66 20 30 30 20 30 30 20 30 30 0A

(32 for the '2' character, 66 for 'f', 20 for ' ' etc., and still with a return carriage at the end)

likewise with "%c" 0, this gives actual hex byte value of 30 (for an ascii '0' character)

any ideas how to get round this?
# 5  
Old 08-15-2008
How about echo -n /... | tr . '\000'

... or use Perl (or awk, or ...)

Code:
perl -e 'printf "/\0\0\0"'

# 6  
Old 08-15-2008
didn't know about -n, great thanks. what's the tr for? '.' seems to be 2e rather than 00
# 7  
Old 08-15-2008
the tr bit seems to do the trick but i dont get why.. thanks though
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert Binary File To Hex In Linux

dHi, I have the attached file(actual file can be extracted post unzipping it) & i am trying to use the following code for coversion to hex format. Starting hex value is 84 which is start of the record & termination is done using 00 00 followed by 84(hex) which i can see in the dump clearly using... (14 Replies)
Discussion started by: siramitsharma
14 Replies

2. 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

3. Shell Programming and Scripting

Convert binary file to csv and then back to the binary format

Hello *nix specialists, Im working for a non profit organisation in Germany to transport DSL over WLAN to people in areas without no DSL. We are using Linksys WRT 54 router with DD-WRT firmware There are at the moment over 180 router running but we have to change some settings next time. So my... (7 Replies)
Discussion started by: digidax
7 Replies

4. Shell Programming and Scripting

Output redirection of c binary file to a file in shell script is failing

I am struck up with a problem and that is with output redirection. I used all the ways for the redirection of the output of c binary to a file, still it is failing. Here are the different ways which I have used: ./a.out | tee -a /root/tmp.txt 2>&1 ./a.out | tee -a /root/tmp.txt 1>&1 ./a.out |... (2 Replies)
Discussion started by: Maya29988
2 Replies

5. 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

6. Shell Programming and Scripting

To log binary file output to a txt file

Hi, I wrote a small script whose function is to execute the postemsg provided if the threshold breaches. I want to log this postemsg messages to a log file. But I am not able to do. Can someone throw some light on how to log the output of this. I am pasting a snippet of that code. ... (2 Replies)
Discussion started by: dbashyam
2 Replies

7. Shell Programming and Scripting

convert hex to binary and send to variable

Folks, can anyone help with a script to convert hex to binary digits, and break the 32 bit binary into packs of 4 and send them to 8 different variables.Any help is sincerely appreciated. Thanks venu Its in korn shell...... (24 Replies)
Discussion started by: venu
24 Replies

8. 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

9. UNIX for Advanced & Expert Users

Modifying binary file by editing Hex values ?

Hi , i'm using special binary file (lotus notes) and modifying an hexadecimal address range with windows hex editor and it works fine ! The file is an AIX one and i'm forced to transfert (ftp) it before modifying it and re-transfert ! NOW i would do this directly under AIX ! I can... (4 Replies)
Discussion started by: Nicol
4 Replies

10. Shell Programming and Scripting

Binary and hex in unix

not much familiar with binary and hex calculation in script programming.... explaination: binary format control the parameter turned on or off in the program stored in hex mode, the question is: how to change 39e to 19e using the binary calculation(although i don't know the command for... (2 Replies)
Discussion started by: trynew
2 Replies
Login or Register to Ask a Question