LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 03-04-2006, 08:21 AM   #1
zero79
Member
 
Registered: Nov 2003
Location: Ohio
Distribution: Debian Unstable
Posts: 460

Rep: Reputation: 30
writing binary data via the shell?


Hello,

I was wondering if there was any way to write raw binary data to a file? Say I want a file to contain the hex data 1f332d01, is there a way to do something like

Code:
$ echo 1f332d01 | hex2binary > file
$ hexdump file
0000000 1f33 2d01
where hex2binary is some tool (I don't know if something like it exists or not) that takes hex data on standard input and outputs binary data on standard output. Any tool that would allow me to accomplish the goal of putting the above data into a file would be very useful to me.

I found that someone has written C code to do essentially this (http://www.engr.umd.edu/~yavuz/softw...ex2Binary.htm), but I'd prefer not to have to bring in someone else's code especially since there is no license information. Also, I would like to use an existing shell tool if it exists, otherwise, I will just write a short python script.

Anyway, thanks for any thoughts.

Mike
 
Old 03-04-2006, 08:48 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
i'm not used to using hex really, but you can easily convert bases using bc:
Code:
echo "ibase=16; obase=2; 1F332D01" | bc
That'll give you your binary string, but if i use hexdump to read that when redirected, i don't get the original string back as fasr as i can tell.
 
Old 03-04-2006, 09:04 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
look at perl's 'pack' function maybe
 
Old 03-04-2006, 11:35 AM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
No need to use perl, a shell script can do it:
Code:
  while read w
  do
    for byte in $(echo $w | sed 's/../& /g')
    do
      printf "\\$(printf "%o" 0x$byte)"
    done
  done
 
  


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
Reading and Writing integers to binary file oulevon Programming 2 02-26-2006 12:27 AM
Python DCOP (KDE) and binary data carl.waldbieser Programming 0 12-22-2005 03:02 PM
Binary reading/writing in cpp kornerr Programming 1 11-27-2005 07:09 PM
recording raw binary data as an MP3 uman Linux - General 1 02-19-2005 09:07 PM
Problem in reading/writing binary data in Linux esi-eric Linux - Hardware 3 07-20-2004 04:21 PM

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

All times are GMT -5. The time now is 06:23 PM.

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