Thread: Binary Output

  1. #1
    Registered User Ajsan's Avatar
    Join Date
    Dec 2003
    Posts
    55

    Binary Output

    Does anyone know how to output in binary without me having to write a converter myself, it needs to convert text / numbers / ... yeah thats about it.

    Any Help would be apreciated...!
    Style is overrated.

    - —₽‚¢‰Î -

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Post an example of what you want to write to disk in binary mode.

    One solution is ofstream's write().

    Kuphryn

  3. #3
    Registered User Ajsan's Avatar
    Join Date
    Dec 2003
    Posts
    55
    All of the things that i want to output are stored in variables but for some examples



    Suan //first name
    Seith //Last name
    12 //Strength
    13 //...
    15 //...
    13 //...
    10 //...
    67 //hp
    85 //maxhp
    12 //mp
    12 //maxmp
    Style is overrated.

    - —₽‚¢‰Î -

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    why do you need to output it as binary?
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    Registered User Ajsan's Avatar
    Join Date
    Dec 2003
    Posts
    55
    I'd just like it so that no one can change their savefile without atleast going through some trouble of converting into readable text and even then some people are to dumb to evern think of converting the binary to normal text.
    Style is overrated.

    - —₽‚¢‰Î -

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    when I want to do that, I usually just shift the characters (using the ASCII table) by a random number of positions and put that number somewhere in the file, for example:
    Code:
    int RANDNUM=1+rand()%100;  //any number between 1 and 100
    
    ofile<<"N"<<RANDNUM<<":"<<flush;  //output the number at the beginning of the file
    
    for(...)  //loop through all characters (including spaces, newlines, numbers, etc.)
        ofile<<static_cast<char>(static_cast<int>(THISCHAR+RANDNUM));  //shift and write the chars
    now the file will look something like this (assuming the random number was 56)
    Code:
    N56:¸Ïj¦ô°X·Ìx#‚:T;t4I™÷6æÜäg¯j½Ék”ƒa8|ž]I~DSCûÀSÚµ¦“t“}¬{¡\{0.H8¨¾€Ý˜õ<tÏQM+×c
    now when you read in teh file, discard the N, read in the number (56 in this case), discard the colon (, and start reading in the characters, decrementing each by the number you just read in... This should in now way be considered 'secure', but it's just something to keep the average user from fking around with the file... although it probably won't do much... another thing you could do is save it as .ico or something, so people might think it's an icon and not touch it... but I wouldn't go that far...

    one thing I would do though, is supply a way to edit the savefile, but keep it somewhat secure... maybe through a password or something, but sometimes it becomes necessary to backup/restore/edit a file...
    Last edited by major_small; 04-22-2004 at 08:56 PM. Reason: added more
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  7. #7
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    You could use the bitset stl class. Its contructor accepts a long. There is the << operator overloaded for stream, but I do not know how the output should like.
    Nothing more to tell about me...
    Happy day =)

  8. #8
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Lightbulb <bitset> example

    Here's an example that converts between various bases. It uses bitset to display in binary.

    Note: I had to change the name to base.txt, from base.cpp... Apparently the new board software won't allow a cpp file.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    ... or you could read the FAQ...
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Exclamation Binary doesn't always mean base-2.

    I'd just like it so that no one can change their savefile without atleast going through some trouble...
    Oh! ... You don't want to use bitset then.

    This is a little confusing...
    The "binary" file format isn't really binary. When you save a file in binary format, the bytes are written directly to the file without any re-formatting. ...Well everything in the computer is binary, but if you open a binary file with a text editor, you will NOT see a bunch of ones and zeros.

    The text format is a special feature. When you save a file as text, all it does is add a carrage-return and/or linefeed (depending on how our system stores text) to the end of each line.

    So, if you save a null-terminated c-style string, the zero at the end of the string will be replaced by a carrage-return/linefeed. If you save it as "binary" the ASCII characters will be saved as normal ASCII characters, and the null-terminating zero will be written to the file. If you then open this "binary" file with a text editor, you will see the text, but new lines won't start on one line down.

    The reverse is true when you open a file as text. The compiler throws-out the carrage return/linefeed, and replaces it with a zero (in the case of a c-style string), or formats it as a C++ string.

    So, try what major_small suggested, or look into XOR (exclusve or) encryption.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting 32 bit binary IP to decimal IP (vice-versa)
    By Mankthetank19 in forum C Programming
    Replies: 15
    Last Post: 12-28-2009, 07:17 PM
  2. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  3. why this output?
    By dredre in forum C Programming
    Replies: 4
    Last Post: 05-08-2004, 04:09 PM
  4. binary
    By webturtle0 in forum A Brief History of Cprogramming.com
    Replies: 52
    Last Post: 12-05-2002, 02:46 AM
  5. Array, Linked List, or Binary Tree?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 01-05-2002, 10:07 PM