简体   繁体   中英

divide uint64_t data containing N decimal digits into two uint32_t and trim the output in C

I need to divide two parts into high 8 decimal digits , low 8 decimal digit I will not write all the details of the code. (eg memory allocation etc) focusing for N decimal digits as input with data type uint64_t* and divide into two 8 decimal digits for first 8 digits, next 8 digits (data type uint32_t), so if there are more digitis, it will be just trimmed. (Please understand I am a newbie. so if there are wrong in the code, appreciate explanation, only criticism without explanation will not help the newbies)

The code expected to work like this: Assumption: uint8_t* data_64b as input contains already N decimal digits.

eg1)

  • 1234567803456789 as input (16 decimal digits) in uint64_t* data_64bits
  • Then , the output will be 12345678 , or 03456789 based on the id.

eg2)

  • the input data has 17 decimal digits: 98765432155555553
  • Then I assume that the last 1 digit '3' should be thrown out.
  • And output will be each 8 digits either 98765432 or 15555555 based on id

The code:

// @Input: unit8_t id                       input identifier 
// @input: uint64_t* data_64bits   input parameter , assume it contains N decimal digits number
// @output: uint32_t* data_32bits  output parameter to return the divided each 8 decimal digits 
dataFrom64To32bits( uint8_t id, uint64_t* data_64bits, uint32_t* data_32bits )
{  
    uint8_t* data_64bits;    /* 64bits */

#if 0 
/* [A] My original thoughts, but I have told this is not necessary. 
because the data contains decimal digits
[Q1] really don't need this? */

    uint32_t 4BytesLSB;
    uint32_t 4BytesMSB;

    4BytesLSB = data_64bits & 0xFFFFFFFF;                 /* obtains the 4 LSB */
    4BytesMSB = (data_64bits & 0xFFFFFFFF00000000) >> 32; /* obtains the 4 MSB */
#endif

#if 1
    /* [B] I have got advice like this, 
and have told that it will be enough doing like this 
for 8 decimal digits for high part, low part to divide.
[Q2] Does this really make sense? and will give the result? */
    if(id == id_highpart)
    {
        *data_32bits = ( *data_64bits / 100000000 ) % 100000000;
    }
    else if(id == id_lowpart)
    {
        *data_32bits = *data_64bits % 100000000;
    }
#endif
}

Dose it make sense?

A part told that is not necessary. B part adviced for the expected result Please look at [Q1], [Q2] in the code.

Additional, which is the right for throwing out the rest from the certain part in the code? trim or something else? Thanks in advance

Additional, which is the right for throwing out the rest from the certain part in the code? trim or something else?

To discard digits after the 16th, divide by 10 until there are no more (before extracting the parts):

    while (*data_64bits > 9999999999999999ll) *data_64bits /= 10;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM