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.
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...
Write a program in Python that - Takes in a gross salary. - If the gross...
Write a program in Python that - Takes in a gross salary. - If the gross salary is above €50.000 taxes are 50%. - If the gross salary is above €25.000 taxes are 25%. - If the gross salary is above €10.000 taxes are 10%. - If the gross salary is below €10.000 there is no tax. - Output the gross salary and the net income. Note: Give the pseudocode of your program.
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...
I am trying to create a method in JAVA that takes in an ArrayList and sorts...
I am trying to create a method in JAVA that takes in an ArrayList and sorts it by the requested "amenities" that a property has. So if someone wants a "pool" and "gym" it would show all members of the array that contain a "pool" and "gym". 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. You can edit it...
Write a program that takes two command line arguments at the time the program is executed....
Write a program that takes two command line arguments at the time the program is executed. You may assume the user enters only decimal numeric characters. The input must be fully qualified, and the user should be notified of any value out of range for a 23-bit unsigned integer. The first argument is to be considered a data field. This data field is to be is operated upon by a mask defined by the second argument. The program should display...
I am trying to create a makefile for the following program in Unix. The C++ program...
I am trying to create a makefile for the following program in Unix. The C++ program I am trying to run is presented here. I was wondering if you could help me create a makefile for the following C++ file in Unix and show screenshots of the process? I am doing this all in blue on putty and not in Ubuntu, so i don't have the luxury of viewing the files on my computer, or I don't know how to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT