天天看點

ORACLE 數字算法的C語言實作

先來研究NUMBER類型的數字回推算法

類型 <[長度]>,符号/指數位 [數字1,數字2,數字3,......,數字20]

1、長度類型沒什麼号說的

2、符号位,這個需要說一下

The sign bit, which is the high order bit (128)

按照文檔的說法這個判斷方法是和128進行最高位按位與出來的,如果

這位小于128則是負數,我們使用127試試吧

128的二進制為1000 0000 

127的二進制為0111 1111

最高位1&0=0 則代表他是負數,

如果是129則是正數

129的二進制為1000 0001

最高位1&1=1 則代表他是正數,

3、指數位

正數

128固定為正負數的判斷

65固定 The offset, which is always 65

那麼就是128+65=193就是正數指數的固定數值

負數

固定為62為負數的固定數值

4、數字位

那我們來判斷這樣一個數字

193,64,13,31

那麼就是

193-193=0

(64-1)*100^(0-0) =63

(13-1)*100^(0-1) =0.12

(31-1)*100^(0-2) =0.0030

為63.123

62,38,89,71,102

62-62=0

101-38 = 63 *100^(0-0)=63

101-89 = 12 *100^(0-1)=0.12

101-71 = 30 *100^(0-2)=0.003

102 為排序位不用理會

是以為-63.123

研究了數字的算法接下來我們使用C語言進行實作

考慮實際的16進制格式

4,c1,40,d,1f      63.123

5,3e,26,59,47,66  -63.123

04,c1,40,0d,1f,05,3e,26,59,47,66

#include <stdio.h>

#include <math.h>

#include <stdlib.h>

struct bin1b

{

unsigned a1:1;

unsigned a2:1;

unsigned a3:1;

unsigned a4:1;

unsigned a5:1;

unsigned a6:1;

unsigned a7:1;

unsigned a8:1;

};

void main()

int sseek=8182;//設定偏移量後期可以直接推算出來

int clg,digt,i;

double a=0.0;

char clgc;

char *Ptr;

int *Ptrn;

struct bin1b *data;

FILE *fp;

fp=fopen("D:\\c\\95393.dbf","rb");

if(!fp)

printf("can't open this file!");

}

else

fseek(fp,sseek+1,0);

        fread(&clgc,1,1,fp);

   printf("%d\n",ftell(fp));

clg=clgc;

Ptr=(char *)malloc(clg); //配置設定字段長度的一個4個單位元組記憶體空間

Ptrn=(int *)malloc(sizeof(int)*clg);

fread(Ptr,1,clg,fp);

printf("%d\n",ftell(fp));

for(i=0;i<clg;i++)

data=&Ptr[i];

Ptrn[i]=data->a1+data->a2*2+data->a3*pow(2,2)+data->a4*pow(2,3)+data->a5*pow(2,4)+data->a6*pow(2,5)+data->a7*pow(2,6)+data->a8*pow(2,7);

printf("%d\n",Ptrn[i]);

if(Ptrn[0]<128)

digt=62-Ptrn[0];

if((i>0) && (Ptrn[i]!=102))

a=a+(101-Ptrn[i])*pow(100,(digt-(i-1)));

if(Ptrn[0]>128)

digt=Ptrn[0]-193;

if(i>0 )

a=a+(Ptrn[i]-1)*pow(100,(digt-(i-1)));

printf("%lf\n",-a);

printf("%lf\n",a);

        free(Ptr);

   free(Ptrn);

fclose(fp);

繼續閱讀