-1

I have this Hexadecimal enumeration of 8 bytes:

enum MyEnum
{
    OPTION_1        = 0x000000;
    OPTION_2        = 0x000001;
};

Is there any way to add OPTION_1to the following pointer (this pointer will contain other binary data):

unsigned char* MyArray; // bytes array

EDIT I appologize for the unclear expression of "to add". Well, if I had

enum MyEnum
{
    OPTION_1        = 0x00;
    OPTION_2        = 0x01;
};

Then the values are of this enum are of size 1 byte. And so after memory allocation of the pointer, I can simply assign as follows:

MyArray[0]=OPTION_1;

But now, the problem is that OPTION_1 is of size 8 bytes and not 1 byte. And so I can't do that simple assignment.

I should have for assignments, but I don't know the values:

 MyArray[0]=?; // copy the first byte of OPTION_1
 MyArray[1]=?; // copy the second byte of OPTION_1
 MyArray[2]=?; // ...
 MyArray[3]=?; // ...
 MyArray[4]=?;
 MyArray[5]=?;
 MyArray[6]=?;
 MyArray[7]=?;

Thank you a lot!

Farah
  • 2,469
  • 5
  • 31
  • 52
  • 1
    In what sense is `MyEnum` 8 bytes? And what do you mean by "add"? The expression `MyArray + OPTION2` is perfectly legal, but I can't tell whether that's what you want. – Keith Thompson Oct 22 '14 at 16:27
  • `enum` is usually regarded by the compiler as `int` (perhaps it's even a part of the C language standard). – barak manos Oct 22 '14 at 16:40
  • I've done an EDIT. Thank you @KeithThompson – Farah Oct 22 '14 at 16:54
  • An enumeration constant like `OPTION_1` is of type `int`, which is most commonly 4 bytes, not 8. Is there some `sizeof ...` expression that yielded a value of `8` for you? `OPTION_1` is a value, not an object; what do you mean by its first, second, ... byte? (For an 8-bit integer with the value `0x01`, typically either the first or the last byte will have the value 1, depending whether the system is little-endian or big-endian). Your question is still unclear. – Keith Thompson Oct 22 '14 at 17:24
  • Perhaps what you want is something like `uint64_t foo = OPTION_1;` (which will *convert* the value of `OPTION_1` to the 8-byte type `uint64_t`), and you then want to copy the bytes of `foo` into `MyArray`? – Keith Thompson Oct 22 '14 at 17:25
  • What is a hex enumeration value? Values are just values, hex, decimal are ways to *print* values. `16` and `0x10` are the *same* value, just two different forms to write it down. – Jens Gustedt Oct 22 '14 at 19:45

1 Answers1

1

You have to be more specific about what you mean by "add OPTION_1 to the following pointer". Adding to a pointer can mean:

  1. increment the address of the pointer

    MyArray += OPTION_1; // this moves the address of the pointer by OPTION_1

  2. increment the value in the memory address pointed to by the pointer (ie. dereference the pointer and increment)

    *MyArray += OPTION_1; // this will try to add OPTION_1 to the value at the address

And if you're referring the the second then you're in a bit of trouble because your pointer references memory in increments of 8 bits (unsigned char) and your OPTION_1 is defined as a 32-bit integer (although only the first 24 are used).

If you'd like to add the individual bytes of OPTION_1 to the first 3 bytes at the address pointed to by MyArray, you could do so like this:

  *MyArray = (unsigned char)((OPTION_1 >> 16) & 0xFF);
  *(MyArray+1) = (unsigned char)(OPTION_1 >> 8) & 0xFF;
  *(MyArray+2) = (unsigned char)(OPTION_1 & 0xFF);

Of course, you'd have to be careful to ensure that MyArray points to a location with at least 3 bytes that you can manipulate.

Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151
  • Hello! I applogize for the unclear expression. I've done an edit, and I think your last suggestion the valid one, except that I need to copy all the 8 bytes into the pointer whose memory is already allocated. – Farah Oct 22 '14 at 16:58