Hello,

I made the following code to convert known datatypes to bytes:

template <class T>
void convertToByte(const T *t, PBYTE pbArray)
{
	DWORD dwSize = sizeof(T);
	memset(pbArray, 0, dwSize);
	memcpy_s(pbArray, dwSize, reinterpret_cast<LPCVOID>(t), dwSize);
}

But i was wondering how i'd go about it without passing a datatype into the array. i'd like to create a function that i could just define a size and value and convert it to bytes.

So when i pass DWORD (unsigned int) which is 4 bytes holding a value of 3234.
Becomes;
1 Byte - 162
1 Byte - 12
1 Byte - 0
1 Byte - 0

So i'm wondering how 162 and 12 are converted to 3234?

Thanks guys.

So i'm wondering how 162 and 12 are converted to 3234?

Don't forget that the 12 is your second byte, so it needs to be appropriately shifted: (12 << 8) | 162 == 3234 .

Don't forget that the 12 is your second byte, so it needs to be appropriately shifted: (12 << 8) | 162 == 3234 .

Thanks, that's great. I did some more intensive research with bitwise operator and found a solution how to turn the value into bytes;
http://stackoverflow.com/questions/2693182/dword-to-bytes-using-bitwise-shift-operators

I wrote a function to convert the size and value to bytes.

Solution to anyone that's interested:

void convertToByte(const DWORD dwLen, DWORD const dwVal, PBYTE pbArray)
{
	memset(pbArray, 0, dwLen);

	DWORD dwBitSize = 0;
	for (DWORD dw = 0; dw < dwLen; ++dw)
	{
		pbArray[dw] = (dwVal >> dwBitSize);
		dwBitSize = (dw + 1) * 8;
	}
}

Thanks once again.

Unless you need to force endianness, there's no need for bitwise operators at all. You can pun the value into an "array" of bytes with a simple cast:

#include <cstddef>
#include <iostream>

int main()
{
    int value = 3234;
    unsigned char *p = (unsigned char*)&value;
    
    for (std::size_t i = 0; i < sizeof(value); i++)
        std::cout << (int)p[i] << '\n';
}

Yeah, but i won't always know the datatype but i'll always know the data length. Hense why i developed it without using a datatype.

Basically my main goals are to develop plugin or tool for memory scanning array of datatypes. Since CheatEngine only supports byte arrays only, which would be time eating converting each datatype you require to a byte.

So, if i convert any data length to bytes, then i can search for unknown datatypes.
So if a user of my tool wants to scan for custom (unknown datatype) they can just define the data length.

Attachments 1.png 33.09 KB

Yeah, but i won't always know the datatype but i'll always know the data length. Hense why i developed it without using a datatype

I suspect you don't understand my suggestion, otherwise you'd realize that you don't need to know the source data type for it to work. Technically you don't even need to know the data length, but it certainly helps when accessing bytes in a way that avoids out of bounds accesses.

I suspect you don't understand my suggestion, otherwise you'd realize that you don't need to know the source data type for it to work. Technically you don't even need to know the data length, but it certainly helps when accessing bytes in a way that avoids out of bounds accesses.

Ok, if you check my screenshot provided in previous post, i'll select datatype, e.g. 4 bytes from combobox then type in a value as just text. So really the value are stored in a char array then i'll convert what 4 byte with that certain value will be into byte array, if that make sense.

For example;

char szExample[5] = "1234" // Let's say the datatype is 4 bytes.

Convert "1234" string into number then to bytes which would be; 210, 4, 0, 0.

Make sense why i can't use your previous suggestion?

Edited 4 Years Ago by some_coder: n/a

Make sense why i can't use your previous suggestion?

I love how you basically changed the requirements without anyone knowing unless they reverse engineered your code from the screenshot. :icon_rolleyes:

I love how you basically changed the requirements without anyone knowing unless they reverse engineered your code from the screenshot. :icon_rolleyes:

Yeah, sorry i didn't well describe my requirements.

My thread was asking 2 questions.

Question 1:
How that; 162, 12, 0, 0 converted to 3234.
You solved that.

Question 2:
But i was wondering how i'd go about it without passing a datatype into the array. i'd like to create a function that i could just define a size and value and convert it to bytes.
Which I resolved myself after reading more into bitwise operator.

Edited 4 Years Ago by some_coder: n/a

This article has been dead for over six months. Start a new discussion instead.