views:

169

answers:

2

Hi i have used the following code for converting the bigint in decimal to bytearray (raw data) , but i am getting wrong result What is the mistake here.

I am 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

A: 

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

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

num should be cast to uint8_t before copy

data[k--] = (uint8_t)num; 
Andy
Somehow i am getting the first 4 bytes are correct and the end 4 bytes are correct even i cast to (int8_t)num Thanks
First few bytes and some end bytes of the correct data are -93 -109 106 -102 101 -90 -5 -2 119 45 -105 ......69 96 -110 -106 -63 -83 100 -50 14 The Wrong data are 35 -109 106 102 50 -45 125 -1 29 -53 101 -33.....21 65 37 45 -125 45 100 -50 14 comparing both only few bytes in start and end are same others are wrong , I have tried Andy suggestion above also but the result remains same , thanks
when i tried converting for simple decimal value "123456789" in java i am getting the byte string is as below The Data in byte:: 7The Data in byte:: 91The Data in byte:: -51The Data in byte:: 21But in mac with the above code i am getting The Data in byte:: 35The Data in byte:: 69The Data in byte:: 103The Data in byte:: -119so is that Mask is wrong , pleas advice
in Java the result is 7 , 91 ,-51 , 21in Mac with the above c code the result is 35 , 69, 103, -119what is the wrong in the code , Please help
I am not able to find what is the issue with Mac code. Did you try the code snippet which I had given above with this input.
Andy
Yes i tried with the same above c code but i am compiling and runnning in Xcode in Mac , Any difference in compiler/Mask / Endian or byte representation ?Thanks
When My test decimal Data is char *i6 = "12345678910";Java gives Correct Result { 2 ,-33 ,-36 ,28 ,62}But the above C code give wrong result {5 ,95 ,-36 ,28 ,62 } only the last 3 bytes are always correct !!!when the test data is shrink to the minimum as char *i6 = "1234567891";Both Java and C give the same correct Result { 73 ,-106 ,2 ,-45}But the above c method supposed to work for BigInteger I have tried this in ANSI C with Windows and Mac both same result , is this Logic work for biginteger (shft by 8) or any other logic available for converting toBytearr