Store each byte from 8-byte value in array [closed] - c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 5 years ago.
Improve this question
I have a large array of bytes called memory and a uint64_t value called valA. I want each byte in valA to be stored in a position in memory (each position in memory holds one byte). The position in memory I'm starting from is 3832.
This is what I've coded so far:
uint64_t valA = 81985529216486895;
memory[3832] = valA;
When I print out each position in memory:
printf("number in memory - %d%d%d%d%d%d%d%d",
memory[valE+0], memory[valE+1], memory[valE+2], memory[valE+3],
memory[valE+4], memory[valE+5], memory[valE+6], memory[valE+7]);
The output is "number in memory- 2390000000". I want the output to be the original number that was stored in valA. Any suggestions?

I want the output to be the original number that was stored in valA
It is not going to work with decimal output, because decimal representation does not break at byte's boundary. You can get it to work with hexadecimal notation, though:
long long unsigned int valA = 81985529216486895LL;
uint8_t memory[5000];
uint32_t valE = 3832;
memcpy(&memory[valE], &valA, sizeof(valA));
printf(
"%02x%02x%02x%02x%02x%02x%02x%02x\n"
, memory[valE+7]
, memory[valE+6]
, memory[valE+5]
, memory[valE+4]
, memory[valE+3]
, memory[valE+2]
, memory[valE+1]
, memory[valE+0]
);
Demo
Note that the bytes are ordered in reverse to match the ordering on the demo system.

Related

Convert multiple bytes array into single long variable [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Im getting 10 bytes of data in an char array like which contains hex value
Data1[0] = 0x00,Data1[1] = 0x00,Data1[0] = 0x9 Data1[2]=0x01and so on...
Now I want to get this different array bytes into single long variable . Like
Long_var = 091...
How can do it any method can be accepted.
Sorry, i forgot to mention, i want to do this in 8051 code
There are generally two ways to do type punning in C, both involving arrays.
The first is to use a plain array of 32-bit integers, and then copy the bytes into that array:
char data[12];
// data is initialized...
uint32_t integers[3];
memcpy(integers, data, 12);
printf("First value is 0x%08x\n", integers[0]);
The other way is to use unions:
union type_punning_union
{
uint32_t integers[3];
char data[12];
};
union type_punning_union u;
// Initialize u.data...
printf("First value is 0x%08x\n", u.integers[0]);
Big important note 1: Your byte array have a size mismatch for matching all data evenly to 32-bit integers.
Big important note 2: The code shown above doesn't care about endianness, meaning the results printed might not be exactly what you expect.

how much space use an array variable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
In c ,How much memory consume an array,That is only one single array
ie,int a[0]; or char a[0];
I want to know it when the program writes on a paper ,not at program running on compiler
Here I cant use sizeof function , my compiler is avrgcc ,
In the part of my program some where I require an array of int a[13];only
or Instead of int a[13]; an int a[3]; along with an integer type additionally ie, int i.
specifically I require
if i require 13 integer array or 4 integer array along with an integer variables.
which is less memory used
The size of an array is the sum total of the size of each element in the array.
For example,
if the array size is 5
the array element (type) size is 4 bytes
The whole array would consume (size * sizeof individual element), i.e., in this case 5 * 4 == 20 bytes.
This is irrespective of the usage, i.e., how many elements you actually plan to use.
FWIW, a 0-size/ 0-length array is non-standard. It's a gcc extension for a particular purpose (before the addition of flexible array member as a standard) that supports a 0-sized array, but you better not reply on it.
An int (integer) type variable has a size of 2 bytes, a char has 1 byte. In an array the size of array multiplied by size of variable according to its type will give you the size of the array.
And you can use the sizeof also.

C - Read .doc file as 32 bit binary numbers [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
It's just a practice task , as "Every file is just a binary file with different set of bytes." , i am reading a .doc file as binary file, let suppose
"this .doc file is actually a binary file with 32-bit unsigned numbers and i want to sort them as ascending within the file."
Now so far my logic is to count total bytes from file divide them by sizeof(int) , and make int array of that size. and start reading bytes . but there is a problem in this logic :
file.doc with size 250 bytes where sizeof(int) = 4 , now 250/4 = 62.5 , i made an array of 63 integers , now how will i handle 63rd integer , as 32 bit binary number ?
Do something like rem = filesize % sizeof(int) to get the number of bytes after the last contiguous block of sizeof(int) bytes, and for the last integer in the array do fread(&array[i], rem, 1, stream).
Usually these problems are handled by adding padding. You can pretend that the original file has been filled with zeros (for example) until the next multiple of 4 bytes.

How to take input from file containing string and integer? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I am trying to get an input from a text file,whose name is of the format
(without spaces) .Eg, 3shop where 3 is number of inputs in file.
How do I extract this number from the string ?
Also content of the file is of same format and variable length.
**i.e complete 3shop.txt is of the form
1soap 3toothpate 5biscuits
8biscuits
9toothpaste 5 soap
There is no limit on size of integer,otherwise i could have used an array and extracted the number.
Please, suggest some good ways to do this.
Thanks in advance.
char name[32] = "3shop.txt", *p = name;
long id;
id = strtol(name, &p, 10);
printf("id=%ld name=%s\n", id, p);
10 in strtol means base 10 (decimal)
After the call to strtol, p points to the next character in name after the numerical value

How do you create an array of a certain size using malloc() in C? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Part of the program I am writing requires an array to be created using malloc instead of the regular way of doing it. I have to have the user enter a number, assign that number the name MAX, and create an array using malloc() with numbers 2 through the number entered. How would I go about coding this?
You create an "array" with malloc() by specifying the size (in bytes) of the array and assigning the return value to a pointer of the appropriate type. If you're intending for this to be an array of objects that are larger than one byte, you can multiply the number of objects by the size of the object, which can be obtained with the sizeof operator.
For example, you can create an "array" of fifty int objects like so:
int *ar = malloc(50 * sizeof (int) );
You can do that by:
T *dynamic_memory;
....... //Get the desired array size from user input and store in 'array_max_size'
dynamic_memory=malloc((sizeof(T) * array_max_size);
T : data type of array
You can then use dynamic_memory for your purpose.

Resources