Reading binary content


 
Thread Tools Search this Thread
Operating Systems Solaris Reading binary content
# 15  
Old 10-20-2015
@wisecracker

With the correct shebang ("#!/bin/bash" as you use bash specific features), your script works under Solaris but is missing some code to answer the original question. Moreover, opening then skipping to each possible offset the input file for every byte in it is a very conterproductive approach.

@RudiC

"od -w" is non portable GNUism:

Code:
$ od -An -w1 -td1 file
usage: od [-bcCdDfFoOsSvxX] [-] [file] [offset_string]
       od [-bcCdDfFoOsSvxX] [-t type_string]... [-A address_base] [-j skip] [-N count] [-] [file...]

This User Gave Thanks to jlliagre For This Post:
# 16  
Old 10-20-2015
Thanks. Alas, I don't have access to Solaris for tests.Try, then,
Code:
od -An -td1 file | tr -s ' ' $'\n'

Not sure if tr -s is portable, though.
# 17  
Old 10-20-2015
Quote:
Originally Posted by jlliagre
This being the Solaris forum, you should stick to Posix or Solaris commands, options and syntax.
Smilie Sorry about that. I was in the Linux zone of mind.
# 18  
Old 10-20-2015
@RudiC, still requiring some improvement:

Code:
$ od -x file
0000000 cea9 b0db 13f8 f4f5 837a a463 9066 3d1c
0000020 b201 61d4 2a5c 7732 bf55 b480 88e7 82a8
0000040
$ od -An -td1 file | tr -s ' ' $'\n' | while read VALUE; do for ((i=7; i>=0; i--)); do printf "%d" $(( (VALUE>>i)%2 )); done; printf "\n"; done
00000000
-10-10-100-1
-1-100-1-1-10
-1-10-1-10-1-1
-10-1-10000
-1-1-1-1-1000
00010011
-1-1-1-10-10-1
-1-1-1-10-100
01111010
-100000-1-1
01100011
-10-100-100
01100110
-100-10000
00011100
00111101
00000000
00000001
-10-1-100-10
-1-10-10-100
01100001
01011100
00101010
00110010
01110111
01010101
-10-1-1-1-1-1-1
-10000000
-10-1-10-100
-1-1-100-1-1-1
-1000-1000
-10-10-1000
-100000-10

# 19  
Old 10-20-2015
Hmmm -
Code:
od -x file
0000000 6c73 6461 6a66 0a6b 6672 6676 000a

od -An -v -td1 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
73    01110011
6C    01101100
61    01100001
64    01100100
66    01100110
6A    01101010
6B    01101011
0A    00001010
72    01110010
66    01100110
76    01110110
66    01100110
0A    00001010

---------- Post updated at 19:19 ---------- Previous update was at 19:17 ----------

... works on FreeBSD as well ...

---------- Post updated at 19:21 ---------- Previous update was at 19:19 ----------

Not sure where the - signs come from. If you remove them, the output is correct.
# 20  
Old 10-20-2015
Quote:
Originally Posted by RudiC
Hmmm -
...
Not sure where the - signs come from. If you remove them, the output is correct.
You are testing with an ASCII file so are missing to observe these negative values. Use a truly binary input file.
This User Gave Thanks to jlliagre For This Post:
# 21  
Old 10-20-2015
Ohhh yes - that's it. Be back later.

---------- Post updated at 21:39 ---------- Previous update was at 21:35 ----------

Try the -tu1 instead of the -td1 option to od.
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