简体   繁体   中英

converting Biginteger to Bytearray(Raw data)

I have used the following code for converting the bigint in decimal to bytearray (raw data), but I'm getting wrong result.

What is the mistake here?

I'm trying this in Apple Mac ( for Iphone app) COMP_BYTE_SIZE is 4

Is there any bigendian/ little endian issue, please Help.

void bi_export(BI_CTX *ctx, bigint *x, uint8_t *data, int size)
{
    int i, j, k = size-1;

    check(x);
    memset(data, 0, size);  /* ensure all leading 0's are cleared */

    for (i = 0; i < x->size; i++)
    {
        for (j = 0; j < COMP_BYTE_SIZE; j++)
        {
            comp mask = 0xff << (j*8);
            int num = (x->comps[i] & mask) >> (j*8);
            data[k--] = num;

            if (k < 0)
            {
                break;
            }
        }
    }

Thanks.

The argument size is at least x->size*4, ie. the target array is big enough? Also use

comp mask = (comp)0xff << (j*8);

num应该在复制之前uint8_tuint8_t

data[k--] = (uint8_t)num; 

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