了解Java字节 [英] Understanding Java bytes

查看:151
本文介绍了了解Java字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此​​,在昨天的工作,我不得不写一个应用程序来计算在AFP文件中的页面。所以,我重新翻阅了我的MO:DCA规格PDF,发现结构化字段 BPG(开始页)和它的3个字节的标识符。该应用程序需要在AIX设备上运行,所以我决定把它写在Java中。

So at work yesterday, I had to write an application to count the pages in an AFP file. So I dusted off my MO:DCA spec PDF and found the structured field BPG (Begin Page) and its 3-byte identifier. The app needs to run on an AIX box, so I decided to write it in Java.

有关最高效率,我决定,我将读取的第6个字节的每个结构域,然后跳到在字段中剩余的字节。这将让我:

For maximum efficiency, I decided that I would read the first 6 bytes of each structured field and then skip the remaining bytes in the field. This would get me:

0: Start of field byte
1-2: 2-byte length of field
3-5: 3-byte sequence identifying the type of field

所以我检查字段类型,并增加一个页面计数器,如果是 BPG ,我不,如果它不是。然后,我跳过其余的字节在该领域,而不是通过他们读。在这里,在跳跃(真正的字段长度)是我发现Java使用符号字节。

So I check the field type and increment a page counter if it's BPG, and I don't if it's not. Then I skip the remaining bytes in the field rather than read through them. And here, in the skipping (and really in the field length) is where I discovered that Java uses signed bytes.

我做了一些谷歌搜索,发现了不少有用的信息。最有用的,当然是做一个按位&放指令; 0xFF的来获得无符号整型值。这是必要的,我让可能在计算字节跳过数使用的长度。

I did some googling and found quite a bit of useful information. Most useful, of course, was the instruction to do a bitwise & to 0xff to get the unsigned int value. This was necessary for me to get a length that could be used in the calculation for the number of bytes to skip.

我现在知道,在128,我们开始向后从-128计数。我想知道的是按位运算是如何在这里工作 - 更确切地说,我是如何在二进制重新presentation为负数到达

I now know that at 128, we start counting backwards from -128. What I want to know is how the bitwise operation works here--more specifically, how I arrive at the binary representation for a negative number.

如果我理解的按位&安培; 得当,你的结果等于其中只有你两个数字的普通位被设置一个数字。因此,假如字节B = -128 ,我们将有:

If I understand the bitwise & properly, your result is equal to a number where only the common bits of your two numbers are set. So assuming byte b = -128, we would have:

b & 0xff // 128

1000 0000-128
1111 1111 255
---------
1000 0000 128

所以,我将如何到达1000 0000 -128?我怎么会得到的东西再二元presentation像-72或-64不太明显?

So how would I arrive at 1000 0000 for -128? How would I get the binary representation of something less obvious like -72 or -64?

推荐答案

为了得到负数的二进制重新presentation你计算二的补

In order to obtain the binary representation of a negative number you calculate two's complement:


  • 获得正数的二进制重新presentation

  • 反转所有的位

  • 新增一个

让我们做-72为例:

0100 1000    72
1011 0111    All bits inverted
1011 1000    Add one

所以二进制(8位)再$ P $的-72 psentation是 10111000

什么是真正发生在你如下:您的文件中有值的字节 10111000 。当间preTED为一个无符号字节(这可能是你想要的),这是88。

What is actually happening to you is the following: You file has a byte with value 10111000. When interpreted as an unsigned byte (which is probably what you want), this is 88.

在Java中,当这个字节作为一个int(例如,由于阅读()返回一个int,或因隐促销),这将是间preTED作为一个符号字节,并签署扩展到 11111111 11111111 11111111 10111000 。这是与值-72的整数。

In Java, when this byte is used as an int (for example because read() returns an int, or because of implicit promotion), it will be interpreted as a signed byte, and sign-extended to 11111111 11111111 11111111 10111000. This is an integer with value -72.

通过AND运算与 0xFF的您只保留最低的8位,所以你的整数现在是 00000000 00000000 00000000 10111000 ,这是88

By ANDing with 0xff you retain only the lowest 8 bits, so your integer is now 00000000 00000000 00000000 10111000, which is 88.

这篇关于了解Java字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