Question

(In c++) Write a void function that receives an unsigned integer value and displays the hex...

(In c++) Write a void function that receives an unsigned integer value and displays the hex representation of value. You must use bit manipulation techniques.

Note that you must convert the hex value for each 4 bits starting at the right side. The hex value for 0 to 9 is the same digit. The hex value for 10 is A for 11 is B, for 12 id C, for 13 is D, for 14 is E and for 15 is F.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>

#include <iostream>

#include <string>

using namespace std;

void decimalToHexa(unsigned int dec_num)

{

               // First converting the number into binary

               int binary_num[1000];

               int i = 0;

               while (dec_num > 0)

               {

                              binary_num[i] = dec_num % 2;

                              dec_num = dec_num / 2;

                              i++;

               }

               // Second converting binary into hexadecimal format

               int k = 0;

               string hex_num = "";

               while (k < i)

               {

                              int hex = 0;

                              // getting the binary numbers as a group of 4 digits

                              for (int j = 0; j < 4; j++)

                              {

                                             if (binary_num[j + k] == 1)

                                             {

                                                            hex = hex + pow(2, j);

                                             }

                              }

                              // incrementing the value by 4 so that in next iteration, we move to next 4 digits of the binary number

                              k+=4;

                              // based on the value we decide which string to concatenate to the result hex_num

                              if (hex == 10)

                              {

                                             hex_num = hex_num + "A";

                              }

                              else if (hex == 11)

                              {

                                             hex_num = hex_num + "B";

                              }

                              else if (hex == 12)

                              {

                                             hex_num = hex_num + "C";

                              }

                              else if (hex == 13)

                              {

                                             hex_num = hex_num + "D";

                              }

                              else if (hex == 14)

                              {

                                             hex_num = hex_num + "E";

                              }

                              else if (hex == 15)

                              {

                                             hex_num = hex_num + "F";

                              }

                              else

                              {

                                             hex_num = hex_num + to_string(hex);

                              }

               }

               // we got the hex_num in reverse order so, reversing the entire string to get the actual result

               reverse(hex_num.begin(), hex_num.end());

               cout << hex_num << endl;

}

int main()

{

               // testing of the method

               decimalToHexa(10); // A

               decimalToHexa(16); // 10

               decimalToHexa(456); // 1C8

               decimalToHexa(1024); // 400

               decimalToHexa(8998533); // 894E85

               return 0;

}

OUTPUT

10 1C8 400 894E85

Add a comment
Know the answer?
Add Answer to:
(In c++) Write a void function that receives an unsigned integer value and displays the hex...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please give me exactly correct answer! PLEASE DO IT IN C++. If you do it 100%...

    Please give me exactly correct answer! PLEASE DO IT IN C++. If you do it 100% correct, you will get bunch of ratings. Guaranteed! Write a void function that receives an unsigned integer value and displays the hex representation of value. You must use bit manipulation techniques Note that you must convert the hex value for each 4 bits starting at the right side. The hex value for O to 9 is the same digit. The hex value for 10...

  • Write a program that allows the user to enter an unsigned integer (the maximum value of...

    Write a program that allows the user to enter an unsigned integer (the maximum value of an unsigned 4-byte int is 232 = 4,294,967,296) and reverses its format (from little to big endian, or vice versa). Print out the user-entered number in hexadecimal and binary, reverse the endianness, and print the reverse in hexadecimal and binary. Integers in most machine architectures are represented in little endian format: the least significant byte is stored in the smallest address; for instance, the...

  • (3 pts) Consider an unsigned fixed point decimal (Base10) representation with 8 digits, 5 to the...

    (3 pts) Consider an unsigned fixed point decimal (Base10) representation with 8 digits, 5 to the left of the decimal point and 3 to the right. a.      What is the range of the expressible numbers?    b.      What is the precision?    c.       What is the error?    ______________________________________________________________________________   (3 pts) Convert this unsigned base 2 number, 1001 10112, to each base given below   (Note: the space in the binary string is purely for visual convenience) Show your work. Using...

  • For integer n € {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15) function hex(n) returns hexadecimal character from F. Write function hex and...

    For integer n € {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15) function hex(n) returns hexadecimal character from F. Write function hex and use it to create the function HEX(N) that for integer N>0 displays its value hexadecimal form. For all programs use C++, and make all programs as short as possible. Write the main program that supports the following sample dialog: Enter a positive integer : 13 Hexadecimal value D Enter a positive integer : 255 Hexadecimal value Enter a positive integer ; 1234567 Hexadecimal value...

  • Help please! Program in C Problem-4 Write a function that receives an unsigned number and generates...

    Help please! Program in C Problem-4 Write a function that receives an unsigned number and generates and returns a new number that may or may not have an error in it. An error on a random bit must be introduced randomly. Use the rand function to generate 0 or 1 to see if the error needs to be generated and use a rand function in the range of 1 to 32 to see on which bit positon the error needs...

  • (Packing Characters into an Integer) The left-shift operator can be used to pack four character values into a four-byt...

    (Packing Characters into an Integer) The left-shift operator can be used to pack four character values into a four-byte unsigned int variable. Write a program that inputs four characters from the keyboard and passes them to function packCharacters. To pack four characters into an unsigned int variable, assign the first character to the unsigned intvariable, shift the unsigned int variable left by 8 bit positions and combine the unsigned variable with the second character using the bitwise inclusive OR operator....

  • In C, thanks. #include <stdio.h> void set-flag (unsigned int* flag-holder , int flag-position); void unset-flag (unsigned...

    In C, thanks. #include <stdio.h> void set-flag (unsigned int* flag-holder , int flag-position); void unset-flag (unsigned int * flag-holder, int flag-position); int check-flag (unsigned int flag-holder , int flag-position); void display -32_flags (unsigned int flag-holder); int main(int argc, char* argv (1) unsigned int flag -holder = 0; set-flag (& flag-holder, 3); set-flag (& flag-holder, 16); set-flag (& flag-holder, 31); display-32-flags (flag-holder); unset-flag(& flag-holder , 31); unset-flag (& flag-holder, 3); set-flag (& flag-holder , 9); display-32-flags (flag-holder ); return 0; Write...

  • 10.11 (Left Shifting Integers ) Left shifting an unsigned int by 1 bit is equivalent to...

    10.11 (Left Shifting Integers ) Left shifting an unsigned int by 1 bit is equivalent to multiplying the value by 2. Write function power2 that takes two integer arguments number and pow and calculates number * 2pow. You should declare pow as an unsigned integer , but remember that the result may be a negative value . Use the shift operator to calculate the result. Print the values as integers and as bits . Assume that an integer is of...

  • 1) Write a C program that reads a hexadecimal value from the keyboard and then stores...

    1) Write a C program that reads a hexadecimal value from the keyboard and then stores the value into an unsigned char variable. Read two int values p and n from the keyboard, where the values are less than 8. Implement the following commands: S – sets the n bits starting at position p to 11..1 R – resets the n bits starting at position p to 00…0 F – flips the n bits starting at position p to their...

  • problem 1 midterm-1-coding.pdf C:/Users/Cute%20khalsa/Downloads/midterm-1-coding.pdf 2 Problem #1 The first problem is to write the from.fixed point...

    problem 1 midterm-1-coding.pdf C:/Users/Cute%20khalsa/Downloads/midterm-1-coding.pdf 2 Problem #1 The first problem is to write the from.fixed point function, using "bit fiddling". double from_fixed point(unsigned short x); The parameter x is a representation (sometimes called a packed representation) of a fixed- point number. The right most 4 bits of x are the fractional part of the number. We will call them b. The leftmost 12 (or more) bits of x are the integer part of the number. We will call them a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT