Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 06-03-2009
Member
 
Join Date: Jan 2009
Posts: 85
Program to Convert Hexadecimal No into Binary No in C language

hi there

I need a small help from you people as to convert Hexadecimal number to Binary number in C language.

Please provide some views as i am not so good programmer in c language
Reply With Quote
  #2  
Old 06-03-2009
Member
 
Join Date: Apr 2008
Posts: 1,948
Re: Program to Convert Hexadecimal No into Binary No in C language

To convert hexadecimal to binary, at each hexadecimal digit we associate the 4 bits binary number using the following table:
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001
A 1010
B 1011
C 1100
D 1101
E 1110
F 1111

.
For example, FCB1=1111110010110001:
Reply With Quote
  #3  
Old 06-03-2009
Member
 
Join Date: May 2008
Posts: 2,008
Re: Program to Convert Hexadecimal No into Binary No in C language

#include <iostream>
#include <iomanip>
#include <sstream>
using namespace std;
string toBinary(unsigned int num)
{
stringstream s;
for(int i=0; i<sizeof(int)*8; ++i){
s << (num&0x0001) ;
num >>=1;
}
return s.str();
}
void outLine(unsigned int num)
{
cout << setw(10)<< dec << num;
cout << " bin: " << toBinary(num);
cout << " hex:" << hex << setw(10) << num << endl;
}
int main()
{
unsigned int num=0;
outLine(num);
num=1;
while(num>0) {
outLine(num);
num *=2;
}
outLine(UINT_MAX);
cout << endl;
return 0;
}
Reply With Quote
  #4  
Old 06-03-2009
Member
 
Join Date: Apr 2008
Posts: 2,001
Re: Program to Convert Hexadecimal No into Binary No in C language

Dim Binstr As String
Hexstr = Hex$(number)
For LV = 1 To Len(HexStr)
Select Case Mid$(Hexstr, LV, 1)
Case "0"
BinStr = BinStr & "0000"
Case "1"
BinStr = BinStr & "0001"
Case "2"
'and so on.
Case "15"
BinStr = BinStr & "1111"
End Select
Next
Reply With Quote
  #5  
Old 06-03-2009
Member
 
Join Date: May 2008
Posts: 2,293
Re: Program to Convert Hexadecimal No into Binary No in C language

import java.io.*;
import java.lang.*;

public class BinaryToHexa{
public static void main(String[] args)throws IOException{
BufferedReader bf= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the Binary number:");
String hex = bf.readLine();
long num = Long.parseLong(hex);
long rem;
while(num > 0){
rem = num % 10;
num = num / 10;
if(rem != 0 && rem != 1){
System.out.println("This is not a binary number.");
System.out.println("Please try once again.");
System.exit(0);
}
}
int i= Integer.parseInt(hex,2);
String hexString = Integer.toHexString(i);
System.out.println("Hexa decimal: " + hexString);
}
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Program to Convert Hexadecimal No into Binary No in C language"
Thread Thread Starter Forum Replies Last Post
In game language options for Binary Domain Tech^Geek Video Games 1 18-04-2012 07:51 PM
Problem to convert binary to string steg Software Development 1 08-04-2011 01:21 AM
Convert string to binary in java TechGate Software Development 5 28-01-2010 01:36 PM
c program to convert decimal to binary Oswaldo Software Development 3 25-11-2009 03:45 PM
Convert Hex Value to Date from Binary Reg Key RobT Windows Server Help 4 01-04-2005 11:55 AM


All times are GMT +5.5. The time now is 06:05 PM.