Question

In: Computer Science

I got a little problem when i was trying to write a program that takes a...

I got a little problem when i was trying to write a program that takes a hexadecimal number and convert it to a binary number. It just don't give me the right answer. Here's my code:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

int main(int argc, char *argv[])
{
unsigned long int i;
i=0xffff;
char buffer[65];
sprintf(buffer, "%lud", i);
long int x = 0;
while (buffer[x]) {
  
switch (buffer[x]) {
case '0':
printf("0000");
break;
case '1':
printf("0001");
break;
case '2':
printf("0010");
break;
case '3':
printf("0011");
break;
case '4':
printf("0100");
break;
case '5':
printf("0101");
break;
case '6':
printf("0110");
break;
case '7':
printf("0111");
break;
case '8':
printf("1000");
break;
case '9':
printf("1001");
break;
case 'A':
case 'a':
printf("1010");
break;
case 'B':
case 'b':
printf("1011");
break;
case 'C':
case 'c':
printf("1100");
break;
case 'D':
case 'd':
printf("1101");
break;
case 'E':
case 'e':
printf("1110");
break;
case 'F':
case 'f':
printf("1111");
break;
default:
printf("\nInvalid hexadecimal digit %c",
buffer[x]);
}
x++;
}
return 0;
}

Solutions

Expert Solution

`Hey,

Note: Brother if you have any queries related the answer please do comment. I would be very happy to resolve all your queries.


#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

int main(int argc, char *argv[])
{
unsigned long int i;
i=0xffff;
char buffer[65];
sprintf(buffer, "%x", i);
long int x = 0;
while (buffer[x]) {

switch (buffer[x]) {
case '0':
printf("0000");
break;
case '1':
printf("0001");
break;
case '2':
printf("0010");
break;
case '3':
printf("0011");
break;
case '4':
printf("0100");
break;
case '5':
printf("0101");
break;
case '6':
printf("0110");
break;
case '7':
printf("0111");
break;
case '8':
printf("1000");
break;
case '9':
printf("1001");
break;
case 'A':
case 'a':
printf("1010");
break;
case 'B':
case 'b':
printf("1011");
break;
case 'C':
case 'c':
printf("1100");
break;
case 'D':
case 'd':
printf("1101");
break;
case 'E':
case 'e':
printf("1110");
break;
case 'F':
case 'f':
printf("1111");
break;
default:
printf("\nInvalid hexadecimal digit %c",
buffer[x]);
}
x++;
}
return 0;
}

Kindly revert for any queries

Thanks.


Related Solutions

I am trying to write a UDP socket program in which there is a server program...
I am trying to write a UDP socket program in which there is a server program and a client program. This is what I have but I can't figure out where I am going wrong. This is the input and expected output: > gcc udpclient.c –o clientout > gcc udpserver.c –o serverout > ./serverout 52007 ‘to celebrate’ & > ./clientout odin 52007 ‘Today is a good day’ Today is a good day to celebrate //UDP echo client program #include #include...
I got this homework, to make an appointment program. it says like this: Write a superclass...
I got this homework, to make an appointment program. it says like this: Write a superclass Appointment and subclasses OneTime, Daily, and Monthly. An Appointment has a description (for example “see the dentist”) and a date. Write a method occursOn(int year, int month, int day) that checks whether the appointment occurs on that date. For example, for a monthly appointment, you must check whether the day of the month matches. You are provided with a Junit test AppointmentTest .java. Run...
I am trying to write a java program that determines if an inputted year is a...
I am trying to write a java program that determines if an inputted year is a leap year or not, but I am not able to use if else statements how would I do it. I don't need the code just advice.
Write a program in Arduino that, when it takes in a voltage range (0 to 5...
Write a program in Arduino that, when it takes in a voltage range (0 to 5 volts), gives a corresponding servo motor angle with a range from 0 to 180 degrees.
Text Wrap Problem Write a program in Python that takes an input string and prints it...
Text Wrap Problem Write a program in Python that takes an input string and prints it as multiple lines of text such that no line of text is greater than 13 characters and words are kept whole. For example, the first line of the Gettysburg address: Four score and seven years ago our fathers brought forth upon this continent a new nation, conceived in liberty and dedicated to the proposition that all men are created equal Becomes: Four score and...
I get an error when im trying to run this java program, I would appreciate if...
I get an error when im trying to run this java program, I would appreciate if someone helped me asap, I will make sure to leave a good review. thank you in advance! java class Node public class Node { private char item; private Node next; Object getNext; public Node(){    item = ' '; next = null; } public Node(char newItem) { setItem(newItem); next = null; } public Node(char newItem, Node newNext){ setItem(newItem); setNext(newNext); } public void setItem(char newItem){...
Hello, I am trying to write a C++ program that will do the following: Use the...
Hello, I am trying to write a C++ program that will do the following: Use the STL stack container in a program that reads a string, an arithmetic expression to be exact, one character at a time, and determines if the string has balanced parenthesis – that is, for each left parenthesis there is exactly one matching right parenthesis later in the string.                         Use the following strings to test your program. A+ B - C A * B / (C...
I am trying to write a program in Java: Pick 4 cards and display the cards...
I am trying to write a program in Java: Pick 4 cards and display the cards and their total value (Ace = 1, King = 13, Queen = 12, and Jack 11...etc. , BUT you should check that no card is a duplicate... there is only one "Ace of Spades" per deck for example. I need to utilize the displayMenu so the program continues to run for the user without breaking. My program is not running correctly. import java.util.Scanner; import...
I am trying to write code for a program in Visual Studo using Visual Basic programming...
I am trying to write code for a program in Visual Studo using Visual Basic programming language that computes the factorial of an entered number by using a For Loop. My problem is that it keeps re-setting the variable for Factorial. Below is my code if anyone can help. I want it to multiply the given number by that number - 1, then continuing to do so until it hits zero without multiplying by zero. Private Sub BtnCalculate_Click(sender As Object,...
I am trying to create a function in JAVA that takes in an ArrayList and sorts...
I am trying to create a function in JAVA that takes in an ArrayList and sorts the values by their distance in miles in increasing order. So the closest (or lowest) value would be first. It does not need to output the values in anyway, but it should return them so they can be output elsewhere. Please try to use the stub class below. The code for the main class is not necessary. I am only really asking for the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT