2009年6月16日 星期二

知識+ 位元陣列轉換

//===============================================================
// 20090616 知識 +
// http://tw.knowledge.yahoo.com/question/question?qid=1509061603511
// 發問者 :(pplgo ) http://tw.knowledge.yahoo.com/my/my?show=AD01409480
//===============================================================

這還是一個小程式, 是做位元陣列轉換的, 題目是將8bits的陣列, 轉換為5bits的陣列,
我寫了一種方法; 而東大也寫了一種方法, 因有參考價值所以一併收錄...



#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// ADing法轉換 -------
unsigned char * BitsConvert(int bits, unsigned char *A, int *size)
{
if (bits<1 || bits>8 || A==NULL || *size<1) return NULL;
int i, a, b, s[8], len=*size;
*size = (*size*8+bits-1)/bits;
unsigned char * B = (unsigned char *) malloc(*size);
unsigned char m = 0xFF>>(8-bits);
for(b=0, i=8-bits; b<8; ++b, i=(i<=0)?i+8-bits: i-bits)
s[b]=i;
for(b=0; b<*size; ++b)
{
i=s[b%8];
a=b*bits/8;
if (i>=0) B[b] = (A[a]>>i)&m;
else if(a+1>=len) B[b] = (A[a]<<-i)&m;
else B[b] = ((A[a]<<-i)|(A[a+1]>>(8+i)))&m;
}
return B;
}
// 東大法轉換 --------
unsigned char * BitsTransfer(int bits, unsigned char *A, int *size)
{
if (bits<1 || bits>8 || A==NULL || *size<1) return NULL;
int i, j, a, b, d, len=*size;
*size = (*size*8+bits-1)/bits;
unsigned char * B = (unsigned char *) malloc(*size);
unsigned char m = 0x80;
for(a=b=d=j=0; a<len; ++a)
{
for(i=0; i<8; ++i)
{
d = (d<<1) + ((A[a]&m)? 1: 0);
if((m>>=1)==0) m=0x80;
if(++j==bits)
{
B[b++]=d;
d=j=0;
}
}
}
if (j) B[b++]=d<<(bits-j);
return B;
}
//==================================
const int MAX = 32; // 資料量
const int BIT = 5; // 擷取bit數

// --- 資料顯示
void DisplayData( char *Msg, unsigned char *data, int len)
{
int i;
printf(Msg, len);
for(i=0; i<len; )
{
printf("%02X ", data[i]);
if(++i%20==0 || i==len) printf("\n ");
}
}

// 主程式 =======
int main()
{
unsigned char *a;
int i, len;
unsigned char *b;
double runC, runT;
clock_t startC, startT;

a = (unsigned char *)malloc(MAX);
srand((unsigned)time(NULL));
for(i=0; i<MAX; i++)
a[i] = (unsigned char)(rand()%0x100);
DisplayData("顯示原始資料, 共%d筆:\n ", a, MAX);

//-------------------------- BitsConvert()
startC = clock();
len = MAX;
b = BitsConvert(BIT, a, &len);
runC = (double)(clock()-startC)*1000/CLOCKS_PER_SEC;
if (b!=NULL)
{
DisplayData("\n顯示用 BitsConvert()處理後的資料, 共%d筆:\n ", b, len);
free(b);
}
printf("\n* BitsConvert() 的處理時間為 %.2fms\n\n", runC);
//--------------------------

//-------------------------- BitsTransfer()
startT = clock();
len = MAX;
b = BitsTransfer(BIT, a, &len);
runT = (double)(clock()-startT)*1000/CLOCKS_PER_SEC;
if (b!=NULL)
{
DisplayData("\n顯示用 BitsConvert()處理後的資料, 共%d筆:\n ", b, len);
free(b);
}
printf("\n* BitsTransfer() 的處理時間為 %.2fms\n\n", runT);
//--------------------------

free(a);
return 0;
}
//==============================================

沒有留言:

張貼留言