Results 1 to 5 of 5

Thread: Program to Convert Hexadecimal No into Binary No in C language

  1. #1
    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

  2. #2
    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:

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    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;
    }

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    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

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    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);
    }
    }

Similar Threads

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

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,903,154.27966 seconds with 16 queries