I have a problem converting a char array to a unsigned short (UInt16). My converting techniques seems to be wrong…
Here is the code:
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int _tmain(int argc, _TCHAR* argv[])
{
// Symbols 0x1210 :
char test[2];
test[0] = 0x12;
test[1] = 0x10;
unsigned short n;
memcpy(&n, test, sizeof(unsigned short));
int i=0, arrToInt=0;
for(i=1;i>=0;i--)
arrToInt =(arrToInt<<8) | test[i];
/*
Now are:
n = 4114
arrToInt = 4114
But! -> 0x1210 == 4624
*/
return 0;
}
Is there a way (without) reversing the char array?
Thanks for your help!
1 Answer