English 中文(简体)
Java: by
原标题:Java: Convert byte to integer
  • 时间:2010-12-20 18:38:12
  •  标签:
  • java

我需要将2个沥青阵列(每平方位数)转化为 j的惯性价值。 我如何能够这样做?

问题回答

http://download.oracle.com/javase/6/docs/api/java/nio/ByteBuffer.html”。

ByteBuffer buffer = ByteBuffer.wrap(myArray);
buffer.order(ByteOrder.LITTLE_ENDIAN);  // if you want little-endian
int result = buffer.getShort();

另见https://stackoverflow.com/q/2383265/139010> 4 载于

In Java, Bytes are signed, which means a byte s value can be negative, and when that happens, @MattBall s original solution won t work.

例如,如果 by形阵列的双轨形式一样:

101

然后,我的Array[0]为1000 1101,而我的Array ***为1000 1101, The decimal Value of byte 1000 1101-115,而不是141(= 2^7 + 2^3 + 2^2 + 2^0)

如果我们使用,

int results = (myArray[0] << 8) + myArray?

页: 1 该机构是非政府组织。

The reason why its wrong is that when we interpret a 2-byte array into integer, all the bytes are unsigned, so when translating, we should map the signed bytes to unsigned integer:

((myArray[0] & 0xff) << 8) + (myArray *** & 0xff)

结果是36237,使用计算器或Buffer检查其正确性(I已经做到正确),并且是正确的。

每一星体都是介质——128.127,因此,你需要一种办法,绘制一对一对ger的ger子。 这样做有多种方式,取决于你在tes子上所形成的情况。 最常见的做法是储存16倍的经签名的ger。 视你是否储存其大端:

(byte_array[0]<<8) + (byte_array[1] & 0xff)

或几乎没有结束语:

(byte_array[1]<<8) + (byte_array[0] & 0xff)

另外,如果你能够使用瓜瓦图书馆:

Ints.fromByteArray(0, 0, myArray[1], myArray[0]);

值得一提,因为很多项目都利用了它。

只是这样做:

return new BigInteger(byte[] yourByteArray).intValue();

“蓝色”指挥转换工程” 无需担心已签署或未签字的转换。

import java.io.*;
public class ByteArray {

    public static void main(String[] args) throws IOException {
        File f=new File("c:/users/sample.txt");
        byte[]b={1,2,3,4,5};
        ByteArrayInputStream is=new ByteArrayInputStream(b);
        int i;
        while((i=is.read())!=-1) {
            System.out.println((int)i); 
            FileOutputStream f1=new FileOutputStream(f);
            FileOutputStream f2=new FileOutputStream(f);
            ByteArrayOutputStream b1=new ByteArrayOutputStream();
            b1.write(6545);
            b1.writeTo(f1);
            b1.writeTo(f2);
            b1.close();
        }




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...