Android:需要帮助理解变量赋值背后的 &0x04
想知道是否有人有一些见解或向我指出解释以下代码行的正确文档:
byte flag = (byte) (flagOfCardData&0x04);
我不知道为什么在 flagOfCardData 之后有 &0x04 。我知道它与十六进制有关,但仅此而已。这里是函数中的代码行。
public void onReceiveMsgCardData(byte flagOfCardData,byte[] cardData) {
// TODO Auto-generated method stub
byte flag = (byte) (flagOfCardData&0x04);
if(flag==0x00)
_strMSRData = new String (cardData);
if(flag==0x04)
{
//You need to dencrypt the data here first.
_strMSRData = new String (cardData);
}
_MSRData = null;
_MSRData = new byte[cardData.length];
System.arraycopy(cardData, 0, _MSRData, 0, cardData.length);
_isCardData = true;
handler.post(doHideTopDlg);
handler.post(doHideSwipeTopDlg);
handler.post(doUpdateTVS);
}
我会查找为什么要这样做,但我什至无法找出正确的搜索关键字 DOH!感谢您的任何和所有帮助。
这是使用 eclipse 和 java 的 Android sdk。此外,该代码是另一个 SDK 的一部分,该 SDK 是磁卡读卡器的一部分,通过音频端口连接到 Android 设备。该产品称为unimag pro。这是网站:http://www.idtechproducts.com/products/mobile-读者/126.html 我确实向他们发送了同样的问题,但谁知道他们是否以及何时会回复。我也会在这里发布他们的答案,供可能遇到同样问题的其他人使用。
如果您需要更多信息,请告诉我。
谢谢。
wondering if anyone might have some insight or point me to the proper documentation explaining the following line of code:
byte flag = (byte) (flagOfCardData&0x04);
I don't have any clue as to why there is the &0x04 after flagOfCardData. I know it has to do with something hexadecimal but that is it. Here it the line of code as it sits in the function.
public void onReceiveMsgCardData(byte flagOfCardData,byte[] cardData) {
// TODO Auto-generated method stub
byte flag = (byte) (flagOfCardData&0x04);
if(flag==0x00)
_strMSRData = new String (cardData);
if(flag==0x04)
{
//You need to dencrypt the data here first.
_strMSRData = new String (cardData);
}
_MSRData = null;
_MSRData = new byte[cardData.length];
System.arraycopy(cardData, 0, _MSRData, 0, cardData.length);
_isCardData = true;
handler.post(doHideTopDlg);
handler.post(doHideSwipeTopDlg);
handler.post(doUpdateTVS);
}
I would look up why to do this but I can't even figure out the right search keywords DOH! Thanks for any and all help.
This is for Android sdk using eclipse and java. Also, the code is part of another sdk that is part of a magnetic card reader that connects to an Android powered device through the audio port. The product is called unimag pro. Here is the website: http://www.idtechproducts.com/products/mobile-readers/126.html
I did send them the same question but who knows if and when they might respond. I will post their answer on here as well for anyone else that might come across the same problem.
Let me know if you need any more info.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的
flagOfCardData
是一个 8 位整数,但每个位都单独分配了一些独立的含义。就您而言,您仅想知道其第二位。为此,您需要使用 00000100b(即 4)执行按位与:现在,如果设置了第二位,则
X
为1
(1 & 1 = 1
),否则为0
(1 & 0 = 0
)。因此flagOfCardData & 0x04
相应地可以是 4 或 0。通常只对各个位使用十六进制表示法:
Your
flagOfCardData
is an 8-bit integer, but each bit individually is assigned some independent meaning. In your case, you only want to know its second bit. To do so, you perform bitwise-and with 00000100b, which is 4:Now
X
is1
if the second bit is set (1 & 1 = 1
), and0
otherwise (1 & 0 = 0
). ThusflagOfCardData & 0x04
is eiter 4 or 0 accordingly.It's customary to just use hexadecimal notation for the individual bits:
读作:
&
是“按位与”运算符。 0x04 是二进制 00000100 的十六进制。并且该位模式将挑选出flagOfCardData
变量的单个位,我认为它是一组标志。如果设置了标志,则
flag == 0x04
。如果未设置,则flag == 0x00
。这两种可能性在下面的 if 语句中进行检查。从评论来看,这是“加密数据”标志。Read this as:
The
&
is the "bitwise and" operator. 0x04 is the hex for binary 00000100. And'ing that bit pattern will pick out a single bit of theflagOfCardData
variable, which I assume is a set of flags.If the flag is set then
flag == 0x04
. If it is not set thenflag == 0x00
. These two possibilities are checked in theif
statements below. From the look of the comment, this is the "encrypted data" flag.该代码测试 flagOfCardData 的位 2 是否已设置。值 0x4 是二进制 00000100。“&”运算符执行按位 AND,这意味着将两个操作数字节的每个位进行 AND 运算以生成字节大小的结果(与逻辑 AND 相对)。
根据注释,如果标志不为零(即设置了位 2),该函数应该解密数据。
The code tests whether bit 2 of flagOfCardData is set. The value 0x4 is binary 00000100. The '&' operator performs bitwise AND which means AND-ing each of the bits of the two operand bytes together to produce a byte sized result (as opposed to logical AND).
According to the comments, the function is supposed to decrypt the data if flag is not zero, i.e. bit 2 was set.