3

I have smart card on which I can store bytes (multiple of 16). If I do: Save(byteArray, length) then I can do Receive(byteArray,length) and I think I will get byte array in the same order I stored. Now, I have such issue. I realized if I store integer on this card, and some other machine (with different endianness) reads it, it may get wrong data. So, I thought maybe solution is I always store data on this card, in a little endian way, and always retrieve the data in a little endian way (I will write apps to read and write, so I am free to interpret numbers as I like.). Is this possible? Here is something I have come up with:

Embed integer in char array:

int x;
unsigned char buffer[250];

buffer[0] = LSB(x);
buffer[1] = LSB(x>>8);
buffer[2] = LSB(x>>16);
buffer[3] = LSB(x>>24);

Important is I think that LSB function should return the least significant byte regardless of the endiannes of the machine, how would such LSB function look like?

Now, to reconstruct the integer (something like this):

int x = buffer[0] | (buffer[1]<<8) | (buffer[2]<<16) | (buffer[3]<<24);

As I said I want this to work, regardless of the endiannes of the machine who reads it and writes it. Will this work?

  • 1
    dmcr_code - ***[this link](http://stackoverflow.com/q/746171/645128)*** does not directly answer _this_ question, but I think should be very interesting to you because of its extensive discussion on endianess, bit-swapping and byte order. – ryyker Oct 12 '13 at 15:40
  • I'll look at that, I found also some other useful link and I will look at that. –  Oct 12 '13 at 15:57

3 Answers3

1

The 'LSB' function may be implemented via a macro as below:-

#define LSB(x) ((x) & 0xFF)

Provided x is unsigned.

Siddhartha Ghosh
  • 2,988
  • 5
  • 18
  • 25
  • will this LSB correctly work both on little endian and big endian machines? I am not that experienced with such things yet, that is why I asked –  Oct 12 '13 at 15:24
  • 3
    The macro operates on values, not storage. Values do not have endianness. – Raymond Chen Oct 12 '13 at 15:33
  • 3
    The only time you have to worry about endianness or bit pattern representations is if you are doing things like casting pointers to the values or reading from unions (all of which have undefined behaviour). Usual arithmetic operations (including "bitwise" operations like `&`) are defined in terms of the *meanings* of the values only, and not their representation in the hardware. – Gavin Smith Oct 12 '13 at 15:37
  • Keep in mind, endianess refers to how bytes are ordered within computer memory, and (as has been said) nothing to do with the value presented in a variable. The variable will always present itself with its value, whatever it may be, regardless of machine type because of your compiler. – ryyker Oct 12 '13 at 15:46
  • a layout of an integer 1 in memory will be like this: 00 00 00 01 if this is big endian and like this: 01 00 00 00 if this is little endian –  Oct 12 '13 at 15:55
  • @RaymondChen: can you please elaborate what you mean with: "The macro operates on values, not storage. Values do not have endianness."?? –  Oct 15 '13 at 13:42
  • Endianness is a property of storage, not of numbers. Storage has endianness. Numbers do not. If you store the number into memory, now you have endianness. "How was this number stored into memory?" It's like listening to somebody talking and asking, "Is he talking using American or British spelling?" He's not talking with any spelling. Spelling doesn't come into play until you write the words downs onto a piece of paper. – Raymond Chen Oct 15 '13 at 21:39
1

If your C library is posix compliant, then you have standard functions available to do exactly what you are trying to code. ntohl, ntohs, htonl, htons (network to host long, network to host short, ...). That way you don't have to change your code if you want to compile it for a big-endian or for a little-endian architecture. The functions are defined in arpa/inet.h (see http://linux.die.net/man/3/ntohl).

Étienne
  • 4,773
  • 2
  • 33
  • 58
0

I think the answer for your question is YES, you can write data on a smart card such that it is universally (and correctly) read by readers of both big endian AND little endian orientation. With one big caveat: it would be incumbent on the reader to do the interpretation, not your smart card interpreting the reader, would it not? That is, as you know there are many routines to determine endianess (1, 2, 3). But it is the readers that would have to contain code to test endianess, not your card.

Your code example works, but I am not sure it would be necessary given the nature of the problem as it is presented.

By the way, HERE is a related post.

Community
  • 1
  • 1
ryyker
  • 22,849
  • 3
  • 43
  • 87