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.
Related
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.
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.
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 6 years ago.
Improve this question
SOLVED BELOW
I'm writing an IP forwarding program and I'm trying to read the header data.
I have structs for different lines such as this for the first line:
struct line1 {
char a; //version
char b; //header length
unsigned short c; //datagram length
};
The different data types are dependent on the length of the data field.
I have variable initialization:
struct line1 l1 = {};
FILE *ip_packets, *routing_table;
My professor showed a simple read function that was something like read(ip_packets, 4, l1) (4 Bytes) that automatically put the data into the struct fields. I have searched around the web and haven't found a simple method like this. What read function am I looking for?
I've tried fscanf in this way:
if (fscanf(ip_packets, "%c %c %hu", &l1.a, &l1.b, &l1.c)){
printf("%c\n", l1.a);
printf("%c\n", l1.b);
printf("%hu\n", l1.c);
}
I've also tried syntaxt %c,%c,%hu or %c/%c/%hu
but that just prints:
Kendalls-Mac-mini:Programming 2 kendallweihe$ ./ip_read
E
0
SOLUTION
Turns out I was reading it in correctly, but I needed to print the integer value. My testing verification is in terms of integers. Easy enough.
BETTER SOLUTION
fread(&l1, 4, 1, ip_packets);
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 9 years ago.
Improve this question
is there any way how to put integer sized 100 000 into 4 elements of char array? If I use sprintf or itoa, array has 6 elements. I tried to use this, but it didnt work. And is there any way how to put these 4 elements back to integer?
char *s;
int value = 100000;
*((int *)s)=value;
Note that:
int value = 100000;
char *s;
*((int *)s)=value;
dereferences uninitialized pointer s, which causes undefined behavior. You could do:
int value = 100000;
char s[4];
*((int *)&s[0])=value;
just note that this stores value in the memory block "occupied" by charr array (at memory level) unlike sprintf, which would print the value in a form of string (characters representing the number).
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 9 years ago.
Improve this question
In C I wish to create a data type (like int or float) that has only 1byte. How can I possible do this? I've tried with malloc() but didn't work that way I tried.
Could you please give me a hand here?
Example:
sizeof(int) = 4 bytes
sizeof(char) = 1 byte
sizeof(float) = 4 bytes
sizeof(myDataType) = 1 byte
There is such data type. char is always guaranteed to be one byte long. If you want another name for that type, just use typedef and create a new type based on char.
More detailed explanation can be found in this question: Are there machines, where sizeof(char) != 1, or at least CHAR_BIT > 8?