the hex value 00 00 00 00 00 BC 61 4E (LSB)

which is stored in string as

char data[8] = {0x4E, 0x61, 0xBC, 0x00,
0x00, 0x00, 0x00, 0x00};

now i want to convert it into int
(equivalent of above hex string is 12345678)

i have below code

char a = 0x00;
char b = 0x00;
char c = 0x00;
char d = 0x00;
char e = 0x00;
char f = 0xbc;
char g = 0x61;
char h = 0x4e;

long long int combination = (a << 56) |(b << 48) | (c << 40) | (d << 32) |
(e << 24) | (f << 16) | (g << 8) | (h);

cout << combination << endl << endl;

but i'm getting undefined result

help me..!!!

Edited by archana.c07: n/a

2
Contributors
1
2
Views
8 Years
Discussion Span
Last Post by firstPerson

Try doing this :

``````int64 bigEndian(short* hex, int size){
if(size > 18 || size < 1) return -1; //int64 cannot hold anymore
int64 res = int64();
for(int i = 0; i != size-1; ++i){
//OR and SHIFT as we go
res |= hex[i];
res <<= 8;
}
//Just OR the last value since it does not need to be shifted, thats why our loop went from i = 0 to size-1.
res |= hex[size-1];
return res;
}
int main(){
short hexValues[8] = {0x00,0x00,0x00,0x00,0x00,0xBC,0x61,0x4E};
cout  << bigEndian(hexValues,8) << endl;
}``````

Edited by firstPerson: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.