Reading binary content


 
Thread Tools Search this Thread
Operating Systems Solaris Reading binary content
# 8  
Old 10-19-2015
Demo using OSX 10.7.5, default bash terminal...
Code:
#!/bin/bash
# bin.sh
dd if=/dev/urandom of=/tmp/binary count=1
for subscript in {0..511}
do
	num=`hexdump -n1 -s$subscript -v -e '1/1 "%u"' /tmp/binary`
	echo "Decimal number=$num..."
	echo "ibase=10; obase=2; $num" | bc
done

Results:-
Code:
Last login: Mon Oct 19 20:49:24 on ttys000
AMIGA:barrywalker~> cd Desktop/Code/Shell
AMIGA:barrywalker~/Desktop/Code/Shell> ./bin.sh
1+0 records in
1+0 records out
512 bytes transferred in 0.000129 secs (3969471 bytes/sec)
Decimal number=245...
11110101
Decimal number=234...
11101010
Decimal number=130...
10000010
Decimal number=107...
1101011
Decimal number=226...
11100010
Decimal number=34...
100010
Decimal number=146...
10010010
Decimal number=229...
11100101
Decimal number=129...
10000001
.
.
.
.
.
Decimal number=96...
1100000
Decimal number=171...
10101011
Decimal number=145...
10010001
Decimal number=39...
100111
Decimal number=30...
11110
Decimal number=27...
11011
Decimal number=71...
1000111
Decimal number=66...
1000010
Decimal number=169...
10101001
Decimal number=235...
11101011
Decimal number=245...
11110101
AMIGA:barrywalker~/Desktop/Code/Shell> _

EDIT:-
This might be of interest too...
Another Building Block, Binary File Manipulation...

Last edited by wisecracker; 10-19-2015 at 06:07 PM.. Reason: See above...
# 9  
Old 10-19-2015
shell doesn't have to be slow, but C will usually be faster.

Code:
od -An -t x1 -v yourfilename.bin |
 tr -s 'a-f ' 'A-F\012' |
 (echo 16;echo i;echo 2;echo o;sed -e 1d -e 's/^/F/' -e 'a\p') |
 dc -f - |
 sed 's/^1111//' |
 tr -d '\012'

(not totally convinced I know that the OP is wanting though)
# 10  
Old 10-20-2015
This being the Solaris forum, you should stick to Posix or Solaris commands, options and syntax.

@wisecracker:
bash: hexdump: command not found

@cjcox: your code is indeed much better but has a several issues with Solaris.
Code:
sed: command garbled: a\p

With the a\p syntax fixed, the output is still broken:

Code:
0111111101000101empty stack01000110000000010000000100000001000000000000000000000
00000000000000000000000000000000000000000000000000011 is unimplemented0000001000
00000000000011000000000000000100000000000000000000000000000000142 is unimplement
ed000000000101000010000011010000000000000000000000000011 is unimplemented1000100
00010001100000000000000000000000000000000000000000000000000110100000000000010000
0000000000000011000000000001010000000000011 is unimplemented00010111000000000001
01010000000000000110000000000000000000000000001101000000000000000000000000000011
010000000000000001010000100011 is unimplemented000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000010100000000000000000000000
011 is unimplemented000000000000000000000000000000000000001100000000000000000000
0000111101000000000000000000000000000000000000000000000000000000000011 is unimpl
emented0000000000000000000000000000000000010001000000000000000000000000000000000
000000000000000000000000000010000000000000000000000000011 is unimplemented000000
00000000000000000000000000000000010000000000000000000000000000000000000000000000
00000000000000000000000000000001010000100011 is unimplemented0000000000000000000
00000000000000010142 is unimplemented000100000000000000000010142 is unimplemente
d000100000000000000000000010100000000000000000000000011 is unimplemented00000000
00000000000000010000000000000001000000000000000000000000000000000010000000000000
000000000000000000100000000001100000100011 is unimplemented000000000000000000000
000hex digit > 16out of stack space145 is unimplementedhex digit > 16out of stack

# 11  
Old 10-20-2015
Hi,

Where is the file name in c program which read binary
# 12  
Old 10-20-2015
It's reading from stdin - file descriptor 0.
# 13  
Old 10-20-2015
Hi jlliagre...
OK, using shell and od only in OSX 10.7.5 default terminal limited to 32 bytes for this basic DEMO. It can easily be enhanced upon...
(Both hexdump and od are used in AudioScope.sh as CygWin does not have hexdump.)
Code:
#!/bin/sh
# bin.sh
dd if=/dev/urandom of=/tmp/binary bs=32 count=1
for subscript in {0..31}
do
	num=`od -An -N1 -j$subscript -tu /tmp/binary`
	echo "Decimal number ="$num"..."
	echo "ibase=10; obase=2; $num" | bc
done

