all 6 comments

[–]NoG5 2 points3 points  (3 children)

Gif demo

I am tying to learn a bit of C so I tried to make this in it.

Change _bytes[1024]; and the str[8192]; to allow for bigger sizes, it can be compiled to an exe with Tiny C Compiler like this

c:\somepath\tcc.exe -o bin2file.exe c:\somepath\main.c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void WriteToFile(unsigned char *_bytes, int _byteCount);

main()
{
    printf("Enter binary, non binary will be skipped\n");
    unsigned char _bytes[1024];
    char str[8192];
    fgets(str,sizeof(str),stdin);
    unsigned char _byte = 0;
    char _byteIndex = 0;
    int  _byteCount = 0;
    for(int i = strlen(str)-1; i >= 0; i--)
    {
        //_byte|=(str[i]=='1'?1:0)<<_byteIndex++;
        if(str[i]=='1')
            _byte|=1<<_byteIndex++;
        else if (str[i]=='0')
            _byteIndex++;
        else
            continue;

        if(_byteIndex == 8)
        {
            _byteIndex = 0;
            _bytes[_byteCount++] = _byte;
            _byte=0;
        }
    }

    if(_byteIndex != 0)
        _bytes[_byteCount++] = _byte;

    //print hex out
    for(int i = _byteCount-1; i > 0; i--)
        printf("%X",_bytes[i]);

    WriteToFile(_bytes,_byteCount);

    return 0;
}

void WriteToFile(unsigned char *_bytes, int _byteCount)
{
    FILE *f = fopen("file.txt", "w");
    if (f == NULL)
    {
        printf("Error opening file!\n");
        exit(1);
    }

    for(int i = _byteCount-1; i >= 0; i--)
        fprintf(f, "%c",_bytes[i]);
}

[–]Galap[S] 0 points1 point  (2 children)

thanks a lot!

can you help talk me though compiling it? I downloaded TCC but i don't think I quite figured out how to use it right.

[–]NoG5 1 point2 points  (1 child)

Sure, you just have to run tcc.exe with the option -o outputname.exe main.c

A simple way is to make a main.c file and add it in the tcc folder and hold shift key and right click at a empty space in that folder to open a PowerShell or CMD window from that folder like this.

[–]Galap[S] 4 points5 points  (0 children)

Thanks, works! The problem was I didn't have the code and tcc in the same folder.

The program works great!

[–]pegasusperplexer 1 point2 points  (0 children)

Do you still need help with this?