convert UCHAR array to UINT array
-
Hi! I need to convert UCHAR array to UINT array. Example:
0 8 16 24 32 0 32 +------+------+------+------+ +------------+
INDATA | 0x11 | 0x22 | 0x33 | 0x44 | == CONVERT ==> | 0x11223344 | OUTDATA
+------+------+------+------+ +------------+I have this two functions.
/* put byte to dword */
UINT uchar2uint(UCHAR * in)
{
return ((*in << 24) | (*(in + 1) << 16) | (*(in + 2) << 8) | *(in + 3));
}/* put byte array to dword array */
void uchar2uinta(UCHAR * in, UINT * out, size_t outlen)
{
while(outlen--)
{
*out++ = uchar2uint(in);
in += 4;
}
}int main(void)
{
int i;UCHAR indata\[8\] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88 }; UINT outdata\[2\]; uchar2uinta(indata, outdata, 2); for(i = 0; i < 8; ++i) printf("%#x ", indata\[i\]); printf("\\n"); for(i = 0; i < 2; ++i) printf("%#x ", outdata\[i\]); printf("\\n"); return 0;
}
at the console displays the following text:
0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x88
0x11223344 0x55667788It's OK! But, i can have a problems, when working in other system with different endianness (big-endian or little-endian)? May be existed more correct way to convert? Sorry for my poor English :) Best regards, Alexander S.
-
Hi! I need to convert UCHAR array to UINT array. Example:
0 8 16 24 32 0 32 +------+------+------+------+ +------------+
INDATA | 0x11 | 0x22 | 0x33 | 0x44 | == CONVERT ==> | 0x11223344 | OUTDATA
+------+------+------+------+ +------------+I have this two functions.
/* put byte to dword */
UINT uchar2uint(UCHAR * in)
{
return ((*in << 24) | (*(in + 1) << 16) | (*(in + 2) << 8) | *(in + 3));
}/* put byte array to dword array */
void uchar2uinta(UCHAR * in, UINT * out, size_t outlen)
{
while(outlen--)
{
*out++ = uchar2uint(in);
in += 4;
}
}int main(void)
{
int i;UCHAR indata\[8\] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88 }; UINT outdata\[2\]; uchar2uinta(indata, outdata, 2); for(i = 0; i < 8; ++i) printf("%#x ", indata\[i\]); printf("\\n"); for(i = 0; i < 2; ++i) printf("%#x ", outdata\[i\]); printf("\\n"); return 0;
}
at the console displays the following text:
0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x88
0x11223344 0x55667788It's OK! But, i can have a problems, when working in other system with different endianness (big-endian or little-endian)? May be existed more correct way to convert? Sorry for my poor English :) Best regards, Alexander S.
What you have done will work in big- or little-endian systems. It is correct. There may be more *efficient* ways to do the conversion, depending on quirks of the hardware, compilers, etc. Regards, Peter
Software rusts. Simon Stephenson, ca 1994.
-
What you have done will work in big- or little-endian systems. It is correct. There may be more *efficient* ways to do the conversion, depending on quirks of the hardware, compilers, etc. Regards, Peter
Software rusts. Simon Stephenson, ca 1994.
-
Hi! I need to convert UCHAR array to UINT array. Example:
0 8 16 24 32 0 32 +------+------+------+------+ +------------+
INDATA | 0x11 | 0x22 | 0x33 | 0x44 | == CONVERT ==> | 0x11223344 | OUTDATA
+------+------+------+------+ +------------+I have this two functions.
/* put byte to dword */
UINT uchar2uint(UCHAR * in)
{
return ((*in << 24) | (*(in + 1) << 16) | (*(in + 2) << 8) | *(in + 3));
}/* put byte array to dword array */
void uchar2uinta(UCHAR * in, UINT * out, size_t outlen)
{
while(outlen--)
{
*out++ = uchar2uint(in);
in += 4;
}
}int main(void)
{
int i;UCHAR indata\[8\] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88 }; UINT outdata\[2\]; uchar2uinta(indata, outdata, 2); for(i = 0; i < 8; ++i) printf("%#x ", indata\[i\]); printf("\\n"); for(i = 0; i < 2; ++i) printf("%#x ", outdata\[i\]); printf("\\n"); return 0;
}
at the console displays the following text:
0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x88
0x11223344 0x55667788It's OK! But, i can have a problems, when working in other system with different endianness (big-endian or little-endian)? May be existed more correct way to convert? Sorry for my poor English :) Best regards, Alexander S.
fasked wrote:
void uchar2uinta(UCHAR * in, UINT * out, size_t outlen) { while(outlen--) { *out++ = uchar2uint(in); in += 4; } }
Try this instead, its simpler and will do the same you are looking for void uchar2uinta(UCHAR * in, UINT * out, size_t outlen) { while(outlen--) { *out++ = *(UINT*)in; in += 4; } }
HARSH Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning.
-
fasked wrote:
void uchar2uinta(UCHAR * in, UINT * out, size_t outlen) { while(outlen--) { *out++ = uchar2uint(in); in += 4; } }
Try this instead, its simpler and will do the same you are looking for void uchar2uinta(UCHAR * in, UINT * out, size_t outlen) { while(outlen--) { *out++ = *(UINT*)in; in += 4; } }
HARSH Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning.
-
fasked wrote:
void uchar2uinta(UCHAR * in, UINT * out, size_t outlen) { while(outlen--) { *out++ = uchar2uint(in); in += 4; } }
Try this instead, its simpler and will do the same you are looking for void uchar2uinta(UCHAR * in, UINT * out, size_t outlen) { while(outlen--) { *out++ = *(UINT*)in; in += 4; } }
HARSH Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning.