感谢各位兄弟的帮忙,从上面回复的这些代码来看,我觉得很有可能是我的意思没有表达清楚,现在再表述一次。我要写的代码功能正好与下面这段BDS_unpack()代码相反,也就是原来是一个解码过程,我现在在得到文本数据后,需要把它再进行编码,也就是写一个BDS_pack()函数。
相关的宏定义:
#define PDS_DecimalScale(pds) INT2(pds[26],pds[27])
#define BMS_bitmap(bms) ((bms) == NULL ? NULL : (bms)+6)
#define BDS_NumBits(bds) ((int) bds[10])
#define BDS_RefValue(bds) (ibm2flt(bds+6))
#define BDS_BinScale(bds) INT2(bds[4],bds[5])
#define BDS_ComplexPacking(bds) ((bds[3] & 64) != 0)
#define BDS_Harmonic(bds) (bds[3] & 12

#define BDS_Harmonic_RefValue(bds) (ibm2flt(bds+11))
调用:
temp = int_power(10.0, - PDS_DecimalScale(pds));
BDS_unpack(array,bds,BMS_bitmap(bms),BDS_NumBits(bds),nxny,temp*BDS_RefValue(bds),temp*int_power(2.0,BDS_BinScale(bds)));
函数本身:
///// Beginning of "void BDS_unpack()" function /////
static unsigned int mask[] = {0,1,3,7,15,31,63,127,255};
static unsigned int map_masks[8] = {128, 64, 32, 16, 8, 4, 2, 1};
static double shift[9] = {1.0, 2.0, 4.0, 8.0, 16.0, 32.0, 64.0, 128.0, 256.0};
void BDS_unpack(float *flt,unsigned char *bds,unsigned char *bitmap, int n_bits,int n,double ref,double scale)
{unsigned char *bits;
int i,mask_idx,t_bits,c_bits,j_bits;
unsigned int j, map_mask, tbits, jmask, bbits;
double jj;
if(BDS_ComplexPacking(bds))
{fprintf(stderr,"*** Cannot decode complex packed fields n=%d***\n", n);
exit(

;
for(i=0; i<n;i++)
*flt++ = UNDEFINED;
return;
}
if(BDS_Harmonic(bds))
{bits = bds + 15;
/* fill in global mean */
*flt++ = BDS_Harmonic_RefValue(bds);
n -= 1;
}
else
bits = bds + 11;
tbits=bbits=0;
/* assume integer has 32+ bits */
if(n_bits <= 25) /// beginning of "If1(n_bits<=25) in subfunc BDS_unpack()" ///
{jmask = (1 << n_bits) - 1;
t_bits = 0;
if(bitmap)
{for(i=0;i<n;i++) /// beginning of "For1subfunc" ///
{/* check bitmap */
mask_idx = i & 7;
if(mask_idx == 0)
bbits = *bitmap++;
if((bbits & map_masks[mask_idx]) == 0)
{*flt++ = UNDEFINED;
continue;
}
while(t_bits < n_bits)
{tbits = (tbits * 256) + *bits++;
t_bits += 8;
}
t_bits -= n_bits;
j = (tbits >> t_bits) & jmask;
*flt++ = ref + scale*j;
} /// beginning of "For1subfunc" ///
}
else
{for(i=0;i<n;i++)
{while(t_bits < n_bits)
{tbits = (tbits * 256) + *bits++;
t_bits += 8;
}
t_bits -= n_bits;
flt
= (tbits >> t_bits) & jmask;
}
/* at least this vectorizes
*/
for(i=0;i<n;i++)
flt = ref + scale*flt;
}
} /// end of "If1(n_bits<=25) in subfunc BDS_unpack()" ///
else /// beginning of "If1(n_bits<=25) 's else in subfunc BDS_unpack()" ///
{/* older unoptimized code, not often used */
c_bits = 8;
map_mask = 128;
while(n-- > 0) /// beginning of "while1(n-->0) in subfunc BDS_unpack()" ///
{if(bitmap)
{j = (*bitmap & map_mask);
if((map_mask >>= 1) == 0)
{map_mask = 128;
bitmap++;
}
if(j == 0)
{*flt++ = UNDEFINED;
continue;
}
}
jj = 0.0;
j_bits = n_bits;
while(c_bits <= j_bits)
{if(c_bits == 
{jj = jj * 256.0 + (double) (*bits++);
j_bits -= 8;
}
else
{jj = (jj * shift[c_bits]) + (double) (*bits & mask[c_bits]);
bits++;
j_bits -= c_bits;
c_bits = 8;
}
}
if(j_bits)
{c_bits -= j_bits;
jj = (jj * shift[j_bits]) + (double) ((*bits >> c_bits) & mask[j_bits]);
}
*flt++ = ref + scale*jj;
} /// ended of "while(n-->0) in subfunc BDS_unpack()" ///
} /// ended of "If1(n_bits<=25) 's else in subfunc BDS_unpack(), and ended the whole If...Else..." ///
return;
}
///// Ended of "void BDS_unpack()" function /////