简体   繁体   English

在Java中,为什么我的字节数组的元素包含2个字节?

[英]In Java, Why do element of my byte array contain 2 bytes?

I am programming a little Java based Android app that receives ac based unsigned char[] byte array from a Bluetooth chip and streams it into a byte array. 我正在编写一个基于Java的小型Android应用程序,该应用程序从蓝牙芯片接收基于ac的无符号char []字节数组,并将其流式传输为字节数组。

The fact that it is Android and Bluetooth shouldn't matter though, it is just background. 虽然它是Android和蓝牙的事实并不重要,但这只是背景。 But, I am using minimum API 8 if that makes a difference. 但是,如果这有所作为,我将使用最低API 8。

The main code is: 主要代码是:

InputStream is = socket.getInputStream();
DataInputStream dis = new DataInputStream(is);
byte[] buffer = new byte[1024];
bytesRead = dis.read(buffer, 0, 1024);

However, when I look at the actual contents of the buffer, I see this: 但是,当我查看缓冲区的实际内容时,会看到以下内容:

[0] 15 [0xf] [^O]   
[1] 15 [0xf] [^O]   
[2] 0 [0x0] [^@ (NUL)]  
[3] -119 [137] [0x89] [^É]  
[4] 2 [0x2] [^B]    
[5] 6 [0x6] [^F]    
[6] 26 [0x1a] [^Z]  
[7] -47 [209] [0xd1] [Ñ]    
[8] -1 [255] [0xff] [ÿ] 
[9] 104 [0x68] [h]  
[10]    -1 [255] [0xff] [ÿ] 
[11]    -46 [210] [0xd2] [Ò]    
[12]    -1 [255] [0xff] [ÿ] 
[13]    104 [0x68] [h]  
[14]    -19 [237] [0xed] [í]    
[15]    -128 [128] [0x80] [^À]  

The above is a copy from the eclipse Expression view set to show 上面是从Eclipse Expression视图设置的副本,以显示

  1. Element [#] 元素[#]
  2. Signed int value 有符号的int值
  3. If negative, unsigned int value [This value does not appear unless #2 is negative] 如果为负,则为无符号int值[除非#2为负,否则该值不出现]
  4. Hex value 十六进制值
  5. ASCII value ASCII值

My question is, if this is a byte array, why are some of the Hex values containing 2 bytes. 我的问题是,如果这是一个字节数组,为什么某些十六进制值包含2个字节。 Look at elements 6 through 14, each of them are of the form 0x1a, 0x12, 0xff, etc. The bytes 0 through 5 are all one byte (except for element 3). 看元素6到14,每个元素的格式均为0x1a,0x12、0xff等。字节0到5都是一个字节(元素3除外)。

I don't think this is an issue from the Bluetooth side because I see the actual code being made, and its an unsigned char[] array, each element is only one byte. 我不认为这是蓝牙方面的问题,因为我看到了正在编写的实际代码以及它的无符号char []数组,每个元素只有一个字节。 Plus, I recall seeing something like this in a previous little project that involved taking data streams from an online API. 另外,我还记得在以前的一个小项目中看到过类似的事情,该项目涉及从在线API获取数据流。

How can I ensure the Java array elements contain only 1 byte? 如何确保Java数组元素仅包含1个字节? And for that matter, I feel like I am not understanding something important about Java since this perplexes me -- how can Java allow a byte array to contain more than one byte per element? 就此而言,我觉得我不了解Java的重要内容,因为这使我感到困惑– Java如何允许一个字节数组每个元素包含一个以上的字节?

You're confusing bytes and hexadecimal digits. 您混淆了字节和十六进制数字。

A byte contains 8 bits. 一个字节包含8位。 An hexadecimal digit goes from 0 to F, and is thus only 4 bits (16 values, 16 = 2^4, thus 4 bits). 十六进制数字从0到F,因此只有4位(16个值,16 = 2 ^ 4,因此是4位)。 A single byte is thus represented using two hexadecimal digits. 因此,使用两个十六进制数字表示一个字节。

0xf is equivalent to 0x0f . 0xf等效于0x0f The first 4 bits are all 0, and the last 4 bits are all 1. 前4位全为0,后4位全为1。

A byte is 8 bits and so can hold 0 - (2^8 - 1) ie 0-255 (0x0 - 0xff). 字节为8位,因此可以容纳0-(2 ^ 8-1),即0-255(0x0-0xff)。 All your values are a single byte. 您所有的值都是一个字节。

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

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