Android:需要帮助理解变量赋值背后的 &0x04

发布于 2024-11-28 15:24:57 字数 1328 浏览 0 评论 0原文

想知道是否有人有一些见解或向我指出解释以下代码行的正确文档:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜柠檬 2024-12-05 15:24:57

您的 flagOfCardData 是一个 8 位整数,但每个位都单独分配了一些独立的含义。就您而言,您想知道其第二位。为此,您需要使用 00000100b(即 4)执行按位与:

Mystery byte:  ? ? ? ? ? ? ? ? 
We want bit 2: 0 0 0 0 0 1 0 0
               -----------------< bitwise AND
Result         0 0 0 0 0 X 0 0

现在,如果设置了第二位,则 X1 (1 & 1 = 1),否则为 0 (1 & 0 = 0)。因此flagOfCardData & 0x04 相应地可以是 4 或 0。

通常只对各个位使用十六进制表示法:

Bit 0: 0x01   Bit 4: 0x10
Bit 1: 0x02   Bit 5: 0x20
Bit 2: 0x04   Bit 6: 0x40
Bit 3: 0x08   Bit 7: 0x80

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:

Mystery byte:  ? ? ? ? ? ? ? ? 
We want bit 2: 0 0 0 0 0 1 0 0
               -----------------< bitwise AND
Result         0 0 0 0 0 X 0 0

Now X is 1 if the second bit is set (1 & 1 = 1), and 0 otherwise (1 & 0 = 0). Thus flagOfCardData & 0x04 is eiter 4 or 0 accordingly.

It's customary to just use hexadecimal notation for the individual bits:

Bit 0: 0x01   Bit 4: 0x10
Bit 1: 0x02   Bit 5: 0x20
Bit 2: 0x04   Bit 6: 0x40
Bit 3: 0x08   Bit 7: 0x80
浪荡不羁 2024-12-05 15:24:57

读作:

byte flag = (byte) (flagOfCardData & 0x04);

& 是“按位与”运算符。 0x04 是二进制 00000100 的十六进制。并且该位模式将挑选出 flagOfCardData 变量的单个位,我认为它是一组标志。

如果设置了标志,则flag == 0x04。如果未设置,则 flag == 0x00。这两种可能性在下面的 if 语句中进行检查。从评论来看,这是“加密数据”标志。

Read this as:

byte flag = (byte) (flagOfCardData & 0x04);

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 the flagOfCardData variable, which I assume is a set of flags.

If the flag is set then flag == 0x04. If it is not set then flag == 0x00. These two possibilities are checked in the if statements below. From the look of the comment, this is the "encrypted data" flag.

新人笑 2024-12-05 15:24:57

该代码测试 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文