0

Saying that the input is

0000000000000101000000100000000011000010100000001001011000000100000111000000000111001001100000101

The code I wrote is

#include <stdio.h>

#define BYTE unsigned char

int main(int n_args, char** vargs)
{

    char* filename = vargs[1];
    BYTE buffer;
    FILE *file_ptr = fopen(filename,"rb");

    fseek(file_ptr, 0, SEEK_END); 

    size_t file_length = ftell(file_ptr);

    rewind(file_ptr);

    for (int i = 0; i < file_length; i++)
    {
        fread(&buffer, 1, 1, file_ptr); // read 1 byte
        printf("%d ", (int)buffer);
    }

    return 0;
}

However, the output is something like

0 0 10 4 1 133 1 44 8 56 3 147 6

I think it is printed as decimal. How can I convert those outputs into 8-bits binary numbers using bitwise operators including padding bits?

What I want to do with those binary inputs is to make a form of something like:

0000 # padding
0000 0000 # function 0 with 0 arg
00010000 00 0000011 10 000 # MOV 16 to 0x03 
0000010 10 000 01 000 # MOV 0x02 to r0
00000101 00 000 01 000 # MOV 5 to r0 
000 01 001 01 100 # ADD r0 r1 
000 01 0000010 10 000 # MOV r0 to 0x02
00001000 00 0000011 10 000 # MOV 8 to 0x03
0000011 10 010 # POP 0x03
011 # RET 
00001000 # 8 instructions

Can anyone please help me out with this problem?

Thanks...

승기유
  • 133
  • 9
  • 1
    Do some [research](https://stackoverflow.com/questions/111928/is-there-a-printf-converter-to-print-in-binary-format?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa) please. You're not the first one wanting to print binary representation. – Tom's Apr 09 '18 at 14:39
  • Write a function and fed it, the decimal you are getting. This function should convert the decimal into equivalent binary form. And the format you have showed isn't a consistent probably. – Mazhar Apr 09 '18 at 14:39
  • So finally, you want to read a format ? What is this format ? – Tom's Apr 09 '18 at 14:41
  • It is like ([Address][Address_type])[Address][Address_type][Operation Code] – 승기유 Apr 09 '18 at 14:45
  • ??? And how bit long is each type ? Because what you display seem to have 4, 2, 7, 3, 8 bits, so frankly, I really don't understand and I will not try. What's the name of the format ? It's ELF ? It's a BMP ? what is it ? – Tom's Apr 09 '18 at 14:49
  • If you print with `%d`, you get decimal notation. You could print with `%x` or `%X` for hex, or `%o` for octal. There isn't a `%b` for printing binary. Your output layout seems random — I can't see the rationale for the changes in format. You'll have to write the 'output as binary' code anyway. – Jonathan Leffler Apr 09 '18 at 16:30

0 Answers0