Results:-
Code:
Last login: Tue Oct 20 14:00:40 on ttys000
AMIGA:barrywalker~> cd Desktop/Code/Shell
AMIGA:barrywalker~/Desktop/Code/Shell> ./bin.sh
1+0 records in
1+0 records out
32 bytes transferred in 0.000031 secs (1032444 bytes/sec)
Decimal number = 48 ...
110000
Decimal number = 82 ...
1010010
Decimal number = 118 ...
1110110
Decimal number = 152 ...
10011000
Decimal number = 42 ...
101010
Decimal number = 11 ...
1011
Decimal number = 185 ...
10111001
Decimal number = 185 ...
10111001
Decimal number = 101 ...
1100101
Decimal number = 238 ...
11101110
Decimal number = 6 ...
110
Decimal number = 11 ...
1011
Decimal number = 87 ...
1010111
Decimal number = 62 ...
111110
Decimal number = 12 ...
1100
Decimal number = 94 ...
1011110
Decimal number = 1 ...
1
Decimal number = 142 ...
10001110
Decimal number = 233 ...
11101001
Decimal number = 102 ...
1100110
Decimal number = 57 ...
111001
Decimal number = 39 ...
100111
Decimal number = 149 ...
10010101
Decimal number = 134 ...
10000110
Decimal number = 46 ...
101110
Decimal number = 94 ...
1011110
Decimal number = 238 ...
11101110
Decimal number = 200 ...
11001000
Decimal number = 111 ...
1101111
Decimal number = 38 ...
100110
Decimal number = 191 ...
10111111
Decimal number = 248 ...
11111000
AMIGA:barrywalker~/Desktop/Code/Shell>

# 14  
Old 10-20-2015
Try also this bashism:
Code:
od -An -w1 -td1 file | while read VALUE; do for ((i=7; i>=0; i--)); do printf "%d" $(( (VALUE>>i)%2 )); done; printf "\n"; done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Reading flat file content

is there any unix tools that can read the text files like through SQL queries? (ie in Hadoop, Impala DB support flat file query) (1 Reply)
Discussion started by: omykarshan
1 Replies

2. Programming

How to replicate Ruby´s binary file reading with Java?

Hello to all guys, Maybe some expert could help me. I have a working ruby script shown below that reads a big binary file (more than 2GB). The chunks of data I want to analyze is separated by the sequence FF47 withing the binary. So, in the ruby script is defined as "line separator" =... (10 Replies)
Discussion started by: Ophiuchus
10 Replies

3. Shell Programming and Scripting

Trouble reading content of file from a variable

Hi , i have a parameter which has path of a file. Now i need to have another parameter with the content of that file. I tried the belwo script , can any one please help. I dont want to use cat command to read. Can we do it with out using cat command. while read line do... (9 Replies)
Discussion started by: Ravindra Swan
9 Replies

4. Programming

help with reading a binary file and fseek

this is my code and no matter what record number the user enters i cant get any of the records fields to read into the structure acct. What am i doing wrong? #include <stdio.h> typedef struct { char name; int number; float balance; } acct_info_t; int main (int... (0 Replies)
Discussion started by: bjhum33
0 Replies

5. Programming

reading binary files

#include <stdio.h> /* typedef struct { char name; int number; float balance; } acct_info_t; */ int main() { FILE *fptr; fptr = fopen("acct_info", "r"); int magic = 5; fseek(fptr,3,SEEK_SET); fread(&magic,sizeof(int),1,fptr);... (7 Replies)
Discussion started by: robin_simple
7 Replies

6. Shell Programming and Scripting

Reading content of a variable to create a new one?

Hello. I've written up a script, that populates a variable with a list of tapes returned from my library. For example: 701940L3,701941L3,701942L3,701943L3,701944L3,701945L3,701946L3,701947L3,701948L3 So now, the variable "TAPELIST" contains those numbers, delimited by commas. I'd like to... (6 Replies)
Discussion started by: Stephan
6 Replies

7. Shell Programming and Scripting

Problem in reading a file content

Hi, I am reading a file line by line using read line function of while loop. Each line contains 4 fields. I want to take these 4 values in 4 variables in each iteration so that i can use them in my script. The issue here is that my awk command is returning awkward results - Here is a sample line... (8 Replies)
Discussion started by: garman
8 Replies

8. Programming

Reading a binary file in text or ASCII format

Hi All, Please suggest me how to read a binary file in text or ASCII format. thanks Nagendra (3 Replies)
Discussion started by: Nagendra
3 Replies

9. Shell Programming and Scripting

Reading Numerical Binary Data using KSH

Hi, I've searched and couldn't find anyone else with this problem. Is there anyway (preferably using ksh - but other script languages would do) that I can read in binary float data into a text file. The data (arrays from various stages of radar processing) comes in various formats, but mainly... (3 Replies)
Discussion started by: Jonny2Vests
3 Replies

10. Programming

Reading from a binary file

I'm having trouble with reading information back into a program from a binary file. when i try to display the contents of the file i get a Memory fault(coredump). would anyone be able to assist? this is my fread line fread(&file_data,sizeof(struct book_type),1,fileSave); ive also tried it without... (3 Replies)
Discussion started by: primal
3 Replies
Login or Register to Ask a Question