Question

In: Computer Science

How can I create a hexadecimal number in c code from a specific array. For example...

How can I create a hexadecimal number in c code from a specific array. For example I have a[4]={2,5,7,4}; and I want to create this number : 0x004725 by adding 1 number at a time using << operation. Thank you!

Solutions

Expert Solution

yur quetion is vague as per my understanding i am giving you various cases in my answer hopin that one of these will meet ur requirement

case 1:

converting a decimal to hexadecimal c code

#include<stdio.h>
int main() {
   long int decimalNumber,remainder,quotient;
   int i=1,j,temp;
   char hexadecimalNumber[100];
   printf("Enter any decimal number: ");
   scanf("%ld",&decimalNumber);
   quotient = decimalNumber;
   while(quotient!=0) {
       temp = quotient % 16;
       //To convert integer into character
       if( temp < 10)
       temp =temp + 48; else
       temp = temp + 55;
       hexadecimalNumber[i++]= temp;
       quotient = quotient / 16;
   }
   printf("Equivalent hexadecimal value of decimal number %d: ",decimalNumber);
   for (j = i -1 ;j> 0;j--)
   printf("%c",hexadecimalNumber[j]);
   return 0;
}

case 2: << is left shift operator we cannot add numbers with that instead it operates on the binary values of given number

example to explain <<

#include<stdio.h>

int main()
{
int a = 60;

printf("\nNumber is Shifted By 1 Bit : %d",a << 1);
printf("\nNumber is Shifted By 2 Bits : %d",a << 2);
printf("\nNumber is Shifted By 3 Bits : %d",a << 3);

return(0);
}

output is

Number is Shifted By 1 Bit : 120
Number is Shifted By 2 Bits : 240
Number is Shifted By 3 Bits : 480

case 3:

if you want to form a hexadecimal from the numbers in array as given then simple output formaing is sufficient

char arr[10]

arr[0]='0'

arr[1]='x'

arr[2]='0'

j=3;

for(i=0;i<4;i++)

{

arr[j]=(char)a[i%4]; #random value from array

j++

}


Related Solutions

How can i modify my c code so that each number stored in the array is...
How can i modify my c code so that each number stored in the array is not the array index but the value of the array index converted to radians. I have already made a function for this converion above main(). Below is the code: #include <stdio.h> #include <stdlib.h> #include <math.h> float Deg2Rad (float degrees) { // Calculate & return value float Result; Result = ((M_PI*degrees)/180.0); return (Result); } int main(void) { // Declare variables int Array[90]; int i; //...
can you please create the code program in PYTHON for me. i want to create array...
can you please create the code program in PYTHON for me. i want to create array matrix Nx1 (N is multiple of 4 and start from 16), and matrix has the value of elements like this: if N = 16, matrix is [ 4 4 4 4 -4 -4 -4 -4 4 4 4 4 -4 -4 -4 -4] if N = 64, matrix is [8 8 8 8 8 8 8 8 -8 -8 -8 -8 -8 -8 -8...
How can I check that if a string only make from specific character? For example, I...
How can I check that if a string only make from specific character? For example, I want to check if a string only make from ICDM characters. It pass the test if it makes from ICMD. It fail the test if it makes from any special character like @#$& or lower case. Example inputs: if string = CIM, it passed if string = AIM, it failed if string = MCDI, it passed if string = ICDF, it failed This can...
How do I write a C# and a C++ code for creating a character array containing...
How do I write a C# and a C++ code for creating a character array containing the characters 'p', 'i', 'n','e','P','I','N','E' only and then using these lower and capital case letter character generate all possible combinations like PInE or PinE or PIne or PINE or piNE etc. and only in this order so if this order is created eg. NeIP or EnPi or NeIP or IPnE and on. You can generate all the combinations randomly by creating the word pine...
c++ I need a code that will fill an array size of 1000, an array of...
c++ I need a code that will fill an array size of 1000, an array of size 2000, and an array size of 10000, with random int values. Basically like this: array1[1000] = filled all with random numbers array2[2000] = filled all with random numbers array3[10000] = filled all with random numbers C++ no need for print
few problems example of array and 2d array and the solution code in java language. I...
few problems example of array and 2d array and the solution code in java language. I am new to java and trying to learn this chapter and it is kinda hard for me to understand.
Hi, I am wondering how to create code that will allow (in c) the console to...
Hi, I am wondering how to create code that will allow (in c) the console to read a password from a .txt file, and have the ability to change this password? I have a function that logs certain users in using simple if statements and hardcoded passwords, but i would like the ability to be able to change the password and read it from a .txt so it is editable instead of having it in a function in the code....
(In C) Note: Can you create an example code of these tasks. use any variables you...
(In C) Note: Can you create an example code of these tasks. use any variables you wish to use. postfix expressions: (each individual text (T), (F), (NOT), etc is a token) F T NOT T F NOT T T T AND F T F NAND (1) Create stack s. (2) For each token, x, in the postfix expression: If x is T or F push it into the stack s. (T = true, F = false) Else if x is...
C programming What is an array? Explain by taking an example that how can we take...
C programming What is an array? Explain by taking an example that how can we take input and output in 1D array?
(code in C++ language) [Code Bubble sort, Insertion sort Create a Big array with random numbers....
(code in C++ language) [Code Bubble sort, Insertion sort Create a Big array with random numbers. Record the time. Run Bubble Check time (compute the processing time) do it 100 times (random numbers) Take the average Insertion: Compare] (some explanations please)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT