Addressing Byte Arrays for Data Access

So apparently this works:

Float to Byte Array
1
2
3
4
5
6
// float f = ...
char b[4];
b[0] = ((char*)(&f))[0];
b[1] = ((char*)(&f))[1];
b[2] = ((char*)(&f))[2];
b[3] = ((char*)(&f))[3];


Byte Array to Float
1
2
3
4
5
6
// char b[4] = {...}
float f;
((char*)(&f))[0] = b[0];
((char*)(&f))[1] = b[1];
((char*)(&f))[2] = b[2];
((char*)(&f))[3] = b[3];


I'm guessing that the memcpy() approach saves output code... and textual code. Do you think it's best just to use a memcpy() function? This probably isn't worth my time, is it... ? I guess it might be more efficient (execution wise) and useful (programmer utility) than memcpy() if I made functions like float ByteArrayToFloat(const char* byteArray), right?

Also, what do you think about:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
uint StringLength(const char* string)
{
	if (string)
	{
		const char* start = string;

		while(*string != '\0')
		{
			++string;
		}

		return (uint)(string - start); // Particularly this line.
	}
}


Is there something wrong with "(uint)(string - start)" ? I can't remember.
Last edited on
Topic archived. No new replies allowed.