0

I am new to the endians. I am trying to convert a big endian integer array to hexadecimal string in little endian. I tried to convert big endian integer array to little endian hexa decimal string. But at the end, getting big endian hexa decimal encoded string instead of little endian. Please help me

gint16 frame[5] = {10, -26, 35, 7, -35};  //big endian 
gint16 frame_i[5];
size_t i;
for (i= 0; i < 5; i++) {
  frame_i[i] = (frame[i] << 8) | ((frame[i] >> 8) & 0xFF);  //big endian to little endian
}
char *str = malloc(5 * 4 + 1);
size_t j;
for (j= 0; j < 5; j++) {
    snprintf(str + j * 4, 5, "%04X", frame_i[j] & 0xFFFF); // getting big endian  instead of little endian
}
Abu
  • 213
  • 5
  • 14
  • I cannot imagine why you need a little-endian string. Text representations of numbers are always m.s. digit first. Endianness has nothing to do with how arrays are organised: the issue is the byte sequence in memory that a multi-byte value is naturally stored by the processor. Please [read about endianness](https://en.wikipedia.org/wiki/Endianness). – Weather Vane Nov 15 '16 at 16:58
  • Moreover this line `(frame_i[i] << 8) | ((frame_i[i] >> 8) & 0xFF);` has no effect - the expression's value is not assigned. – Weather Vane Nov 15 '16 at 17:02
  • Please post the [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) that will avoid silly mistakes in the question, leaving doubt there might be more transcription errors. – Weather Vane Nov 15 '16 at 17:06
  • sorry. will check in future post – Abu Nov 15 '16 at 17:10
  • @WeatherVane "Text representations of numbers are always m.s. digit first." --> How about a text representation like "fourteen" (four + ten)? ;-) – chux - Reinstate Monica Nov 15 '16 at 17:38
  • @chux you got me there, also `four-and-twenty` but you know what I meant. Even Arabic which is written right-to-left, writes numbers left-to-right. But the Romans didn't. Anyway `four + ten` are not digits, they are words. – Weather Vane Nov 15 '16 at 17:43
  • @WeatherVane That's a [big 10-4](http://english.stackexchange.com/q/66948/51392) good buddy. – chux - Reinstate Monica Nov 15 '16 at 17:49
  • @chux `10-10` with your `20/20`. – Weather Vane Nov 15 '16 at 17:56

1 Answers1

1

The expression (frame_i[i] << 8) | ((frame_i[i] >> 8) & 0xFF); has no effect (you would have noticed that, if you had used -Wall compiler option). Also, you should apply this operation on the frame variable, and store the result in frame_i, e.g.:

int16_t frame[5] = {10, -26, 35, 7, -35};
int16_t frame_i[5] = {0};
size_t i;

for (i = 0; i < sizeof(frame) / sizeof(int16_t); i++) {
  frame_i[i] = (frame[i] << 8) | ((frame[i] >> 8) & 0xFF);
}

for (i = 0; i < sizeof(frame) / sizeof(int16_t); i++) {
  printf("%04X -> %04X\n", frame[i] & 0xFFFF, frame_i[i] & 0xFFFF);
}

Output

000A -> 0A00
FFE6 -> E6FF
0023 -> 2300
0007 -> 0700
FFDD -> DDFF

Note, as @WeatherVane has pointed out, the right shift of a negative signed integer has implementation-defined behavior. See answers to this question. Consider using an unsigned type, e.g. uint16_t from stdint.h.

Community
  • 1
  • 1
Ruslan Osmanov
  • 20,486
  • 7
  • 46
  • 60