|
안녕하세요 만해입니다.
double형 변수를 비트배열로 추출하는 소스를 만들어 봤는데요
혹시 더 좋은 방법이 없을까해서 여쭤 봅니다.
소스전문을 올릴께요
고수님들의 손길을 기대 합니다.
//---------------------------------------------------------------------------
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <conio.h>
#pragma hdrstop
//---------------------------------------------------------------------------
void d2s(double data , unsigned int bits, unsigned int radix , unsigned int append);
//---------------------------------------------------------------------------
#pragma argsused
int main(int argc, char* argv[])
{
double test = 25.1;
unsigned int bits = 64;
d2s(test,bits,10,53);
getche();
return 0;
}
//---------------------------------------------------------------------------
void d2s(double data , unsigned int bits,unsigned int radix , unsigned int append)
{
char *p_str;
char dstr[9];
char* str;
char Nibbles[16][5] = {
{"0000"},{"0001"},{"0010"},{"0011"},
{"0100"},{"0101"},{"0110"},{"0111"},
{"1000"},{"1001"},{"1010"},{"1011"},
{"1100"},{"1101"},{"1110"},{"1111"}
};
unsigned int HNibble=0;
unsigned int LNibble=0;
unsigned char sign;
unsigned char *lpcstr_radix;//[11]={0,};
unsigned char *lpcstr_append;//[54]={0,};
int i =0;
str = (char*)malloc(bits+1);
lpcstr_radix = (char*)malloc(radix+1);
lpcstr_append = (char*)malloc(append+1);
str[0] = '\0';
p_str = (char*)(&data);
printf("floating values is %f\n",data);
printf("floating values string is\n");
for ( i = 7 ; i >= 0 ;i -- )
{
dstr[i] = p_str[i];
HNibble = (dstr[i] & 0xF0) >> 4;
LNibble = (dstr[i] & 0x0F);
printf("%s|%s!",Nibbles[HNibble],Nibbles[LNibble]);
strcat(str,Nibbles[HNibble]);
strcat(str,Nibbles[LNibble]);
}
dstr[8] = '\0';
printf("\n");
sign = str[0];
p_str = &str[1];
strncpy(lpcstr_radix,p_str,radix);
p_str = &str[11];
strncpy(lpcstr_append,p_str,append);
printf("sign bit is : %c\n",sign);
printf("radix is : %s\n",lpcstr_radix);
printf("append is : %s\n",lpcstr_append);
free(dstr);
free(lpcstr_radix);
free(lpcstr_append);
}
//---------------------------------------------------------------------------
결과는 이렇게 나옵니다
floating values is 25.100000
floating values string is
0100|0000!0011|1001!0001|1001!1001|1001!1001|1001!1001|1001!1001|1001!1
sign bit is : 0
radix is : 1000000001
append is : 11001000110011001100110011001100110011001100110011010
고수님들의 조언을 부탁 드립니다.
|