简体   繁体   English

将包含N个十进制数字的uint64_t数据分成两个uint32_t并在C中修整输出

[英]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. 我需要将两个部分分成高8位小数位数,低8位小数位数,我不会写代码的所有细节。 (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. (例如内存分配等),以数据类型为uint64_t *的N个十进制数字为输入,然后将前8个数字分为两个8个十进制数字,然后是后8个数字(uint32_t数据类型),因此如果有更多的数字,则为修剪。 (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. 预期代码将像这样工作:假设: uint8_t* data_64b作为输入已包含N个十进制数字。

eg1) 例1)

  • 1234567803456789 as input (16 decimal digits) in uint64_t* data_64bits 1234567803456789作为uint64_t* data_64bits输入(16个十进制数字)
  • Then , the output will be 12345678 , or 03456789 based on the id. 然后,根据id的输出将为1234567803456789

eg2) eg2)

  • the input data has 17 decimal digits: 98765432155555553 输入数据具有17个十进制数字: 98765432155555553
  • Then I assume that the last 1 digit '3' should be thrown out. 然后,我假定应该扔掉最后一个数字“ 3”。
  • And output will be each 8 digits either 98765432 or 15555555 based on id 根据ID,输出将是每个8位数字9876543215555555

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. 建议提供预期效果的B部分请查看代码中的[Q1],[Q2]。

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): 要在16号之后舍弃数字,请除以10,直到没有数字为止(提取各部分之前):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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