writing binary, help please!

This is a discussion on writing binary, help please! within the C Programming forums, part of the General Programming Boards category; Hello all, first time poster and C newbie.. I'm trying to convert my simple script, to a C plugin... Code: ...

  1. #1
    CNewbie
    Join Date
    Nov 2005
    Location
    Oz
    Posts
    31

    writing binary, help please!

    Hello all, first time poster and C newbie..
    I'm trying to convert my simple script, to a C plugin...
    Code:
    output = File("test.obj","wb");
    output.writeData(test);
    output.close();
    
    @data test 
    070 079 082 077 000 000 001 136 076 087 079 050 084 065 071 
    000 000 000 016 082 101 100 000 086 111 105 100 000 000 086 
    105 100 000 000 076 065 089 082 000 000 000 018 000 000 000
    @end
    And here is my lame attempt in C.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    void main()
    {
    FILE *myfile;
    myfile = fopen("test.obj","wb");
    if(!myfile)
    {
    puts("Some kind of file error!");
    exit(0);
    }
    fprintf(myfile,"070 079 082 077 000 000 001 136 076 087 079 050 084 065 071 
    000 000 000 016 082 101 100 000 086 111 105 100 000 000 086 
    105 100 000 000 076 065 089 082 000 000 000 018 000 000 000");
    fclose(myfile);
    }
    The problem with this code, is it writes the 1byte decimal numbers literally, instead of as binary, despite the WB flag.

    I'm guessing i might have to use sizeof or count bytes,
    but i'm only guessing... (scripting is so much easier!)

    Any and all help would be appreciated...... Especially a working example, as iv'e posted a similar question elsewhere a few weeks back, but all of the suggestions did not work as the script does.

    I can't read music, but i can play music by ear...

    Cheers,
    Scott

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're writing a string, what did you expect? That was rhetorical. "070" is at best an octal number. However, what you've really got is a string of characters. You opening in "binary mode", which incidently, is exactly the same as "text mode" on some operating systems, doesn't mean it automagicly converts all of your strings into some combobulated "binary" mystery.

    If you want a number, write a number. If you want a string, write a string. In other words, stick all those little numbers into an integer or what not, and write that.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Before outputting those numbers to a file you need to be sure exacly what they are. Are they decimal representations or octal?
    you could place them into an array like this
    Code:
    unsigned short []=
    {
      070, 079, 082, 077, 000, 000, 001, 136, 076, 087, 079, 050, 084, 065, 071, 
      000, 000, 000, 016, 082, 101, 100, 000, 086, 111, 105, 100, 000, 000, 086, 
      105, 100, 000, 000, 076, 065, 089, 082, 000, 000, 000, 018, 000, 000, 000
    };
    That would treat each as a 2 byte value giving you 90 bytes.

  4. #4
    CNewbie
    Join Date
    Nov 2005
    Location
    Oz
    Posts
    31
    Like i said, i'm a newbie so almost nothing you said makes sense..

    I also thought the numbers are octal, but i have a viewer that shows 1byte decimal as correct, and i'm adding an extra 0.
    but i don't care what it is, i just want it to work like my script.

    I tried using ASCII, instead by opening the object in notepad,
    and exchanging the copy and pasted code...
    That was weirder, because it worked for the first row of numbers,
    but the 2nd row, has automagically changed...

    Anyway, if anyone else has similar code, or code examples that do similar things please let me know..

    As i said i'm a newbie, i'm on page 40 of the DevC manual,
    and i have not even looked at strings or function pointers..
    I'm just looking at some fwrite stuff, because i will use it a lot.

    I don't care if it's a string, or number, letter or porkypine...

    And honestly, i'm already so bored from reading up on fwrite, fopen, fprint, that i'll be long asleep before finding helpful answers.

    Just the easiest way to translate my script into a compiling C code. would be a helpful start, at least maybe than i could
    look back and compare the script to the C example, and work
    out what the hell you mean...

    As i said, some people learn from reading books about music...
    I learn from listening to music, written by people who read books.

    EDIT:
    Quantum1024

    Thanks Quantom 1024 that's a little more helpful...
    I will have a play, with that array and see if i can somehow use it.
    And let you know, thankyou....
    Last edited by Freestyler; 11-23-2005 at 06:48 PM.

  5. #5
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Given that the numbers in the OP contain the digits 8 and 9, we can probably safely guess that they're decimal.

    Freestyler,

    In C (and probably also in Python, or whatever the original language was that you posted), if a numeric constant in a program starts with a 0, it is treated as an octal value. If it starts with 0x, it is treated as a hexadecimal value, otherwise it is treated as decimal.

  6. #6
    CNewbie
    Join Date
    Nov 2005
    Location
    Oz
    Posts
    31
    Thanks for the clarification, cwr.. Yes i thought it was.

    The original script docs, refrained from mentioning what format it was, i used a viewer that found 1byte decimal (it looked similar to octal) but octal did not work, 1byte decimal did, if i padded it with an extra 0.... So thanks for the reasoning behind it..

    I'd been looking at some examples of the net,
    i thought i'd have to read the amount of bytes or something..
    and then write something like.

    Code:
    fwrite("0x%02x,",bytes[i] & 0xff);
    But that's just guessing at the moment.....

    I will try and use an array like Quantum1024 mentioned.
    (just from his snippett, i learnt arrays are wrapped in their
    own {} kind of like their own function?

    For some reason, endless reading about C, makes me dizzy...
    But simple code examples, make me C.....
    Something like a picture tells a thousand words...

    And for someone with no attention span, this make me absorb far more... So thanks to everyone so far.

    I mention this, just because these forums in particular
    seems harsh on examples or code completion, or helping...
    (just reading some of the other threads) I know students and homework, can be a problem..... But please remember, us dumber minorties, who learn music by ear, and not by reading black lines on white pages.

    If it was not for such places like Sourceforge etc,
    i simply would not even be interested, in attempting C...

    Sorry, Babbling..... Thanks again...

  7. #7
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    I'm trying to convert my simple script, to a C plugin...
    First of all, I'm making an assumption that this is a Win32 plugin. Now, have you taken into consideration that the plugin executable code (or any type of Win32 executable or dll) must be wrapped by a PE (Portable Executable) structure which is used by the OS loader to manage the executable code? The PE format manages everything from API export/import tables to resource management to linking references.

    If you haven't considered the PE format in your binary conversion, then you're really wasting your time on this conversion.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Quote Originally Posted by cwr
    Given that the numbers in the OP contain the digits 8 and 9, we can probably safely guess that they're decimal.
    makes sense.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    unsigned char Data[]=
    {
      70, 79, 82, 77, 0, 0, 1, 136, 76, 87, 79, 50, 84, 65, 71, 
      0, 0, 0, 16, 82, 101, 100, 0, 86, 111, 105, 100, 0, 0, 86, 
      105, 100, 0, 0, 76, 65, 89, 82, 0, 0, 0, 18, 0, 0, 0
    };
    
    void main()
    {
    FILE *myfile;
    myfile = fopen("test.obj","wb");
    if(!myfile)
    {
    puts("Some kind of file error!");
    exit(0);
    }
    fwrite(Data, 1, 45,  myfile); 
    
    fclose(myfile);
    }

  9. #9
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,807
    Quote Originally Posted by Quantum1024
    makes sense.
    Code:
    void main()
    Woop?

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    lol I copied that from above without looking

  11. #11
    CNewbie
    Join Date
    Nov 2005
    Location
    Oz
    Posts
    31
    Quantum1024.

    Thanks again for your help, but that does not work as i need.
    That seems to add a zero and writes literally the numbers..
    (BTW what does the 1,45 do?)

    This is what the file the program should look like in notepad (ascii)

    Code:
    FORM  LWO2TAGS   Red Void  Void  LAYR                     PNTS               BBOX                           VMPA          VMAP   WGHT Void.Weight   ?  POLS   FACE   PTAG   COLR    PTAG   PART   PTAG   SURF   SURF   Void    COLR ?H?H?H  LUMI       DIFF       SPEC       TRAN       RIND       BUMP       GLOS       VCOL ?    RGBAVoid.Vertex SMAN    RFOP   TROP 
    And this is what it looks like in 1byte decimal
    Code:
    070 079 082 077 000 000 001 136 076 087 079 050 084 065 071 083
    000 000 000 016 082 101 100 000 086 111 105 100 000 000 086 111
    105 100 000 000 076 065 089 082 000 000 000 018 000 000 000 000
    000 000 000 000 000 000 000 000 000 000 000 000 000 000 080 078
    084 083 000 000 000 012 000 000 000 000 000 000 000 000 000 000
    000 000 066 066 079 088 000 000 000 024 000 000 000 000 000 000
    000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
    000 000 086 077 080 065 000 000 000 008 000 000 000 000 000 000
    000 006 086 077 065 080 000 000 000 024 087 071 072 084 000 001
    086 111 105 100 046 087 101 105 103 104 116 000 000 000 063 128
    000 000 080 079 076 083 000 000 000 008 070 065 067 069 000 001
    000 000 080 084 065 071 000 000 000 008 067 079 076 082 000 000
    000 000 080 084 065 071 000 000 000 008 080 065 082 084 000 000
    000 001 080 084 065 071 000 000 000 008 083 085 082 070 000 000
    000 002 083 085 082 070 000 000 000 166 086 111 105 100 000 000
    000 000 067 079 076 082 000 014 063 072 200 201 063 072 200 201
    063 072 200 201 000 000 076 085 077 073 000 006 000 000 000 000
    000 000 068 073 070 070 000 006 000 000 000 000 000 000 083 080
    069 067 000 006 000 000 000 000 000 000 084 082 065 078 000 006
    000 000 000 000 000 000 082 073 078 068 000 006 000 000 000 000
    000 000 066 085 077 080 000 006 000 000 000 000 000 000 071 076
    079 083 000 006 000 000 000 000 000 000 086 067 079 076 000 022
    063 128 000 000 000 000 082 071 066 065 086 111 105 100 046 086
    101 114 116 101 120 000 083 077 065 078 000 004 128 000 000 000
    082 070 079 080 000 002 000 000 084 082 079 080 000 002 000 000
    So i'd like to store it in the array, as 1byte decimal...
    and then output a file..... that looks like the ascii in the first example... (in notepad) (i assume)

    I tried just using the ascii to copy and paste.... (last week)
    but the numbers started to change.... (somehow)
    example the first row was identical, but the 2nd started
    to change to 030, 058, 067, when i checked it's binary...

    I have a hard time explaining it...
    But thanks again for the help.

  12. #12
    CNewbie
    Join Date
    Nov 2005
    Location
    Oz
    Posts
    31
    Quote Originally Posted by BobS0327
    First of all, I'm making an assumption that this is a Win32 plugin. Now, have you taken into consideration that the plugin executable code (or any type of Win32 executable or dll) must be wrapped by a PE (Portable Executable) structure which is used by the OS loader to manage the executable code? The PE format manages everything from API export/import tables to resource management to linking references.

    If you haven't considered the PE format in your binary conversion, then you're really wasting your time on this conversion.
    Wow, is this going to be needed just to do what i do in the script?
    I mean the script is so simple, i'm amazed it's this difficult to achieve in C....

    I will eventually, be making a .dll, for a 3D program, written in C.
    I know more about the 3D SDK, than i do of C.
    But at the moment i'd settle for a simple fprint command line.exe
    I'm using DevC/C++ and as of today, CodeBlocks1....

    The scripting language is basically based on C, so i can't see it being a problem, although this is weeks of various answers, with still no working example.... I'll keep plodding along... Thanks though.

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Quote Originally Posted by Freestyler
    Quantum1024.

    (BTW what does the 1,45 do?)
    1 is the size (1 byte) and 45 is the number of bytes.
    it can also be written like this to save counting the bytes yourself
    Code:
    fwrite(Data, 1, sizeof(Data),  myfile);
    Where was a zero being added? I am fairtly sure that this will do what you want, you'll have to remove the zero from all your numbers before putting them into the array otherwise they will be intpreted as octal and and the output will change.
    Last edited by Quantum1024; 11-23-2005 at 09:03 PM.

  14. #14
    CNewbie
    Join Date
    Nov 2005
    Location
    Oz
    Posts
    31
    Excellent Quantum1024.... You are totally correct.

    I don't know what i did before... I must of typed an earlier.exe
    that just copied the literal string, by mistake..

    But i tried it again, and it does look correct in notepad...
    So again, i very much appreciate the help...

    You have offically been lifted to "Legend" status...

    It's weird i'd store it differently, than the script...

    ie, 70, instead of 070...

    I now understand why, although i wonder why the scripting langauge chose to add the 0, as it caused me so much extra effort initially to put those in the script to begin with....

    But thanks again, and to everyone really is appreciated...

    Because i know it does actually work now, that gives great incentive to actually go and read up a bit on why.... I always work backwards for some reason... So thanks... Woohoo..

  15. #15
    CNewbie
    Join Date
    Nov 2005
    Location
    Oz
    Posts
    31
    EDIT -DOH, worked it out.
    Last edited by Freestyler; 11-24-2005 at 12:13 AM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing std::string in binary mode
    By VirtualAce in forum C++ Programming
    Replies: 16
    Last Post: 11-06-2010, 07:39 PM
  2. Writing a struct to a binary file
    By Scarvenger in forum C++ Programming
    Replies: 8
    Last Post: 09-12-2006, 01:50 AM
  3. Writing binary data to a file (bits).
    By OOPboredom in forum C Programming
    Replies: 2
    Last Post: 04-05-2004, 03:53 PM
  4. binary search and search using binary tree
    By Micko in forum C++ Programming
    Replies: 9
    Last Post: 03-18-2004, 09:18 AM
  5. binary to decimal
    By miryellis in forum C Programming
    Replies: 7
    Last Post: 03-14-2004, 07:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21