LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-25-2007, 05:04 PM   #1
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
write hex bytes to file


I want to generate a file with a range of binary bytes. As an example, the script below writes the bytes 0x00 thru 0xff, with each byte followed by 0x0a (line feed):
Code:
#!/bin/bash
#this crude little script writes 0x00 thru 0xff to a file

rm hexfile 2>/dev/null
for i in {0..15}; do
        for j in {0..15}; do
                hex=`printf "%x" $i``printf "%x" $j`
                printf "\x$hex\n" >>hexfile
        done
done
The question is: Is there an easier way to do this?
 
Old 06-25-2007, 05:14 PM   #2
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 57
Why a double loop when only one is needed?
Hum I'm probably missing the point here

`seq 0 255`

I think the rest is fine and clear. You could shorten it but would loose visibility.

Last edited by nx5000; 06-25-2007 at 05:15 PM.
 
Old 06-25-2007, 05:26 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Original Poster
Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by nx5000
Why a double loop when only one is needed?
`seq 0 255`
Indeed......
feeble answer: I was lost in the nuances of the printf syntax and lost sight of the forest.

What can you expect from a senior citizen who learned programming with Fortran + an IBM keypunch....
 
Old 06-25-2007, 05:31 PM   #4
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 57
Actually I don't know also the printf so much, I don't use it but it's quite helpful.
Hey I just looked your profile, you worked in Aerospace? I'm working in this field at the moment.
Anyway, have fun with your script ;-)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
converting a hex file to binary linx win Programming 3 07-20-2006 03:04 AM
How do i write to a serial port (modem) in ascii or hex directly? Taliesin.duchaos Linux - Networking 6 04-21-2006 08:31 AM
Search file for Hex value paddyjoy Programming 6 08-11-2005 01:11 AM
Send an .hex file by serial port ODSunal Programming 2 12-09-2004 08:12 PM
Attaching an intel Hex file. krushna Linux From Scratch 0 06-07-2004 03:47 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration