English 中文(简体)
C#和java之间的产出差异
原标题:Difference in outputs between C# and java
  • 时间:2012-04-09 07:47:30
  •  标签:
  • c#
  • java

我正试图在C#中写出一个相当于 Java的职能。 守则如下。

C#:

byte[] a = new byte[sizeof(Int32)];
readBytes(fStream, a, 0, sizeof(Int32)); //fstream is System.IO.Filestream
int answer = BitConverter.ToInt32(a, 0);

Java:

InputStream fstream = new FileInputStream(fileName);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
byte[] a = new byte[4];
readBytes(in, a, 0, 4);
int answer = byteArrayToInt(a);

Java和C#:

int readBytes(Stream stream, byte[] storageBuffer, int offset, int requiredCount)
        {
            int totalBytesRead = 0;
            while (totalBytesRead < requiredCount)
            {
                int bytesRead = stream.Read(
                                storageBuffer,
                                offset + totalBytesRead,
                                requiredCount - totalBytesRead);
                if (bytesRead == 0)
                {
                    break; // while
                }

                totalBytesRead += bytesRead;
            }

            return totalBytesRead;
        }

产出:

C#: answer = 192  (Correct)  
In JAVA: answer = -1073741824 

两者有所不同。 我正在从一个文件输入流中阅读,该目录编码并按头四个字标。 C#代码似乎产生了192条,这是正确的答案,而Java却产生了1073741824条错误答案。 为什么?

http://www.ohchr.org。

这里,我 Array ToInt

public static int byteArrayToInt(byte[] b, int offset) {
        int value = 0;
        for (int i = 0; i < 4; i++) {
            int shift = (4 - 1 - i) * 8;
            value += (b[i + offset] & 0x000000FF) << shift;
        }
        return value;
    }

SOLUTION The right solution for byteArrayToInt

public static int byteArrayToInt(byte[] b) 
    {
        long value = 0;
        for (int i = 0; i < b.length; i++)
        {
           value += (b[i] & 0xff) << (8 * i);
        }
        return (int) value;
    }

这使产出正确。

最佳回答

在java的 by子里,你——64岁 by的 by子是两倍的,在 c(192=256 - 64)中相当于192。

问题在<代码>byte ArrayToInt()中出现,如果你在转换过程中假定不签名的话。

简单

 `b & 0x000000FF`

在这种情况下可能会有所帮助。

问题回答

Java s byte object is signed as soulcheck wrote. The binary value for 192 on an unsigned 8 bit integer would be 11000000.

如果你用签字格式阅读这一数值,则主要1表示负数。 这意味着11000000改为negative 01000000,即-64.





相关问题
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 ...

热门标签