Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Numerical AnalysisRSS Feeds

Program to read decimal numbers from a file and convert into hexadecimal

Posted By: Easy Tutor     Category: C++ Programming     Views: 6520

Write a program to read decimal numbers from a text file and convert into hexadecimal. Input is terminated with a 0 for N which should not be processed.

Code for Program to read decimal numbers from a file and convert into hexadecimal in C++ Programming

# include <iostream.h>
# include <fstream.h>
# include <string.h>
# include <stdlib.h>
# include <conio.h>

 void hexadecimal(long);

 int main( )
    {
       clrscr( );

       fstream file("CP-01.txt",ios::in|ios::nocreate);

       if(!file)
      {
         cout<<"\n Unable to open the input file."<<endl;
         cout<<"\n Press any key to exit.";

         getch( );
         exit(EXIT_FAILURE);
      }

       long decimal_number=0;

       char Decimal_number[10]={NULL};

       do
      {
         strset(Decimal_number,NULL);

         file.getline(Decimal_number,6);

         decimal_number=atol(Decimal_number);

         if(decimal_number==0)
        break;

         hexadecimal(decimal_number);
      }
       while(1);

       file.close( ); 

       getch( );
       return 0;
    }

 /*************************************************************************///--------------------------  hexadecimal(long) -------------------------///*************************************************************************/void hexadecimal(long number)
    {
       int qutiont=0;
       int remainder=0;

       char Hex_digit[5]={NULL};
       char Hexadecimal_number[10]={NULL};

       do
      {
         qutiont=number/16;
         remainder=number%16;
         number=qutiont;

         if(remainder<10)
        {
           strset(Hex_digit,NULL);

           itoa(remainder,Hex_digit,10);

           strcat(Hexadecimal_number,Hex_digit);
        }

         else
        {
           switch (remainder)
              {
             case 10 : strcat(Hexadecimal_number,"A");
                   break;
             case 11 : strcat(Hexadecimal_number,"B");
                   break;

             case 12 : strcat(Hexadecimal_number,"C");
                   break;

             case 13 : strcat(Hexadecimal_number,"D");
                   break;

             case 14 : strcat(Hexadecimal_number,"E");
                   break;

             case 15 : strcat(Hexadecimal_number,"F");
                   break;
              }
        }
      }
       while(number>0);

       strrev(Hexadecimal_number);

       cout<<Hexadecimal_number<<endl;
    }

  
Share: 



Easy Tutor
Easy Tutor author of Program to read decimal numbers from a file and convert into hexadecimal is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!