📄 point2bin.c
字号:
////////////////////////////////////////////////////////////////////////////////// Coypright (C) Aviral Mittal.////////////////////////////////////////////////////////////////////////////////// All rights reserved. Reproducion in whole or in part is prohibited without // written consent of copyright Owner.The professional use of this material // is subject to the copy right owners approval in written. ////////////////////////////////////////////////////////////////////////////////// Comments welcome on avimit@yahoo.com// Visit http://www.vlsiip.com for more free stuff////////////////////////////////////////////////////////////////////////////////// This program converts the fractional part of a number into binary bits// The input given to this program will NOT have any decimal(point) in it// Example: ./point2bin 45823// Above is an example, where the user is trying to convert 0.45823 into binary// ./point2bit 45823// 0111010101001110////////////////////////////////////////////////////////////////////////////////#include <stdio.h>double pvalues;double inval = 0.0;int tothepower = 2;char inlen = 0;char mystring[10] ;char *pmystring ; //pointer to mystringchar error = 0;char resbin[32]; //binary resultchar i;//loop variable;char binbits = 32;long int po(short int x,short int y);int main(int argc, char* argv[]) {//Initialization pmystring = &mystring[0]; pvalues = 1/(double) tothepower;//Initialization Ends if(argc == 2) { //calculate length of input string and copy it into my string while (*argv[1] != '\0') { //check if the input is essentialy numerical if(*argv[1] >= '0' && *argv[1] <= '9') { //printf("Input OK\n"); } //if(*argv[1] >= '0' && *argv[1] <= '9') else { //printf("Input NOT OK\n"); error = 1; //One or more input elemets are reported to be non-numeric }// else of if(*argv[1] >= '0' && *argv[1] <= '9') //check if the input is essentialy numerical Ends *pmystring = *argv[1]; *argv[1]++; *pmystring++; inlen++; } //while (*argv[1] != '\0') *pmystring = '\0'; pmystring = &mystring[0]; //#printf("inlen = %d\n",inlen); //printf("mystring = %s\n",mystring);//////////////////////////////////////////////////////////////////////////////// //Convert the string into an integer/double but ONLY if error=0//////////////////////////////////////////////////////////////////////////////// if(error == 0) { //Convert Input String into a double for(i=0;i<inlen;i++){ //#printf("Input = %d\n",mystring[i]-48); //Here inval = (((double) (mystring[i]-48)))/po(10,(i+1)) + inval; }// for(i=0;i<inlen;i++) printf("input number is evluated as %f\n",inval); //Convert Input String into a double Ends for(i=0;i<binbits;i++) { //#### printf("pvalues for this itration = %f\n",pvalues); if(inval >= pvalues) { inval = inval-pvalues; resbin[i] = '1'; } else { resbin[i] = '0'; } //printf("resbin[i] = %d\n",resbin[i]); tothepower = tothepower*2; pvalues = 1/(double) tothepower; } //for(i=0;i<binbits<i++) resbin[binbits] = '\0'; printf("%s\n",resbin); //##printf("resbin = %s\n",resbin); }//if(error == 0) else { printf("Cannot convert Input, because Input format NOT numeric\n"); }// else of if(error == 0)//////////////////////////////////////////////////////////////////////////////// }//if(argc == 2) else { printf("\n"); printf("########################################################\n"); printf("Error: Not correct Arguments\n"); printf("This program converts the fractional part of any number into \n"); printf("Binary suitable for Fixed Point Representation \n"); printf("\n"); printf("Example: To convert 0.76283 Into Binary \n"); printf("Unix> point2bit 76283 \n"); printf("1100001101001000\n"); printf("\n"); printf("Note that ONLY the digits after the decimal are given as input\n"); printf("The INPUT format: ONLY the digits following a '.' can be given\n"); printf("########################################################\n"); printf("\n"); } // else of if(argc == 2)}//int main(int argc, char* argv[])//Functions Declarationlong int po(short int x,short int y){ //to Calculate 10^4, po(10,4); int j; long int ret=1; for(j=0;j<y;j++) { ret = ret*x; } return(ret);}//Functions Declaration Ends
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -