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  

2 Answers

Learn C Language

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 a macro, and how do you use it?
A macro is a fragment of code which has been given a name. Whenever the name is used, it is replaced by the contents of the macro. There are two kinds of macros. They differ mostly in what they look...
Sandeep
What is the difference between n=1 and n==1 in the C language?
'=' is ASSIGNMENT operator , where as '==' is COMPARISON operator. int a=7; assigns the variable a an integer value 7 , where as a==7; checks if the VALUE of a is EQUAL to 7.
Soujanya
0 0
8
Which is older - Java or C?
C is older than JAVA
Baby
Which should I learn first, C or UNIX?
As you know C is a programming language and UNIX is Operating System if you want to grow your career as Programmer/Developer than go for C/C++ programming as it is very basic programming languages and...
Tushar
0 0
5
What are the toughest topics in C language?
1. pointer 2. dynamic memory allocation 3. File handling
Lokayya
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

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