2

What I need to do is to read binary inputs from a file. The inputs are for example (binary dump),

00000000 00001010 00000100 00000001 10000101 00000001 00101100 00001000 00111000 00000011 10010011 00000101

What I did is,

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);
}

But the problem here is that, I need to divide those binary inputs in some ways so that I can use it as a command (e.g. 101 in the input is to add two numbers)

But when I run the program with the code I wrote, this provides me an output like:

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

which shows in ASCII numbers.

How can I read the inputs as binary numbers, not ASCII numbers?

The inputs should be used in this way:

0 # Padding for the whole file! 
0000|0000 # Function 0 with 0 arguments 
00000101|00|000|01|000 # MOVE the value 5 to register 0 (000 is MOV function)
00000011|00|001|01|000 # MOVE the value 3 to register 1 
000|01|001|01|100 # ADD registers 0 and 1 (100 is ADD function)
000|01|0000011|10|000 # MOVE register 0 to 0x03 
0000011|10|010 # POP the value at 0x03 
011 # Return from the function 
00000110 # 6 instructions in this function

I am trying to implement some sort of like assembly language commands

Can someone please help me out with this problem?

Thanks!

10
  • 1
    Maybe I miss understand what do you mean by binary input ? You need to read the number directly ? In this case what is your format ? Commented Apr 8, 2018 at 6:20
  • yes! I need to read the binary numbers directly. Printing as buffer - '0' gave me strange outputs.. Commented Apr 8, 2018 at 6:22
  • Ok, but we need for information, what is the format of your data ? little endian, big endian, how many byte ? how many bit ? there are too many possibility right now to give you a correct answer. I count 53 bit in your exemple, that seem very strange. Commented Apr 8, 2018 at 6:24
  • 1
    Well, it's gonna be a long answer ;) Your question is more clear, now. Commented Apr 8, 2018 at 6:31
  • 1
    You input is either 3-integer values, 6-short values, or 12-char values. You can read the input into arrays of either and then use bitwise operators to extract the bits as needed. Commented Apr 8, 2018 at 7:13

2 Answers 2

2

You need to understand the difference between data and its representation. You are correctly reading the data in binary. When you print the data, printf() gives the decimal representation of the binary data. Note that 00001010 in binary is the same as 10 in decimal and 00000100 in binary is 4 in decimal. If you convert each sequence of bits into its decimal value, you will see that the output is exactly correct. You seem to be confusing the representation of the data as it is output with how the data is read and stored in memory. These are two different and distinct things.

The next step to solve your problem is to learn about bitwise operators: |, &, ~, >>, and <<. Then use the appropriate combination of operators to extract the data you need from the stream of bits.

Sign up to request clarification or add additional context in comments.

Comments

1

The format you use is not divisible by a byte, so you need to read your bits into a circular buffer and parse it with a state machine.

Read "in binary" or "in text" is quite the same thing, the only thing that change is your interpretation of the data. In your exemple you are reading a byte, and you are printing the decimal value of that byte. But you want to print the bit of that char, to do that you just need to use binary operator of C.

For example:

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <limits.h>

struct binary_circle_buffer {
    size_t i;
    unsigned char buffer;
};

bool read_bit(struct binary_circle_buffer *bcn, FILE *file, bool *bit) {
    if (bcn->i == CHAR_BIT) {
        size_t ret = fread(&bcn->buffer, sizeof bcn->buffer, 1, file);
        if (!ret) {
            return false;
        }
        bcn->i = 0;
    }
    *bit = bcn->buffer & ((unsigned char)1 << bcn->i++); // maybe wrong order you should test yourself
    // *bit = bcn->buffer & (((unsigned char)UCHAR_MAX / 2 + 1) >> bcn->i++);
    return true;
}

int main(void)
{
    struct binary_circle_buffer bcn = { .i = CHAR_BIT };

    FILE *file = stdin; // replace by your file
    bool bit;
    size_t i = 0;
    while (read_bit(&bcn, file, &bit)) {
        // here you must code your state machine to parse instruction gl & hf
        printf(bit ? "1" : "0");
        if (++i >= 7) {
            i = 0;
            printf(" ");
        }
    }
}

Help you more would be difficult, you are basically asking help to code a virtual machine...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.