saving a double to a file as 4 characters?

This is a discussion on saving a double to a file as 4 characters? within the C++ Programming forums, part of the General Programming Boards category; ok i have a script for an interpreted language that i use to change large numbers into only 4 bytes ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    137

    saving a double to a file as 4 characters?

    ok i have a script for an interpreted language that i use to change large numbers into only 4 bytes inside a file. this is the script:

    PHP Code:
    {
        var 
    valuebytestackMASKinumBytes;
        
        
    MASK 255;
        
        
    value    argument0;
        
    stack    argument1;
        
    numBytes argument2;
        
        
    //Push the low-order byte on the stack, moving other bytes lower.
        
    for (0numBytes+= 1)
        {
            
    byte MASK value;
        
            
    ds_stack_push(stackbyte);
        
            
    value value >> 8;
        }

    the problem is the ds_stack, i don't know how i can mock that in c++. I know c++ you can make stacks, but i'm just wondering is there a way to get around it. i don't know how to use a c++ stack very well yet so... any help would be appreciated.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    well, the sizeof(double) probably will not be 4 bytes -- sizeof(double) on 32-bit machine is 8, not 4.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    You can use fwrite() (or whatever the C++ equivalent is) to write a variable to a binary file.

    [edit]
    And what's var? Is it a typedef or something?
    [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    the script was for a different language. var specifies a variable that will go out of scope when the function ends.

    ancient dragon, i don't see what you are saying. i don't think sizeof() does what i want.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    He's just saying that you can't store an 8 byte variable in 4 bytes without losing some of its precision. A float would fit into 4 bytes.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    errr. ok i'm dumb. i meant float not double. thank you for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  2. Double print
    By thestrap in forum C Programming
    Replies: 1
    Last Post: 12-05-2007, 06:17 PM
  3. Conversion From C++ To C
    By dicon in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 02:54 PM
  4. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 08:13 AM
  5. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 05:54 PM

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