UrbanPro

Learn C Language from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

How can you convert hexadecimal to binary without using arrays in C program?

Asked by Last Modified  

Follow 1
Answer

Please enter your answer

Advance Excel And VBA Training

The moment a (C) program is loaded into a computer’s memory by the OS’s loader (in laymen terms - started) two concepts apply to the entity that we call data: storage representation On a separate note - as far as a computer is concerned everything it acts is a number: even what we call...
read more
The moment a (C) program is loaded into a computer’s memory by the OS’s loader (in laymen terms - started) two concepts apply to the entity that we call data: storage representation On a separate note - as far as a computer is concerned everything it acts is a number: even what we call code eventually becomes a carefully sequenced set of instructions each of which is still a number. As such, when a number comes to life it is stored in a format that can be acted on or manipulated by a given processor. When a human being wants to look at a number then the number’s representation becomes relevant: there is normally a one-to-many relationship between the storage and the representation since a number in an internal format can be rendered in various numerical basis, for example. A number in an internal, traditionally - binary, format is usually rendered in base sixteen in a textual form - as a sequence of characters each of which belongs to the base sixteen alphabet of 0 - 9 and A - F. If you have access to the variable of an integral binary type, such as int or long, for example, you can render that variable in a binary format directly. Assume that bits is a variable of the unsigned long type whose contents are to be rendered in a binary format on a 64 -bit machine: int i; char bd; unsigned long mask = 1UL << 63; for ( i = 0; i < 64; i++ ) { bd=bits & mask ? '1' : '0'; printf( "%c", bd ); mask>>= 1; } If you want to convert a base sixteen representation of a number to a binary representation then it can be done a multitude of ways. For example, convert the contents of the base sixteen string into an integral binary format via strtol() family of functions and then use the code above to render it visually in base two. If you are absolutely sure that the number rendered in base sixteen as string will fit into an int sized variable integer then: sscanf( string, "%x", &integer ); Adjust the sizes accordingly. If you want to convert a base sixteen character into a base two representation as an academic exercise then one way to do that is to: construct a base sixteen alphabet yourself in b16alph variable as follows: char b16alph[] = { '0', '1', '2', ..., 'F' }; scan the base sixteen string an a left-to-right fashion for each input base sixteen character hexch: find the location of hexch inside b16alph, say it's hexndx the contents of hexndx will be equal to the base ten internal representation of hexch render the contents of hexndx into base two representation as shown above (showing only a nibble, four rightmost bits, at a time: by shifting the mask leftward only 3 times (mask = 1 << 3)) read less
Comments

Home Tutor

/* C Program to convert decimal to binary without using array */ #include int main(){  int n, i, j, bin_rev=0,bin=0;  printf("Enter a number to convert : ");  scanf("%d",&n); /*****************************/  i=1;  while(n){  bin_rev=bin_rev+(n%2)*i;  i=i*10;  n=n/2;  }    i=1;  while(bin_rev){  bin=bin+(bin_rev%10)*i;  i=i*10;  bin_rev=bin_rev/10;  }  /*****************************/ printf("nBinary...
read more
/* C Program to convert decimal to binary without using array */ #include int main(){  int n, i, j, bin_rev=0,bin=0;  printf("Enter a number to convert : ");  scanf("%d",&n); /*****************************/  i=1;  while(n){  bin_rev=bin_rev+(n%2)*i;  i=i*10;  n=n/2;  }    i=1;  while(bin_rev){  bin=bin+(bin_rev%10)*i;  i=i*10;  bin_rev=bin_rev/10;  }  /*****************************/ printf("nBinary is %d",bin);    getch();  return 0; } read less
Comments

Related Questions

What is the best IDE for C and C++ development?
If you're a student then Turbo C++ is best for C and C++ program + inbuilt C/C++ Graphics Function If you're a professional or working then Netbeans for C/C++ or Code:: Blocks is Best.
Balakrishnasingh
In how many days can we learn the C language?
Around one month and depends upon you how much time you give.
Ravi Shankar
0 0
9
Other than in a for statement, when is the comma operator used?
Example : int i,j; i=(10,20,30,40); j=10,20,30,40; printf("%d",i); printf("%d",j); // in this program output of i will be 40 and j will be 10 // In interviews you can face such type of comma operator related questions.
Sanjay
Why is the C language important?
C language is a high-level, general-purpose programming language. It provides a straightforward, consistent, powerful interface for programming systems. That's why the C language is widely used for developing...
Ganesh
0 0
5

Now ask question in any of the 1000+ Categories, and get Answers from Tutors and Trainers on UrbanPro.com

Ask a Question

Related Lessons

INTRODUCTION TO PROGRAMMING LANGUAGES
Language is a medium for communication. If we want to perform anything with another person, we can know the human language as a human being. Similarly, if we want to perform anything with a computer, we...

Java and C trainer
Always think any conspect with real-time example like Object -- object must have states and behaviour then only we will call that thing is Object like fan is Object (rotating,color)

C-Program Swapping Contents Of Variables Using Function [Call By Reference Method]
//Header Files #include#include // User defined functions swap with 2 pointer variables passed as an argument list void swap(int*i,int*j){ // Local variable temp int temp; // swapping contents using...
S

C Program-String Comparison
// WAP to compare strings entered by the user //Header files #include<stdio.h>#include<conio.h>#include<string.h> //Main function void main(){ char str1; char str2; int comp; //Function...
S

Creating First Program Using C Language
Step 1: Install and setup Turbo C compiler on your computer. Step 2: Open Turbo C from your Desktop or Programs menu. Select “File” from menu bar and select option “New”. Step...

Recommended Articles

Lasya Infotech is a Hyderabad based IT training institute founded in 2016 by O Venkat. Believing in his innovation, passion and persistence and with a diverse blend of experience, he started his brainchild to deliver exemplary professional courses to aspiring candidates by honing their skills. Ever since the institute envisions...

Read full article >

Brilliant Academy is one of the reputed institutes for B.Tech tuition classes. This institute is specialised in delivering quality tuition classes for B.E, Engineering - all streams and Engineering diploma courses. Incorporated in 2012, Brillant Academy is a brainchild of Mr Jagadeesh. The main motto of the academy is to...

Read full article >

Hadoop is a framework which has been developed for organizing and analysing big chunks of data for a business. Suppose you have a file larger than your system’s storage capacity and you can’t store it. Hadoop helps in storing bigger files than what could be stored on one particular server. You can therefore store very,...

Read full article >

Business Process outsourcing (BPO) services can be considered as a kind of outsourcing which involves subletting of specific functions associated with any business to a third party service provider. BPO is usually administered as a cost-saving procedure for functions which an organization needs but does not rely upon to...

Read full article >

Looking for C Language Classes?

Learn from the Best Tutors on UrbanPro

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you
X

Looking for C Language Classes?

The best tutors for C Language Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn C Language with the Best Tutors

The best Tutors for C Language Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more