简体   繁体   English

将字节转换为单个int Android

[英]Converting byte to a single int Android

I am implementing a Bluetooth Android application, where Image is sent from one device to another. 我正在实现一个蓝牙Android应用程序,其中图像从一台设备发送到另一台设备。 The bitmap's byte array is sent and successfully reconstructed at the receiver end. 位图的字节数组已发送,并在接收器端成功重建。 However, I need to send a single integer value together with the bitmap as an index(so the receiver knows what to do with the received bitmap). 但是,我需要将单个整数值与位图一起发送作为索引(因此接收方知道如何处理接收到的位图)。 So basically I want to send this in a byte stream: 所以基本上我想以字节流发送它:

int|bitmap 整数|位图

Since I need to transfer an int up to 27, that means it fits into a single byte, right? 由于我需要传输一个最大为27的int,这意味着它可以容纳一个字节。 My current code looks like this: 我当前的代码如下所示:

ba[0] = Integer.valueOf(drawableNumber).byteValue(); //drawableNumber value is between 1 and 27
 ByteArrayOutputStream bs = new ByteArrayOutputStream();  //create new output stream
 try {
     bs.write(ba);         //bytes of the integer
     bs.write(bitmapdata); //bytes of the bitmap
     bs.toByteArray()      // put everything into byte array
}

 mChatService.write(bs.toByteArray()); // that is where bytes are sent to another device

And at the receiver end: 在接收端:

 case MESSAGE_READ:
    readBuf = (byte[]) msg.obj;  // readBuf contains ALL the received bytes using .read method

So my question is, how can I reconstruct the integer and the image I have sent(basically a single byte to a single integer)? 所以我的问题是,如何重建整数和已发送的图像(基本上是一个字节到一个整数)? I manage to reconstruct the bitmap alone, but I need this additional integer value to know what to do with the received image. 我设法单独重建位图,但是我需要这个附加的整数值才能知道如何处理接收到的图像。 The integer value will always be between 0 and 27. I have checked all other answers, but could not find a proper solution.. 整数值将始终在0到27之间。我已经检查了所有其他答案,但找不到合适的解决方案。

EDIT: Main question is how to separate the integer bytes from the bitmap bytes in the byte array. 编辑:主要问题是如何从字节数组中的位图字节分离整数字节。 Because at the receiving end I want to reconstruct the sent integer AND the bitmap separately 因为在接收端,我想分别重构发送的整数和位图

When I try this in java, it simply tells me what my mind was thinking when I commented. 当我在Java中尝试时,它只是告诉我在评论时我在想什么。 An integer is represented as a byte (so in your case, it is simply ba[0]). 整数表示为一个字节(因此,在您的情况下,它只是ba [0])。 Or it should be based on your code. 还是应该基于您的代码。 Any more than that and it would be a long. 除此之外,还很长。 That means it is also the first byte that will get read out of your buffer (or should be). 这意味着它也是将从缓冲区(或应该从缓冲区)中读取的第一个字节。

import java.io.ByteArrayOutputStream;


public class TestClass {
    public static void main(String args[]){
        byte[] ba = new byte[10];
        int myInt = 13;
        ByteArrayOutputStream bs = new ByteArrayOutputStream();  //create new output stream

        try {
           bs.write(myInt);         //bytes of the integer
           ba = bs.toByteArray();      // put everything into byte array
        } finally{};

        for(int i = 0; i < ba.length; i++){
          System.out.println(i);
          System.out.println(ba[i]);
        }
    }
}

Again I realize this isn't exactly an answer, just too much for a comment. 再一次,我意识到这并不是一个完全的答案,只是评论太多了。

由于将字节转换为int是一种低估,因此您只需将字节分配给int变量即可。

int myInt = ba[0];

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

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