Tools

A Bash Script to Convert brainfuck Code Into C

Brainfuck is a minimalist Turing Complete language. You can read about it here.

The following bash script takes a file with brainfuck code (legal characters are >< ,.+-[] any other characters are ignored), and generates its C equivalent code, which can then be compiled with a C compiler and executed.

The output of this script lacks indentation. If you insist on having the C code indented you can achieve this by passing the output through a pipe to “indent”, “astyle” or similar programs.

The script should be called in the following way:

./bf.sh code.bf > code.c

Where code.bf is the file containing the brainfuck code.

› Continue reading

Post to Twitter Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to Ping.fm Post to Reddit Post to StumbleUpon

Tags: , , ,

Sunday, June 14th, 2009 Programming No Comments

Tiny Bash Functions to Convert Between Numeric Representations

I have these functions in my aliases file. They make it easy to convert between hex, binary and decimal representations of numbers. You might find it useful.

Just add these to your aliases file (imported from ~/.bashrc):

1
2
3
4
5
6
function h2d { echo "obase=10; ibase=16; $( echo "$*" | sed -e 's/0x//g' -e 's/\([a-z]\)/\u\1/g' )" | bc; }
function h2b { echo "obase=2; ibase=16; $( echo "$*" | sed -e 's/0x//g' -e 's/\([a-z]\)/\u\1/g' )" | bc; }
function b2d { echo "obase=10; ibase=2; "$*"" | bc; }
function b2h { echo "0x$(echo "obase=16; ibase=2;"$*"" | bc)"; }
function d2b { echo "obase=2; ibase=10; "$*"" | bc; }
function d2h { echo "0x$(echo "obase=16; ibase=10; "$*"" | bc)"; }

Now you should be able to use it like this:

h2d 0xff

The output will be 255 in this case.

h2b is for “hex to binary”, the others are similar (h: hex, b: binary, d: decimal)

Enjoy ;)

Post to Twitter Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to Ping.fm Post to Reddit Post to StumbleUpon

Tags: , ,

Friday, June 12th, 2009 Bash No Comments

A Program for Bit-Slicing

I wrote this short program in C because I needed to do some bit-slicing in some homework. It's not too generic (accepts only 32 bit integers) but it can help in most cases.


An example usage of the program:

bs 0x12345678 31 4


Will give the following output:

0x12345678[31:4] = 0x1234567

› Continue reading

Post to Twitter Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to Ping.fm Post to Reddit Post to StumbleUpon

Tags: ,

Wednesday, June 10th, 2009 Tools 1 Comment
my email
Already a member?
Login
Login using Facebook:
Last visitors
my photo
Hi,
My name is Amir Watad. I have a BSc. in biomedical engineering from The Biomedical Engineering school , Technion , Israel, and am currently studying for a BSc. in electrical engineering at The Electrical Engineering school , Technion , Israel.
I work at the verification dept. in Mellanox Technologies Ltd.
I love Linux, the Command Line and the OpenSource Community.
I used to write Poems (Arabic) when I was able to find time for this.
March 2010
S M T W T F S
« Jan    
 123456
78910111213
14151617181920
21222324252627
28293031  
Opensource Logo
SEO Powered by Platinum SEO from Techblissonline

Twitter links powered by Tweet This v1.6.1, a WordPress plugin for Twitter.