简体   繁体   English

如何在BluetoothChat中从输入/输出流读取/写入原始十六进制字节?

[英]How do I read/write raw hex bytes from input/output stream in BluetoothChat?

I'm developing an app based on the BluetoothChat sample code. 我正在开发一个基于BluetoothChat示例代码的应用程序。 I need to be able to write a byte array containing hex values to the output stream. 我需要能够将包含十六进制值的字节数组写入输出流。 I also need to parse through the byte array on the inputstream and read the hex values. 我还需要解析输入流上的字节数组并读取十六进制值。 Here's my code just to simply write hex values to the byte array 这是我的代码,只是简单地将十六进制值写入字节数组

byte[] data = new byte [3];
data[0] = (byte) 0x53;
data[1] = (byte) 0x1C;
data[2] = (byte) 0X06;

However, when stepping through debug and watching "data", the debugger shows the values as data[0]=83, data[1]=28, data[2]=6. 但是,当单步执行调试并观察“数据”时,调试器会将值显示为data [0] = 83,data [1] = 28,data [2] = 6。 Why are all the values converted to ascii? 为什么所有值都转换为ascii? The same thing is happening when I watch byte[] buffer when reading the inputstream. 当我在读取输入流时观察byte []缓冲区时,会发生同样的事情。

 // Read from the InputStream
 bytes = mmInStream.read(buffer);

I sent a byte array containing hex values, but byte[] buffer is showing ascii values. 我发送了一个包含十六进制值的字节数组,但是byte []缓冲区显示了ascii值。 How can I read and write the raw hex values over bluetooth? 如何通过蓝牙读取和写入原始十六进制值? Some of the read/write streams will be short <15byte stream for commands. 对于命令,一些读/写流将短<15byte流。 But I'll also need to read a large file encoded as hex bytes. 但我还需要读取一个编码为十六进制字节的大文件。 Any suggestions for that input stream? 对该输入流的任何建议? Do I need to use BufferedInputStream? 我需要使用BufferedInputStream吗?

When you write 当你写作

int answer = 42;
System.out.println(answer);

The int value stored in the answer variable is converted to a String and then displayed on your monitor. 存储在answer变量中的int值将转换为String ,然后显示在监视器上。 The same applies to displaying the value in your IDE's debugger. 这同样适用于在IDE的调试器中显示值。 Eventually, all numerical data is converted to ASCII (or some other character encoding) in order to display it. 最终,所有数值数据都将转换为ASCII(或其他一些字符编码)以显示它。 This is true in any programming language. 在任何编程语言中都是如此。

Now we might wonder how does this conversion occur. 现在我们可能想知道这种转换是如何发生的。 By default, the int value is converted to its decimal representation. 默认情况下, int值将转换为其十进制表示形式。 This is what most humans are used to reading and expect to see. 这是大多数人习惯阅读和期望看到的东西。 As programmers, we sometimes need to think of the hexadecimal representation of an int value, such as you are doing in your code. 作为程序员,我们有时需要考虑int值的十六进制表示,例如您在代码中所做的。 However, the computer does not automatically know this just because we set the value of a variable using a hexadecimal representation. 但是,计算机不会自动知道这一点,因为我们使用十六进制表示设置变量的值。 In particular, your IDE still defaults to displaying all int data in its decimal representation. 特别是,IDE仍然默认以十进制表示形式显示所有int数据。

Fortunately, IDE designers are programmers and realize that we sometimes need to see int data in a different form. 幸运的是,IDE设计人员是程序员,并意识到我们有时需要以不同的形式查看int数据。 You can change the setting for your debugger to display int data as hexadecimal. 您可以更改设置为你的调试器显示int为十六进制数据。 Since the steps to do this depends on your IDE, you should check the help files for details. 由于执行此操作的步骤取决于您的IDE,因此您应该查看帮助文件以获取详细信息。

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

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