Sponsored Content
Full Discussion: Reading binary content
Operating Systems Solaris Reading binary content Post 302958115 by jlliagre on Monday 19th of October 2015 12:51:04 PM
Old 10-19-2015
Here is a working solution based on cero suggestion, although extremely inefficient. Shell scripting is not really adapted to this kind of tasks.

Code:
od -An -v -t d1 yourbinary |
  while read line ; do
    for byte in $line ; do
      if [ $byte -lt 0 ] ; then byte=$((byte+256)); fi
      printf "%08.8s" $(echo "ibase=10;obase=2;$byte"|bc)
    done
  done

Here is how I would do it in C:

Code:
main(){
        int i,j,cc;
        static unsigned char buf[1024];
        static unsigned char bin[256][9];
        for(i=0;i<256;i++) {
                for(j=0;j<8;j++) {
                        bin[i][7-j]=i&(1<<j)?'1':'0';
                }
                bin[i][8]=0;
        }
        while((cc=read(0,buf,1024))>0)
                for(i=0;i<cc;i++)
                        printf("%s",bin[buf[i]]);
        printf("\n");
}

The shell script takes 2 min 10 s for a 10 kB file while the compiled C takes about 10 milliseconds for the same input.

Last edited by jlliagre; 10-21-2015 at 06:24 AM..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
GLCALLLISTS(3G) 														   GLCALLLISTS(3G)

NAME
glCallLists - execute a list of display lists C SPECIFICATION
void glCallLists( GLsizei n, GLenum type, const GLvoid *lists ) PARAMETERS
n Specifies the number of display lists to be executed. type Specifies the type of values in lists. Symbolic constants GL_BYTE, GL_UNSIGNED_BYTE, GL_SHORT, GL_UNSIGNED_SHORT, GL_INT, GL_UNSIGNED_INT, GL_FLOAT, GL_2_BYTES, GL_3_BYTES, and GL_4_BYTES are accepted. lists Specifies the address of an array of name offsets in the display list. The pointer type is void because the offsets can be bytes, shorts, ints, or floats, depending on the value of type. DESCRIPTION
glCallLists causes each display list in the list of names passed as lists to be executed. As a result, the commands saved in each display list are executed in order, just as if they were called without using a display list. Names of display lists that have not been defined are ignored. glCallLists provides an efficient means for executing more than one display list. type allows lists with various name formats to be accepted. The formats are as follows: GL_BYTE lists is treated as an array of signed bytes, each in the range -128 through 127. GL_UNSIGNED_BYTE lists is treated as an array of unsigned bytes, each in the range 0 through 255. GL_SHORT lists is treated as an array of signed two-byte integers, each in the range -32768 through 32767. GL_UNSIGNED_SHORT lists is treated as an array of unsigned two-byte integers, each in the range 0 through 65535. GL_INT lists is treated as an array of signed four-byte integers. GL_UNSIGNED_INT lists is treated as an array of unsigned four-byte integers. GL_FLOAT lists is treated as an array of four-byte floating-point values. GL_2_BYTES lists is treated as an array of unsigned bytes. Each pair of bytes specifies a single display-list name. The value of the pair is computed as 256 times the unsigned value of the first byte plus the unsigned value of the second byte. GL_3_BYTES lists is treated as an array of unsigned bytes. Each triplet of bytes specifies a single display-list name. The value of the triplet is computed as 65536 times the unsigned value of the first byte, plus 256 times the unsigned value of the second byte, plus the unsigned value of the third byte. GL_4_BYTES lists is treated as an array of unsigned bytes. Each quadruplet of bytes specifies a single display-list name. The value of the quadruplet is computed as 16777216 times the unsigned value of the first byte, plus 65536 times the unsigned value of the second byte, plus 256 times the unsigned value of the third byte, plus the unsigned value of the fourth byte. The list of display-list names is not null-terminated. Rather, n specifies how many names are to be taken from lists. An additional level of indirection is made available with the glListBase command, which specifies an unsigned offset that is added to each display-list name specified in lists before that display list is executed. glCallLists can appear inside a display list. To avoid the possibility of infinite recursion resulting from display lists calling one another, a limit is placed on the nesting level of display lists during display-list execution. This limit must be at least 64, and it depends on the implementation. GL state is not saved and restored across a call to glCallLists. Thus, changes made to GL state during the execution of the display lists remain after execution is completed. Use glPushAttrib, glPopAttrib, glPushMatrix, and glPopMatrix to preserve GL state across glCallLists calls. NOTES
Display lists can be executed between a call to glBegin and the corresponding call to glEnd, as long as the display list includes only com- mands that are allowed in this interval. ERRORS
GL_INVALID_VALUE is generated if n is negative. GL_INVALID_ENUM is generated if type is not one of GL_BYTE, GL_UNSIGNED_BYTE, GL_SHORT, GL_UNSIGNED_SHORT, GL_INT, GL_UNSIGNED_INT, GL_FLOAT, GL_2_BYTES, GL_3_BYTES, GL_4_BYTES. ASSOCIATED GETS
glGet with argument GL_LIST_BASE glGet with argument GL_MAX_LIST_NESTING glIsList SEE ALSO
glCallList, glDeleteLists, glGenLists, glListBase, glNewList, glPushAttrib, glPushMatrix GLCALLLISTS(3G)
All times are GMT -4. The time now is 03:34 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy