Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/articles/c-tutorials/)
-   -   Decimal, Hex, octal and binary number inter conversion (http://www.go4expert.com/articles/decimal-hex-octal-binary-inter-t1548/)

shabbir 9Jan2007 17:40

Re: Decimal, Hex, octal and binary number inter conversion
 
Quote:

Originally Posted by ayat_Ace
hi mr shabbir
the answers i mean that you seems to me a very skillful experts so advise me to be good at assemly programming i mean that you know more than me about very powerful websites help me to improve my self in programming

thanks alot
could you send me your email please
my email : ayat_ace@yahoo.com

My email add is in the forum. Click on the contact us or also on my profile.

rai_gandalf 22Feb2007 02:55

Re: Decimal, Hex, octal and binary number inter conversion
 
Hey Shabbir,
Very useful thread, this one. The thing is one becomes so proficient in programming & concerned with advanced implementation issues, that one often forgets small snippets & nuggets of code such as this. ;) So, Thx a lot for it. :)

Waise, I developed a Decimal Integer to Binary String or Hexa String Convertor. I have put the source code here .... ;)

Ciao,
Rajiv


Code:

/* Decimal Integer to Binary String or Hexa String Convertor */

#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>

#define DEFAULTVAL 0

char* dec_to_binstr(int,int=DEFAULTVAL);
char* dec_to_hexstr(int,int=DEFAULTVAL);

void main()
  {
  int choice;
  unsigned int decqty,numbits;

  do{
    clrscr();
    printf("1. DEC TO BIN\t2. DEC TO HEX\n3. EXIT");
    printf("\nEnter choice :  ");
    scanf("%d",&choice);

    if(choice==1 || choice==2)
      {
      printf("\n\n\nEnter the Decimal Quantity :  ");
      scanf("%d",&decqty);
      printf("\nEnter a specific Number of Bits/Hexa-Digits if you want the O/P to contain a specific number of bits");
      printf("\n(Enter '0' (or DEFAULTVAL) to leave blank so that program calculates required number of bits) :  ");
      scanf("%d",&numbits);
      }

    switch(choice)
      {
      default:
        printf("\n\nINVALID CHOICE!! Please Try Again!!");
      case 1:
        printf("\n\nBinary Equivalent is :  %s",dec_to_binstr(decqty,numbits));
        break;
      case 2:
        printf("\n\nHexadecimal Equivalent is :  %s",dec_to_hexstr(decqty,numbits));
        break;
      case 3:
        break;
      }
    getch();
    }while(choice!=3);
  }

/* This Routine Converts a decimal Qty to binary Qty */
char* dec_to_binstr(int decnum,int numbits)
  {
  int i,j,k;
  char *result;
  char *temp;

  printf("\n\n\nEntered in Routine :  dec_to_binstr()");
  printf("\ndecnum = %d\tnumbits = %d",decnum,numbits);

  // Calculating Number of bits required
  for(i=0;pow(2,i)<abs(decnum);i++);

  printf("\nNo. of bits reqd (i) = %d",i);

  // Checking Whether 'numbits' provided in Call is adequate
  if(numbits!=DEFAULTVAL && numbits<i)
    return "ERROR!! Incorrect 'numbits' Specification during Call!!";

  // Allocating Memory for 'temp' & 'result'
  else if(numbits==DEFAULTVAL)
    // No 'numbits' provided in call
    {
    temp=(char*)malloc((i+1)*sizeof(char));
    result=(char*)malloc((i+1)*sizeof(char));
    }
  else
    // Function call has provided the 'numbits'
    // Assuming that 'numbits' involves the 1 extra character slot required
    // in case of signed integers
    {
    temp=(char*)malloc((numbits+1)*sizeof(char));
    result=(char*)malloc((numbits+1)*sizeof(char));
    }

  // Performing Intermediate processing on 'temp'
  for(j=0;decnum>0;j++)
    {
    temp[j]=char('0'+decnum%2);
    decnum=decnum/2;
    }

  // Padding Any Bits Remaining if at all with Zeroes
  for(;j<numbits;j++)
    temp[j]='0';
  temp[j]='\0';

  // Forming the 'result' string by String Reversal
  k=0;

  if(numbits==DEFAULTVAL)
    j=i-1;
  else
    j=numbits-1;

  for(;j>=0;j--,k++)
    result[k]=temp[j];
  result[k]='\0';

  return result;
  }

char* dec_to_hexstr(int decnum,int numbits)
  {
  int i,j,k;
  char *result;
  char *temp;
  int numtemp;
 
  printf("\n\n\nEntered in Routine :  dec_to_hexstr()!!");
  printf("\ndecnum = %d\tnumbits = %d",decnum,numbits);

  // Calculating Number of Hexa-digits required
  for(i=0;pow(16,i)<abs(decnum);i++);

  printf("\nNo. of Hexa-digits reqd (i) = %d",i);

  // Checking Whether 'numbits' provided in Call is adequate
  if(numbits!=DEFAULTVAL && numbits<i)
    return "ERROR!! Incorrect 'numbits' Specification during Call!!";

  // Allocating Memory for 'temp' & 'result'
  else if(numbits==DEFAULTVAL)
    // No 'numbits' provided in call
    {
    temp=(char*)malloc((i+1)*sizeof(char));
    result=(char*)malloc((i+1)*sizeof(char));
    }
  else
    // Function call has provided the 'numbits'
    // Assuming that 'numbits' involves the 1 extra character slot required
    // in case of signed integers
    {
    temp=(char*)malloc((numbits+1)*sizeof(char));
    result=(char*)malloc((numbits+1)*sizeof(char));
    }

  // Performing Intermediate processing on 'temp'
  for(j=0;decnum>0;j++)
    {
    numtemp=decnum%16;
    if(numtemp>=10)
      temp[j]=char('A'+(numtemp-10));
    else
      temp[j]=char('0'+numtemp);
    decnum=decnum/16;
    }

  // Padding Any Bits Remaining if at all with Zeroes
  for(;j<numbits;j++)
    temp[j]='0';
  temp[j]='\0';

  // Forming the 'result' string by String Reversal
  k=0;

  if(numbits==DEFAULTVAL)
    j=i-1;
  else
    j=numbits-1;

  for(;j>=0;j--,k++)
    result[k]=temp[j];
  result[k]='\0';

  return result;
  }


shabbir 22Feb2007 08:51

Re: Decimal, Hex, octal and binary number inter conversion
 
Quote:

Originally Posted by rai_gandalf
Very useful thread, this one. The thing is one becomes so proficient in programming & concerned with advanced implementation issues, that one often forgets small snippets & nuggets of code such as this. ;) So, Thx a lot for it. :)

My pleasure.

Quote:

Originally Posted by rai_gandalf
Waise, I developed a Decimal Integer to Binary String or Hexa String Convertor. I have put the source code here .... ;)

Thats a good prog and when I will reach home will try running it.

christina 26Mar2007 18:34

Re: Decimal, Hex, octal and binary number inter conversion
 
hi mr shabbir. i hope you receive my email to you. thank you very much to your program. i ask my other proffessor in school to help me, and he did, at first i dont understand but later on when i spend time (much time n_n' ) on i finally did it. all i can say that somehow, ur a genius sir.hee. coz i dont have the talent you possess. my talent is in drawing, i could be a future flash animator or a web developer.hee.ooopppss!!! why am i saying this.hee.sorry.
But let me give you my WARM-BIG THANKS SIR!!!
my prof gave me a grade of 91 for my effort and hardwork in hios subject.c=
thanks a LOT!!

shabbir 26Mar2007 18:51

Re: Decimal, Hex, octal and binary number inter conversion
 
christina, the pleasure is all mine but I would suggest one thing. Try to give something to the programming community from where you have got something and that may not necessarily be this forum.

kunalkunal 1Apr2007 17:19

Need Help!
 
Hey Shabbir,
Can you help me something?
I am BCA 1st sem. Student; I want a program in C language.

Write a program to transform its input according to a specified transformation scheme. The transformation scheme will consist of two strings: a string of characters and then a string of replacement characters. The idea is that your program replaces every instance of the ith character in the initial string with the (i+3) character (of English alphabets) in the replacement string. When no substitution is defined for a character, the program just passes it through to the output unchanged. The program should inform the user of any errors in the transformation scheme. Your program should display the phrase before and after the substitutions have been made.
Example:
Original String: This is a C program.
String after the transformation: Wklv lv d F Surjudp.


Pls help
Thanks in advance

shabbir 1Apr2007 18:00

Re: Decimal, Hex, octal and binary number inter conversion
 
kunalkunal, 2 things I would suggest to get the best out of any forums / groups.

1. Dont jump into others topic / article with your query.
2. Dont ask us to complete your assignments but you can always ask us the best way of doing it.

kunalkunal 1Apr2007 18:05

Re: Decimal, Hex, octal and binary number inter conversion
 
you are right but i am new in c programming, then how can i done it ?
so pls give me some tips
thanks

shabbir 1Apr2007 18:25

Re: Decimal, Hex, octal and binary number inter conversion
 
Just create a thread with your query in the Queries and discussion section

gbagnati 11May2007 23:45

Re: Decimal, Hex, octal and binary number inter conversion
 
One question to understand the conversion program from Decimal to Binary.
How does the variable bin[100] work?


All times are GMT +5.5. The time now is 01:59.