Reading binary content


 
Thread Tools Search this Thread
Operating Systems Solaris Reading binary content
# 22  
Old 10-20-2015
Closer but still a couple of issues:

Code:
$ od -tu1 file
0000000 014 066 202 255 019 245 240 233 158 067 242 233 144 115 182 186
0000020 160 127 044 156 086 172 078 022 251 106 139 122 032 244 210 223
0000040
$ od -tx1 file
0000000 0e 42 ca ff 13 f5 f0 e9 9e 43 f2 e9 90 73 b6 ba
0000020 a0 7f 2c 9c 56 ac 4e 16 fb 6a 8b 7a 20 f4 d2 df
0000040
$ od -An -v -tu1 file | tr -s ' ' $'\n' | while read VALUE; do  printf "%02X\t" $VALUE;   for ((i=7; i>=0; i--)); do printf "%d" $(( (VALUE>>i) %2 )); done; printf "\n"; done
00      00000000
0C      00001100
36      00110110
CA      11001010
FF      11111111
bash: printf: 019: invalid number
00      bash: 019: value too great for base (error token is "019")

# 23  
Old 10-20-2015
Quote:
Originally Posted by jlliagre
You are testing with an ASCII file so are missing to observe these negative values. Use a truly binary input file.
It is also compiler dependent. With some compilers type char is signed; in other compilers it is unsigned. In compilers with type char == type signed char, you'll get negative numbers from the bytes with the high bit set when printing in decimal format as in:
Code:
printf $'\xc4'$'\x74' |od -td1u1o1x1
0000000   -60 116                                                        
          196 116
          304 164                                                        
           c4  74                                                        
0000002

but on systems where od was built using a compiler with type char == type unsigned char, you'll get the output:
Code:
0000000   196 116                                                        
          196 116
          304 164                                                        
           c4  74                                                        
0000002

with the same input.

Note also that with od -An you get leading spaces where the address would go if you were printing it. That is causing some unwanted zeros to appear in the output corresponding to the start of each line of od output. To get around both of these problem you could try:
Code:
od -An -v -tu1 file | tr -s ' ' $'\n' |
    grep -v '^$' |
    while read VALUE
    do	printf "%02X\t" $VALUE
	for ((i=7; i>=0; i--))
	do	printf "%d" $(( (VALUE>>i) %2 ))
	done
    printf "\n"
done

I see RudiC already posted the -tu1 fix. I hope the explanation of why -td1 didn't work helps and that the added grep gives you what you want.

I don't have a Solaris system I can use for testing, but on OS X I'm not getting leading zeros printed by od -tu1, but if that is a problem on Solaris systems, you could change the:
Code:
tr -s ' ' $'\n' | grep -v '^$'

in the pipeline to:
Code:
/usr/xpg4/bin/awk '{for(i=1;i<=NF;i++){sub(/^0+/,"",$i);printf("%u\n",$i)}}'

# 24  
Old 10-20-2015
How about this:-
Code:
#!/bin/sh --posix
# bin.sh
dd if=/dev/urandom of=/tmp/binary bs=16 count=1
od -tx1 /tmp/binary
echo "Binary decode of hexdump display above..."
for subscript in {0..15}
do
	printf "%08d\n" $(echo 'ibase=10; obase=2;'"$(od -An -N1 -j$subscript -tu /tmp/binary)" | bc)
done

Results for just 16 bytes generated...
Code:
Last login: Tue Oct 20 22:03:13 on ttys000
AMIGA:barrywalker~> cd Desktop/Code/Shell
AMIGA:barrywalker~/Desktop/Code/Shell> ./bin_old.sh
1+0 records in
1+0 records out
16 bytes transferred in 0.000036 secs (444430 bytes/sec)
0000000    b9  ed  ed  fe  39  86  fd  59  36  2e  65  dd  3c  ee  ef  ed
0000020
Binary decode of hexdump display above...
10111001
11101101
11101101
11111110
00111001
10000110
11111101
01011001
00110110
00101110
01100101
11011101
00111100
11101110
11101111
11101101
AMIGA:barrywalker~/Desktop/Code/Shell> _

# 25  
Old 10-22-2015
@guddu_12 Beware that the initial C source code I posted had a bug, it is fixed now. It takes 30 ms for a 10k file.

@wisecracker {0..15} is not POSIX. In any case, as already stated, your algorithm is too slow to be usable with anything but tiny files. It takes 26 minutes for the same 10k file

@Don Cragun, with the extra awk code, your script works fine under Solaris and takes 17 seconds for the same 10k file.

Here is a mostly awk based solution that takes 230 ms:

Code:
od -An -v -tx1 binary_file |
                /usr/xpg4/bin/awk '
                BEGIN {
                        for(i=0;i<256;i++) {
                                s=""
                                p=128
                                for(j=0;j<8;j++) {
                                        if (int((i/p)%2)==0)
                                                s=s "0"
                                        else
                                                s=s "1"
                                        p=p/2
                                }
                                hi=sprintf("%02x",i)
                                bin[""hi]=s
                                # printf(" %s:%s\n",i,bin[""hi])
                        }
                }
                {
                        for(i=1;i<=NF;i++) {
                                printf("%s\n",bin["" $i])
                        }
                }'

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