Question

In: Computer Science

C language problem. Suppose I open I file and read a integer 24. And I want...

C language problem.

Suppose I open I file and read a integer 24. And I want to store them into a array byte [].

The answer should be byte[0] = 0x9F.

How can I do that?

Solutions

Expert Solution

#include <stdio.h>

int main(void) {
        int x = 24;

        // in C, there is no byte type, however char is of 1
        // byte, hence we can treat it same way.
        unsigned char byteArr[4];

        byteArr[0] = x & 0xFF;
        x = x >> 8; // shift to right by 8 bits.
        
        byteArr[1] = x & 0xFF;
        x = x >> 8; // shift to right by 8 bits.
        
        byteArr[2] = x & 0xFF;
        x = x >> 8; // shift to right by 8 bits.
        
        byteArr[3] = x & 0xFF;
        
        printf("0th byte(hex): %X\n", byteArr[0]);
        printf("1st byte(hex): %X\n", byteArr[1]);
        printf("2nd byte(hex): %X\n", byteArr[2]);
        printf("3rd byte(hex): %X\n", byteArr[3]);
}
**************************************************
0x9F is equal to 16*9 + 15 = 159 in decimal.. So Your answer is incorrect. 24 is equal to 18 => 16 + 8 = 24 in decimal.


Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

For c language. I want to read a text file called input.txt for example, the file...
For c language. I want to read a text file called input.txt for example, the file has the form. 4 hello goodbye hihi goodnight where the first number indicates the n number of words while other words are separated by newlines. I want to store these words into a 2D array so I can further work on these. and there are fewer words in the word file than specified by the number in the first line of the file, then...
I have a C problem. I need to read the data of a txt file to...
I have a C problem. I need to read the data of a txt file to array by struct, then use these data to sum or sort. The txt file and the struct like aa.txt 1 2 3 4 ***************************** struct aaa{ int num1,num2; }bbb[2]; num1 for 1,3, num2 for 2 4 I made a void readfile () function for read data. How can I pass the numbers to another function ( void sum () and void sort() ) for...
C++ language There are 100 integers in the inputfile.txt. Read each integer in the inputfile.txt and...
C++ language There are 100 integers in the inputfile.txt. Read each integer in the inputfile.txt and store each one in the array named myArray. After that, complete the code in the function printArray(...) to output the elements in the array with the following format: - Each output line should consist of 10 integers - Each integer should be separated by | The output should be exactly as shown Array contains the following elements 12 | 32 | 34 | 56...
so i want to read in a file. just to keep it simple, the text file...
so i want to read in a file. just to keep it simple, the text file has student name and the grade, separated by a space. let's say there are 192 student's name and I want to read them in and made them into a Student /object class, which has a constructor, student(string name, int grade). individually creating them seems pain in the ass. reading in should work for any number of names. So how do I do this in...
Please Use Python 3.The Problem: Read the name of a file and then open it for...
Please Use Python 3.The Problem: Read the name of a file and then open it for reading. Read the name of another file and then open it for output. When the program completes, the output file must contain the lines in the input file, but in the reverse order. • Write the input, output, and the relationship between the input and output. • Develop the test data (A single test will be alright). Make the input file at least 3...
I made the command cp code in c language. But when I copy a file, file...
I made the command cp code in c language. But when I copy a file, file permissions are not copied equally. So I want to copy the file authority as well. What should I do? #include #include #include #include #include #include #include #include #include int main(int argc, char *argv[]) { struct stat st; char ch; int src, dst; if(argc != 3) { printf("argument error \n"); printf("usage: ./a.out src dest \n"); exit(0); } src = open(argv[1], O_RDONLY); if(src == -1){ perror("open...
Present a screenshot of the following Program to open up the created file in C++ language....
Present a screenshot of the following Program to open up the created file in C++ language. The list of random numbers will be below the instructions I have provided for you a file named Random.txt for use in this program. This file contains a long list of random numbers. You are to write a program that opens the file, reads all the numbers from the file and calculates the following: A. The number of numbers in the file B. The...
C++ Code While Loops. Ask user for file and open file. Do priming read and make...
C++ Code While Loops. Ask user for file and open file. Do priming read and make a while loop that: 1. Reads in numbers. 2. Counts how many there is. 3. Also for every 10 numbers in the file, print out the average of those 10 numbers. Ex: (If 20 numbers in the file. "With 10 numbers the average is .... and With 20 numbers the average is" and EX for a file with 5 numbers "There are 5 numbers...
Query the user for the name of a file. Open the file, read it, and count...
Query the user for the name of a file. Open the file, read it, and count and report the number of vowels found in the file. Using C++.
Write a C++ program to open and read a text file and count each unique token...
Write a C++ program to open and read a text file and count each unique token (word) by creating a new data type, struct, and by managing a vector of struct objects, passing the vector into and out of a function. Declare a struct TokenFreq that consists of two data members: (1) string value; and (2) int freq; Obviously, an object of this struct will be used to store a specific token and its frequency. For example, the following object...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT