Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write code in C please. Provided Code: /* Number converter Menu Convert between integer, binary, octal and hexadecimal This program should accept numeric values in

Write code in C please.

image text in transcribed

Provided Code:

/*

Number converter

Menu Convert between integer, binary, octal and hexadecimal

This program should accept numeric values in hexadecimal, decimal, octal and binary formats as:

Hex 0x0 to 0xFFFFFFFF Dec 0 to 4294967295 Oct o0 to o37777777777 Bin b0 to b11111111111111111111111111111111

After a value is input the code in main will interpret the data types above an process the conversion to an unsigned int. The unsigned int will be used to convert the input to strings containing hexadecimal, octal and binary strings.

*/

#include #include

//int input_to_decimal(char *input); unsigned int bin_to_uint(char *input); unsigned int oct_to_uint(char *input); unsigned int hex_to_uint(char *input); unsigned int dec_to_uint(char *input); void uint_to_hex(unsigned int n, char *output); void uint_to_oct(unsigned int n, char *output); void uint_to_bin(unsigned int n, char *output);

int error = 0;

int main(){

char input[50]; unsigned int n = 0; char output[50];

// Write code here to test your functions // Uncomment code below when done

/* printf("Enter a binary, octal, decimal or hexadecimal number "); printf("convert > "); gets(input);

// Detect input data type // Hexadecimal if(input[0] == '0' && input[1] == 'x'){ n = hex_to_uint(input); } // Decimal else if(input[0] >= '0' && input[0]

// Print results printf("The decimal value of %s is %u ", input, n); uint_to_hex(n, output); printf("The hexadecimal value of %s is %s ", input, output); uint_to_oct(n, output); printf("The octal value of %s is %s ", input, output); uint_to_bin(n, output); printf("The binary value of %s is %s ", input, output); */

return 0; }

/* This function converts the value part of the hex string to an unsigned integer value. The first two chars are 0x, which tells that the string is in hex. Start processing the value at index 2 until the null, calculating the int value as you would on paper. Try on paper first. */ // Convert a hexadecimal char array to uint unsigned int hex_to_uint(char *input){ // Declare result and set to zero unsigned int res = 0; // Declare and set multiplier to 1

// Declare iterator

// Loop through value part of input string

// If between 0 and 9 add 0 to 9 to res with multiplier

// If between A and F add 10 to 15 to res with multiplier

// Error - exit

// Advance multiplier to next position value

return res; }

/* Copy hex_to_uint() and modify for decimal input. */ // Convert a unsigned integer char array to uint unsigned int dec_to_uint(char *input){ // Declare result and set to zero unsigned int res = 0;

return res; }

/* Copy dec_to_uint() and modify for octal input. */ // Convert a octal char array to uint unsigned int oct_to_uint(char *input){ // Declare result and set to zero unsigned int res = 0;

return res; }

/* Copy oct_to_uint() and modify for binary input. */ // Convert a binary char array to unsigned int unsigned int bin_to_uint(char *input){ // Declare result and set to zero unsigned int res = 0;

return res; }

/* This function converts from unsigned int to a hex char array. Try this on paper before coding. */ // Convert a unsigned integer char array to hexadecimal void uint_to_hex(unsigned int n, char *output){ // Declare a uint for remainder

// Declare an int for division

// Declare a char array buffer

// Use a loop to generate a hex string - string will be reverse

// Get last hex char

// Put null at end of buffer

// Copy 0x to output string

// Copy chars from buffer in reverse order to output string

return; }

/* Copy uint_to_hex() and modify for octal */ // Convert a unsigned integer char array to octal void uint_to_oct(unsigned int n, char *output){

return; }

/* Copy uint_to_oct() and modify for binary */ // Convert a unsigned integer char array to binary void uint_to_bin(unsigned int n, char *output){

return; }

Part 1: Converting data types (50 points) In this project, you will write code to convert strings containing hexadecimal. Decimal, octal and binary strings to an unsigned integer. Some of the code is completed for you. Open the Convert source code file and look at the completed code and comments. The description and comments in each function should help you complete this project. To convert from hex string to unsigned int, accumulate the position-wise values of each character in the string. Remember that you can perform math operations on char data because they are numbers and have specific positions in the ASCII table. This method is like the one shown in lecture. Convert OxFFFF to a unsigned char, considering that F 15 and each hex char a multiple of 16: 15 *115 1615 25615* 4096 65,535 This process is similar with decimal, octal and binary. To from an unsigned int 16,535 convert to hex: 1. 2. 3. 4. 65,535 / 16 = 4095 and 65,535 % 16-15 4,095 / 16 = 255 and 4095 % 16-15 255 / 16 = 15 and 255 % 16 15 15/16=0and 15 % 16 = 15 Collect remainders for the hex values in reverse of these steps. 15 15 15 15 -> 0xFFFF This is a similar process for base-10, base-8 and base-2 conversions. Part 1: Converting data types (50 points) In this project, you will write code to convert strings containing hexadecimal. Decimal, octal and binary strings to an unsigned integer. Some of the code is completed for you. Open the Convert source code file and look at the completed code and comments. The description and comments in each function should help you complete this project. To convert from hex string to unsigned int, accumulate the position-wise values of each character in the string. Remember that you can perform math operations on char data because they are numbers and have specific positions in the ASCII table. This method is like the one shown in lecture. Convert OxFFFF to a unsigned char, considering that F 15 and each hex char a multiple of 16: 15 *115 1615 25615* 4096 65,535 This process is similar with decimal, octal and binary. To from an unsigned int 16,535 convert to hex: 1. 2. 3. 4. 65,535 / 16 = 4095 and 65,535 % 16-15 4,095 / 16 = 255 and 4095 % 16-15 255 / 16 = 15 and 255 % 16 15 15/16=0and 15 % 16 = 15 Collect remainders for the hex values in reverse of these steps. 15 15 15 15 -> 0xFFFF This is a similar process for base-10, base-8 and base-2 conversions

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transactions On Large Scale Data And Knowledge Centered Systems Xxxviii Special Issue On Database And Expert Systems Applications Lncs 11250

Authors: Abdelkader Hameurlain ,Roland Wagner ,Sven Hartmann ,Hui Ma

1st Edition

3662583836, 978-3662583838

More Books

Students also viewed these Databases questions

Question

When is it appropriate to use a root cause analysis

Answered: 1 week ago

Question

Evaluate employees readiness for training. page 275

Answered: 1 week ago