Thread: 5 Byte Integer

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    6

    5 Byte Integer

    I have a requirement from a customer to create a binary data file, with the first 5 Bytes (or 40 bits) representing the microseconds of the day. Since there are 86400 seconds per day, the max microseconds of the day is 86400000000 and the customer wants this number represented in 5 bytes. How can I store this number into a 5 byte integer? Longs only go up to 4 bytes, or 32 bits. I could create an array of 5 unsigned chars, but how to store the number?

    The number is read as a double, and would look something like 742039.302502 and I would somehow get it into a 5 byte integer representation of 742039302502

    Any ideas?

    thanks,

    NewPappa

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Depending on your compiler, you should have a 64 bit integer. Either:
    long long int num = 86400000000;

    Or:
    __int64 num = 86400000000;

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Which OS/Compiler?
    Do you have say "long long" or "int64_t" as types?

    > The number is read as a double
    Read from where?

    > Since there are 86400 seconds per day
    This is only mostly true. Sometimes there are leap seconds.
    http://www.boulder.nist.gov/timefreq/general/leaps.htm
    Somebody that keen to store time down to microseconds probably wants to know that.
    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.

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    6
    1) Microsoft Visual C/C++... on Windows platform.

    2) The microseconds is read from a binary file that has data that has been recorded already. The customer wants the data in this binary file processed and outputted to a binary file with the 5 byte microseconds time tag at the beginning of each write to file.

    Any ideas on how to solve this problem?

    thanks.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you're just reading it from one file to write it out to another, then just use an array of 5 bytes.

    Code:
    char t[5];
    fread( t, 1, sizeof(t), fp );
    // blah code
    fwrite( t, 1, sizeof(t), out );
    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.

  6. #6
    Registered User
    Join Date
    Jul 2004
    Posts
    6
    it's stored in the first file as a double, then must be converted and outputted to the 2nd file in it's 5 byte integer representation.

  7. #7
    Registered User
    Join Date
    Jul 2003
    Posts
    59
    Maybe this will work

    Code:
    char *p;
    char buf[32];
    double dbl;
    
    fread( dbl, 1, sizeof(double), fp );
    
    sprintf(buf, "%f", dbl);
    
    if((p= strchr(buf, '.'))!=NULL)
        memcpy(p, p + 1, strlen(p) + 1);
    	 
    for(p = buf + strlen(buf); *p == '0'; p--)
       *p='\0';
    
    fwrite( buf, 1, strlen(buf), out );
    Last edited by erikj; 07-22-2004 at 10:22 AM.

  8. #8
    Registered User
    Join Date
    Jul 2004
    Posts
    6
    Quote Originally Posted by erikj
    Maybe this will work

    Code:
    char *p;
    char buf[32];
    double dbl;
    
    fread( dbl, 1, sizeof(double), fp );
    
    sprintf(buf, "%f\0", dbl);
    
    if((p= strchr(buf, '.'))!=NULL)
        memcpy(p, p + 1, strlen(p) + 1);
    	 
    for(p = buf + strlen(buf); *p == '0'; p--)
       *p='\0';
    
    fwrite( buf, 1, strlen(buf), out );
    That just stores it as an ascii value, I need to store it in it's integer representation. For example a value of 29 would look like a 0x1D in a binary file. Now how to represent a large number like 81556123456, which needs more than 32 bits of precision??

    Thanks.

  9. #9
    Registered User
    Join Date
    Jul 2004
    Posts
    6
    I finally came up with a solution. It required using the __int64 data type. I just didn't know about this data type. Having that data type at my disposal made the solution a piece of cake.

    thanks to all that helped.

    NewPappa

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Except that is not 40 bits, it's 64. Thus, if you write it to a file, you'll be writing a binary 64 bits, instead of 40, which when you read it again, will screw it all up.

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

  11. #11
    Registered User
    Join Date
    Jul 2004
    Posts
    6
    >> Except that is not 40 bits, it's 64. Thus, if you write it to a file, you'll be writing a binary 64 bits, instead of 40, which when you read it again, will screw it all up.

    But I can create a union with an 8 element unsigned char array, and use the 5 bytes of significance.

    Code:
    union mixed {
      unsigned char byteArray[8];
      __int64 iPortion64;
    };
    
    union mixed microSeconds;
    something like that.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Then you have to figure out whether your machine is little-endian or big-endian. The MSB of your int is in either array[0] or array[7]

    Which is perhaps OK if you only ever run this on one type of machine.

    Probably doesn't matter, your file format is horribly machine specific to start with from the sound of it.

    If you want to be sure of getting the bytes in the ouput file in the right order, then you need to extract info from the int a byte at a time
    Code:
    array[0] = myint & 0xff;
    array[1] = (myint>>8) & 0xff;
    array[2] = (myint>>16) & 0xff;
    //etc
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  4. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  5. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM