简体   繁体   English

将字符嵌入int中,反之亦然

[英]embedding chars in int and vice versa

I have smart card on which I can store bytes (multiple of 16). 我有智能卡,我可以存储字节(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. 如果我这样做: Save(byteArray, length)然后我可以做Receive(byteArray,length) ,我想我将按照我存储的相同顺序获取字节数组。 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.). 因此,我认为可能解决方案是我总是以一种小端方式将数据存储在此卡上,并始终以一种小端方式检索数据(我将编写应用程序进行读写,因此我可以自由地将数字解释为I喜欢。)。 Is this possible? 这可能吗? Here is something I have come up with: 以下是我提出的问题:

Embed integer in char array: 在char数组中嵌入整数:

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? 重要的是我认为无论机器的endiannes如何, LSB函数都应该返回最低有效字节,这样的LSB函数会是什么样子?

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? 这会有用吗?

The 'LSB' function may be implemented via a macro as below:- “LSB”功能可以通过宏实现如下: -

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

Provided x is unsigned. 如果x是未签名的。

If your C library is posix compliant, then you have standard functions available to do exactly what you are trying to code. 如果您的C库符合posix标准,那么您可以使用标准函数来完成您尝试编写的代码。 ntohl , ntohs , htonl , htons (network to host long, network to host short, ...). ntohlntohshtonlhtons (网络主机长,网络主机短,...)。 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. 这样,如果要为big-endian或little-endian架构编译代码,则无需更改代码。 The functions are defined in arpa/inet.h (see http://linux.die.net/man/3/ntohl ). 这些函数在arpa/inet.h中定义(参见http://linux.die.net/man/3/ntohl )。

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 ). 也就是说,当你知道有很多程序,以确定字节序( 123 )。 But it is the readers that would have to contain code to test endianess, not your card. 但是读者必须包含测试endianess的代码,而不是你的卡片。

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. 顺便说一下, HERE是一个相关的帖子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM