search

Home  >  Q&A  >  body text

java - 问一个比较基础的问题,为什么0x8FFFFFF 等于 128 MB ?

怎么换算的啊?进制数之间可以转换,为什么两个不同计量单位之间也可以转换啊?
代码如下:

import java.nio.*;
import java.nio.channels.*;
import java.io.*;
import static net.mindview.util.Print.*;

public class LargeMappedFiles {
  static int length = 0x8FFFFFF; // 128 MB
  public static void main(String[] args) throws Exception {
    MappedByteBuffer out =
      new RandomAccessFile("test.dat", "rw").getChannel()
      .map(FileChannel.MapMode.READ_WRITE, 0, length);
    for(int i = 0; i < length; i++)
      out.put((byte)'x');
    print("Finished writing");
    for(int i = length/2; i < length/2 + 6; i++)
      printnb((char)out.get(i));
  }
}
阿神阿神2889 days ago923

reply all(6)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 17:37:27

    It should be a clerical error. You can use a calculator to calculate that 128MB is 0x8000000, or you can test it yourself and the generated test.data will be 143MB

    RandomAccessFile randomAccessFile = new RandomAccessFile("test.data", "rw");
    FileChannel channel = randomAccessFile.getChannel();
    ByteBuffer buffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, 0x8FFFFFF);
    

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 17:37:27

    Convert to binary. . Count to 0

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 17:37:27

    Even if the unit of 0x8FFFFFF is B, it is about 144MB after conversion. 0x8000000 B is equal to 128MB

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 17:37:27

    It should be that the code and comments are out of sync. For example, it was originally 0x8000000, which is 128MB. Later, it was changed to 0x8FFFFFFF for some reason, but I forgot to change the comment. Or simply a clerical error. This feels like a typical "useless" comment.

    reply
    0
  • 迷茫

    迷茫2017-04-17 17:37:27

    0x8FFFFFF is just a number, without unit, how can it be equal to 128M

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 17:37:27

    This seems to be a problem about conversion between complement code, inverse code and source code.

    reply
    0
  • Cancelreply