Question

In: Computer Science

Write the main.c program for a PSoC project that takes an input byte and converts it...

Write the main.c program for a PSoC project that takes an input byte and converts it to an integer value in base 10 for subsequent processing within the program as an integer. (Example, 0x5A = 9010).

Solutions

Expert Solution

0x5A hexadecimal equals to 90 in base 10.

conversion tales place: 5 * 16 ^ 1 + A * 16 ^ 0

where A is equals to 10.

First way: Using inbuilt C Feature.

This is a very simple task. C programming language provides inbuilt functionality of converting numbers to another form.

First task is to declare a variable which can hold hexadecimal value like 0x5A. Integer data type is capable of storing the hexadecimal value. So a variable can be declared as follows:

int hexInput;

Next task is to get get the input in hexadecimal format. scanf function with format specifier %x can be used to scan hexadecimal value.

scanf("%X", &hexInput);

Last task is to get the decimal value that is base 10 value. Simply you can assign the hexInput to another integer type variable or use hexInput directly. To print the hexInput in base10 use the %d format specifier with printf statement.

base10Value = hexInput;   
printf("Integer value: %d", base10Value);

where base10Value is an integer type variable.

Output:

Second way: Input in string form and converting to base 10

Take input in character array. Parse the input character by character. The input must start with 0x.

#include <stdio.h>
#include <string.h>
#include <math.h>

int main()
{
char hexInput[25], ch;
int base10Value = 0, length, i = 0;
  
// get the input from user
printf("Enter the hexadecimal value. Format should be 0x5A: ");
gets(hexInput);
  
// get the length of the input
length = strlen(hexInput);
  
// ignore first 2 characters. so loop till length is greater than 2.
while(length > 2)
{
// get the character begining from last
ch = hexInput[length - 1];
  
//decrement the length by 1
length--;
  
// if charcter is between 'A' and 'F' or 'a' and 'f', get the decimal value and add 10
// add 10 is required as (ch - 'A') will give differnce in ASCII value
if(ch >= 'A' && ch <= "F")
{
// i is used for power. as we are starting from last, i is initialized to 0
base10Value += pow(16, i) * (ch - 'A' + 10);
printf("\n%d", base10Value);
}
else if(ch >= 'a' && ch <= "f")
{
base10Value += pow(16, i) * (ch - 'a' + 10);
printf("\n%d", base10Value);
}
// if ch is digit, subtract '0' from ch to get decimal value
else if(ch >= '0' && ch <= '9')
{
base10Value += pow(16, i) * (ch - '0');
printf("\n%d", base10Value);
}
  
// increment power value
i++;
}
  
printf("Integer value: %d", base10Value);

return 0;
}

Output:


Related Solutions

Write a C function str_to_float() that takes a numeric string as input and converts it to...
Write a C function str_to_float() that takes a numeric string as input and converts it to a floating point number. The function should return the floating point number by reference. If the conversion is successful, the function should return 0, and -1 otherwise (e.g. the string does not contain a valid number). The prototype of the function is given below int str_to_float(char * str, float * number);
Write Java program that asks a user to input a letter, converts the user input to...
Write Java program that asks a user to input a letter, converts the user input to uppercase if the user types the letter in lowercase, and based on the letter the user the user enters, display a message showing the number that matches the letter entered. For letters A or B or C display 2 For letter D or E or F display 3 For letter G or H or I display 4 For letter J or K or L...
2. Write a C++ program that; Takes in the weight of a person in Kilograms, converts...
2. Write a C++ program that; Takes in the weight of a person in Kilograms, converts and outputs the equivalent weight in pounds. Format your output to 3 decimal places. Your output should look like this 53.000 Kg is equivalent to 123.459 Ibs (Note 1Kg = 2.2046226218488 lbs) Takes in the price of an item on an online store in pound sterling, converts and outputs the equivalent price in U.S dollars. Format your output to 2 decimal places. Your output...
Write a program that takes a date as input and outputs the date's season. The input...
Write a program that takes a date as input and outputs the date's season. The input is a string to represent the month and an int to represent the day. Ex: If the input is: April 11 the output is: Spring In addition, check if the string and int are valid (an actual month and day). Ex: If the input is: Blue 65 the output is: Invalid The dates for each season are: Spring: March 20 - June 20 Summer:...
Write a program that takes in a positive integer as input, and outputs a string of...
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x / 2 Note: The above algorithm outputs the 0's and 1's in reverse order. Ex: If the input is: 6 the output is: 011 6 in binary is...
IN PYTHON Write a program that takes in a positive integer as input, and outputs a...
IN PYTHON Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x // 2 Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a second function to reverse the string....
Write a program that takes in a line of text as input, and outputs that line...
Write a program that takes in a line of text as input, and outputs that line of text in reverse. The program repeats, ending when the user enters "Quit", "quit", or "q" for the line of text. Ex: If the input is: Hello there Hey quit then the output is: ereht olleH yeH IN C++ PLEASE!
Write a program that takes in a positive integer as input, and outputs a string of...
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x / 2 Note: The above algorithm outputs the 0's and 1's in reverse order. The code needs to be in Java
Write a short C++ program that takes all the lines input to standard input and writes...
Write a short C++ program that takes all the lines input to standard input and writes them to standard output in reverse order. That is, each line is output in the correct order, but the ordering of the lines is reversed. Please use vector datatype standaard library #include <vector> Thanks
Write a JAVA program called Convertor that has two methods. The first one converts an input...
Write a JAVA program called Convertor that has two methods. The first one converts an input binary into its equivalent decimal number. The second method converts a decimal number into its equivalent binary.​ binary2Decimal, decimal2Binary are the names for those methods.​ binary2Decimal receives a binary number as a string and returns a decimal number as an integer. And vice versa for the decimal2Binary method.​
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT