copying integers from char array depending on number of bytes in C
NickName:sats Ask DateTime:2015-04-01T07:04:38

copying integers from char array depending on number of bytes in C

I have one character array of 8 bytes containing integer values. I need to copy the 1 byte to one integer variable, next 4 bytes to different integer variable, next 3 bytes to another integer variable. I have used "memcpy" but the results are not proper.

My try:

    unsigned char bytes[8];
    int Data1 = 32769;
    int Data2 = 65535;

    int logic1 = 0;
    int logic2 = 0;
    int logic3 = 0;

    memcpy(&bytes[0], &Data1, sizeof(Data1));
    memcpy(&bytes[4], &Data2, sizeof(Data2));

//Value of bytes[] array after this operation is
//bytes[0] = 1
//bytes[1] = 128
//bytes[2] = 0
//bytes[3] = 0
//bytes[4] = 255
//bytes[5] = 255
//bytes[6] = 0
//bytes[7] = 0



    memcpy(&logic1,&bytes,1);
    memcpy(&logic2,&bytes + 1,4);
    memcpy(&logic3,&bytes + 5,1);

My output should be :
logic1 = bytes[0]
logic2 = bytes[1] to bytes[4]
logic3 = bytes[5] to bytes[7]

Copyright Notice:Content Author:「sats」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/29380065/copying-integers-from-char-array-depending-on-number-of-bytes-in-c

More about “copying integers from char array depending on number of bytes in C” related questions

copying integers from char array depending on number of bytes in C

I have one character array of 8 bytes containing integer values. I need to copy the 1 byte to one integer variable, next 4 bytes to different integer variable, next 3 bytes to another integer varia...

Show Detail

C: unsigned char * bytes array copying elements to other unsigned char * array

I think that copying elements of array like this: unsigned char *new_bytes_array = malloc(sizeof(unsigned char)*length/2); for(int i=0, j=0; i<length; i++) { if(i % 2 == 0) continue;

Show Detail

How to convert a Byte Array to an Int Array without copying

There is already a solution but it involves memory copying. I want a solution that does not involve memory copying. In this scenario, it is guaranteed that the input byte array (byte[]) must consis...

Show Detail

Copying Bytes from Memory to char array then writing the bytes to a registry key via Driver C

I am having an issue with my project where I am attempting to copy a structure from memory. Copying the structure into a char array and writing the bytes in the char array to a registry key of type

Show Detail

size in bytes of a char number and int number

please tell me if I am wrong, if a number is stored as a character it will contain 1 byte per character of the number(not 4 bytes)? for example if I make an int variable of the number 8 and a char

Show Detail

Copying unsigned char in C

I want to use memcpy but it seems to me that it's copying the array from the start? I wish to copy from A[a] to A[b]. So, instead I found an alternative way, void copy_file(char* from, int offse...

Show Detail

Print a Char Array of Integers in C

For class, I am required to create a function that converts an Integer into it's corresponding Binary number. However, I am forced to use the given main and parameters for the to_binary function. The

Show Detail

Storing Integers in an Char Array in C

Im doing some work for Uni and wrote a programm which stores Integers in an char Array and converts them to their ASCII Values and prints them at the end. My code did not work before and only started

Show Detail

Turn time char array into integers in C

I don't use C regularly so this is kind of tricky for me even though I'm sure it's a pretty simple task. I've been trying to research how to do this and I don't think I'm phrasing it correctly ever...

Show Detail

How can bytes in char array represent integers?

So let's say I have char array that I read from binary file (like ext2 formatted filesystem image file). Now I need to read integer starting at offset byte 1024(<--that's the offset from start o...

Show Detail