Turbo-C
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
터보-C 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
Lua 게시판
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
볼랜드포럼 광고 모집

C/C++ Q/A
[3215] [만해]double 형 변수의 비트배열 추출 하기
강재호.만해 [greenuri] 2057 읽음    2004-02-09 01:41
안녕하세요 만해입니다.

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

고수님들의 조언을 부탁 드립니다.

+ -

관련 글 리스트
3215 [만해]double 형 변수의 비트배열 추출 하기 강재호.만해 2057 2004/02/09
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.