15,884,298 members
1.00/5 (1 vote)
See more:
Hi,
I have this array :
C++
``BYTE set[6] = { 0x15, 0x12, 0x84, 0x03, 0x00, 0x00 }``
and i need to insert this value : int Value = 900; ....on last 4 bytes. Practically to convert from int to hex and then to write inside the array...
Is this possible ?

I already have "BitConverter::GetBytes" function, but that's not enough :(

Thank you,
Posted
pasztorpisti 11-Jul-13 12:16pm
If we assume that int is 32 bit then we can say `*(int*)&set[sizeof(set)-sizeof(int)] = Value;`. This is ugly but if you want to do this...
Dholakiya Ankit 13-Jul-13 5:30am
int value = 900;
BYTE* pValue=(BYTE*)&value;

## Solution 1

To convert an int to 4 bytes, you do not need to go through hex :

2 solutions,
1) use your int as a byte array :
C++
```int value = 900;
BYTE* pValue=(BYTE*)&value;
//pValue[0], pValue[1], pValue[2] & pValue[3] will contain the 4 bytes.```

2) use shift on your int in a loop :
C++
```int value = 900;
int tmp=value;
for(int i=0;i<4;++i)
{
initialized_byte_array[i]=(tmp& 0xFF);
tmp=tmp>>8;
}```

The tmp var is used to keep value intact.

with
C++
`#define BYTE unsigned char`

or
C++
`#define BYTE char`

v2
lewax00 11-Jul-13 12:32pm
I think that covers most of the options, +5.

I'm a little hazy on the details, but could you also use a union for this?
Pascal-78 11-Jul-13 15:21pm
the union could be :
typedef union { int ivalue; char bvalues[4]; } theUnion;

theUnion v;
v.ivalue; // To Get or Set the int value
v.bvalues[0 to 3]; // To Get or Set the byte values
vmvictor 11-Jul-13 13:45pm
Ok, i have fix it :)
Great job Pascal-78, was so simple and i lost 2 days reading all over :))
One more lesson learned .
Thank you,
vmvictor 11-Jul-13 14:18pm
If i want to put this "int value = 900;" into ""cout << 'enter your value' <<endl; - cin >> value"....i need a function for this too ?
Pascal-78 11-Jul-13 15:15pm
You're right, to let the user enter the value, you just need to have the value variable declared as an int and use the instructions you mentioned :
int value;
cout << "Enter your value" << endl;
cin >> value;

## Solution 4

I have two solutions which are based on the copying the contents ... so please check if it suits to your purpose...

Solution 1

You can use stringstream for this purpose.
What you will need to do is ... write it once .... and read it 8 times....

Pseudo code will be like this
C++
```int value=900;
stringstream stream;
stream.write( (char*)&value, sizeof( value ) );   ///Writing it once

BYTE bytes[4];
for ( int i = 0; i < 4; i++ )
{
stream.read( &bytes[i], sizeof( BYTE ) );

} ```
Solution 2
Second solution is ..... copy whole memory to the byte array

C++
```int value=900;
BYTE bytes[4];
memcpy( bytes, value, 4 );```

vmvictor 12-Jul-13 10:16am
I already found the solution...posted by Pascal-78, but thank you for your solution.

Now, i have a new problem and i will post it soon :)
vmvictor 12-Jul-13 11:40am
Hi again :)
I have a new problem now...i discovered that i need 2 values not just one, to make it work.
When i press a key, the program must switch between this two values.
With switch case, or with (GetAsyncKeyState(VK_TAB) & 1) is the best to do it ?
And is this function working in this case ?
Posted by Pascal-78 : int tmp = atkSpd; for(int i=6; i<10; ++i) { byt[i] = (tmp & 0xFF); tmp = tmp >> 8; }
Or i need something else to get the job done ?

Thank you,

## Solution 5

I prefer this approach - entirely with pointers and casting. This lets the compiler do all the work and removes the need for byte copying. It will work no matter what the size of set or int.
This will always put the int value at the end of the BYTE array. Reading back is just the inverse.
To be completely robust there should be a check that set is large enough for an int.

C++
```typedef unsigned char BYTE;

BYTE set[6] = { 0x15, 0x12, 0x84, 0x03, 0x00, 0x00 };

int inValue = 900;

// Write
* ((int *) (set + (sizeof(set) - sizeof(int)))) = inValue;

int outValue = * ((int *) (set + (sizeof(set) - sizeof(int))));```

C++
```BYTE set[16]={0xC7,0x80,0xEA,0x04,0x00,0x00,0xB0,0x04,0x00,0x00,0x8B,0xE5,0x5D,0xC2,0x08,0x00 };

// Write
* ((int *) (set + 5)) = inValue;

int outValue = * ((int *) (set + 5));```

Read up on pointers. Also if you run this code in a debugger you can verify that it works.

v4
vmvictor 13-Jul-13 1:14am
And how this function can modify only the 6th,7th,8th,9th bytes from this array : BYTE set[16]={ 0xC7,0x80,0xEA,0x04,0x00,0x00,0xB0,0x04,0x00,0x00,0x8B,0xE5,0x5D,0xC2,0x08,0x00 }; ?
[no name] 13-Jul-13 3:28am
vmvictor 13-Jul-13 5:45am
Returns value = 0 ......
[no name] 13-Jul-13 10:04am
That statement makes no sense. I have done you the courtesy of answering an extra question. Where's your code?
vmvictor 13-Jul-13 13:48pm
BYTE atkByt[30] = { 0xC7,0x83,0x1A,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x48,0x8B,0x74,0x24,0x40,
0x48,0x8B,0x6C,0x24,0x48,0x48,0x8B,0x5C,0x24,0x50,0x48,0x83,0xC4,0x58,0xC3 };

int tempA = atkSpd; for(int c=6; c<10; ++c) { atkByt[c] = (tempA & 0xFF); tempA = tempA >> 8; } // Write 8 bytes starting with 6th position from left to right. (solution posted by Pascal-78 earlier). Thank you,