Thread: binary transfer

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    6

    binary transfer

    my question is a bit in depth, so pardon me if i repeat redundant information before actually getting to my question...

    first off all data is stored in binary on any medium(or atleast is read that way by a computer... ie 01010010) as bits and bytes. how would one go about storing said byte into an array? for example first byte of a file is 01001101 and i want the first listing in my array as 01010010

  2. #2
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    In C you don't deal with the raw binary itself (although you can develop techniques for doing so by combining the bit shift operators (>> and <<) and the bitwise operators '&' etc). If you want to deal with individual bytes of info as you have said in your question, then you would probably use the 'unsigned char' type. That is the size of a single byte.

    Edit: As a general rule, when you program you don't need to deal with individual bits, although there is still support for these kinds of operations.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    How about an array of unsigned char? C "usually" uses 8 bits for char and unsigned char types. Or if you are using a C99-compliant compiler, you can #include <stdint.h> and make an array of type uint8_t. As you said, every number will be stored as binary anyway.

    For example, 01010010 is 82 decimal, so

    Code:
    #include <stdio.h>
    
    int main() {
        int i;
        unsigned char ch, temp, arry1[10];
        arry1[0] = 82;
        temp = arry1[0];
        for( i = 0; i < 8; i++ ) {
            if( temp & 128 ) {
                ch = '1';
            } else ch = '0';
            printf( "%c\n", ch );
            temp <<= 1;
        }
        return 0;
    }
    outputs 01010010.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    6
    i guess i need to get a bit more technical about what i want to do....what i want to do is take the actual binary number stored(I guess it doesnt matter if the number is in hex, dec, or bin) and convert it into a different number. what would be the simplest way to go about doing that? read it as dec and then convert or am i missing something here?

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by alysher View Post
    i guess i need to get a bit more technical about what i want to do....what i want to do is take the actual binary number stored(I guess it doesnt matter if the number is in hex, dec, or bin) and convert it into a different number. what would be the simplest way to go about doing that? read it as dec and then convert or am i missing something here?
    All the numbers in your computer are "actual binary numbers"; letters also.
    Those (decimal, hex, binary, octal, etc.), are just different ways of interpreting the binary data.

    You need to get your emphasis *far* away from this "decimal" and "binary" stuff. Start with designing the program algorithmically, and let the details get put off, for awhile.

    Show an example of the input your program will deal with, and what output you want your program to produce, and what problem you have with making that happen.

    THEN we can start talking turkey!

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by alysher View Post
    i guess i need to get a bit more technical about what i want to do....what i want to do is take the actual binary number stored(I guess it doesnt matter if the number is in hex, dec, or bin) and convert it into a different number. what would be the simplest way to go about doing that? read it as dec and then convert or am i missing something here?
    Ok... post here....
    1) What do you want to put into the program as file/port/user input?
    2) From that input what do you expect to get out?

    Forget this binary crap for a while... lets just get a clear statement of what you are trying to do.
    "Convert it into a different number" doesn't really tell us much.

    For example: Do you want to type in a binary number and get a decimal output?

    We need to know exactly what you're trying to accomplish.

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    6
    the input is going to be a file, any kind of file for that matter

    from here i want to use my conversion algorithm to change said file, the algorithm puts the output into an array or list(im not sure which will be better at this point)

    i then want to write this list or array to its own file(the data of the list or array will be in binary).

    after this process is through i want to reverse it and see if i get the original file back as output

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    It would be preferable to get the file size first and then create an array of that size. Don't use a list.

    Next are you looking at making a basic obfuscator? Perhaps what you want is a 256 byte substitution table. Every value is uniquely mapped to a different value. Then to decode you calculate the reverse substitution table.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by alysher View Post
    the input is going to be a file, any kind of file for that matter

    from here i want to use my conversion algorithm to change said file, the algorithm puts the output into an array or list(im not sure which will be better at this point)

    i then want to write this list or array to its own file(the data of the list or array will be in binary).

    after this process is through i want to reverse it and see if i get the original file back as output
    Ok, I give up...

  10. #10
    Registered User
    Join Date
    Oct 2010
    Posts
    6
    Quote Originally Posted by iMalc View Post
    It would be preferable to get the file size first and then create an array of that size. Don't use a list.
    i had originally planed on an array, but this solved my issue of max file size. Thank you.

    Quote Originally Posted by iMalc View Post
    Next are you looking at making a basic obfuscator?
    Sort of.....

    Quote Originally Posted by iMalc View Post
    Perhaps what you want is a 256 byte substitution table. Every value is uniquely mapped to a different value. Then to decode you calculate the reverse substitution table.
    this is precisely what my algorithm will use for coding and decoding.

    my issue is getting the file into the array for substitution, then outputting EXACTLY what the new file will be...

  11. #11
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    If you're running under Windows, open the output file with something like outp = fopen("output_file_name", "wb"). If you're using a real OS instead, just use "w".

    Use fgetc() to read a character and fputc() to write one.

    Are you reversing the order of the characters in the file or the order of the bits in a single character? Or both?

    If you're just reversing the bits in each character, you only have to work with one character at a time. Read it, reverse the bits, write it out. Move on to the next one. No need to store them all at once.

    If you need to read the whole file because you're reversing the order of the characters, look into using either stat() or fseek(...SEEK_END...) with ftell(). That will give you the length of the array you have to allocate to hold the file. In this case, it might make sense to use fread/fwrite to read/write the whole file in one shot rather than character by character, but I doubt it will matter that much speed wise.

    You'll need to post code which shows the specific problem you're working on before we can give more detailed help than this.

  12. #12
    Registered User
    Join Date
    Oct 2010
    Posts
    6
    to answer the last question first, none of this code has been writen yet because im having trouble with my file input and output process. The only way i knew how to use a file was with ifstream() and ofstream(), which doesnt do me any good because it only reads and writes in ASCII.

    after looking up fgetc() and fputc() did i realized that my teachers conveniently FORGOT to tell me that if i wanted to read and write in binary mode i needed to put a b at the end of my mode string.

    My algorithm is using 4bytes of data for input, but is only going to output 3bytes of data, so i believe its going to be MUCH faster for me if i use fread() and fwrite() rather then go through the process of doing a loop to read my data and a loop to write it.

    out of curiosity would i be better off if i read and write more at all at once? (read 400bytes/write 400bytes rather then read 4bytes/write 4bytes)

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > out of curiosity would i be better off if i read and write more at all at once?
    Maybe, but the mere mention of it screams "premature optimisation".
    You NEED to make sure it works before starting to worry about making it quick.

    It really doesn't matter how damn quick it is, if it doesn't work or you haven't delivered it on time.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    Registered User
    Join Date
    Oct 2010
    Posts
    6
    that is totally understandable, when i get that far ill see what i can do.

    Thank you all for the input and help, and my apologies to those of you who were confused about what i was asking for.

  15. #15
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    No problem about the confusion - we're asking because we want to help. Or ridicule, depending on the mood. But in this case it's help

    Although it's veering off topic to mention it in the C forum, fstream can take ios::binary flag as the second argument to the constructor/open call. There's read()/write() methods which will operate similarly to fread/fwrite. If it's schoolwork it may make sense to see if you're supposed to write C or C++ code ... ignoring the fact that C++ code can use fread/fwrite sometimes people want you to use the ifstream/ofstream stuff exclusively if you're doing C++ homework. If you're doing this for fun, try both ways...

    Note that the "b" is only needed for Windows systems, and it only makes a difference when reading end-of-line / end-of-file characters. Otherwise data is data - binary, text, some random picture you downloaded off the internet, whatever. Binary mode in the open call really means "don't translate a few unique control character sequences to handle strange CP/M backwards compatibility". It doesn't change anything else.

    I only emphasize this point because there's no difference between text data and binary data on a disk. Read a 't' from a text file and it's a 't' when you print it out as a character but 117 (ASCII value for 't') if you print it as in integer. Read 117 from a binary file, print it as a character and it's a 't'. No magic needed to change from text data to binary data - it's just a question of what you do with it in your program.

    If you want to experiment with fread()/fwrite(), you'll probably want to read in a multiple of 1K or 4K bytes since that generally matches the buffer sizes used by an OS. But it's certainly a premature optimization. Then again, even if it doesn't speed things up it would be a good learning opportunity.

    Good luck, keep us up to date.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. converting a string into binary for data transfer
    By Bopamo in forum C++ Programming
    Replies: 2
    Last Post: 10-15-2010, 01:39 PM
  2. transfer binary over socket
    By MK27 in forum Networking/Device Communication
    Replies: 30
    Last Post: 10-09-2008, 09:41 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM