Java:Java中的ord PHP等方法

Java:Java中的ord PHP等方法,java,php,Java,Php,我想编辑一个使用ord方法的php代码,php代码如下: $fh=fopen("../ip.data","r"); fseek($fh,327680); $country_id=ord(fread($fh,1)); //return 137 我尝试用java编写代码: FileReader ip=new FileReader("ip.data"); int i = 0; int str; while((str=ip.read()) != -1){ if(i==327681){

我想编辑一个使用ord方法的php代码,php代码如下:

$fh=fopen("../ip.data","r");
fseek($fh,327680);
$country_id=ord(fread($fh,1));  //return 137
我尝试用java编写代码:

FileReader ip=new FileReader("ip.data");
int i = 0;
int str;
while((str=ip.read()) != -1){
   if(i==327681){
      System.out.println(str);  //return 0
      break;
   }
i++;
}
但两者并不平等。 我知道PHP中的ord('a')==97,Java中的a'==97。 ip.data下载。

假定输入文件的系统默认字符集可能是utf-8,例如。*在这种情况下,最多将读取四个字节以“形成”从FileReader.read()获得的单个字符。
所以也许(只是在这一点上的猜测)这就是问题所在。您的php代码不采用任何编码,它只读取一个(8位)字节,其在java中的等效值为

fis = new  FileInputStream("ip.data");
country_id = fis.read(); // FileInputStream.read() is reading a byte, not a character

*)为了验证这一假设,请尝试

FileReader fr = new FileReader("ip.data")
System.out.println( fr.getEncoding() );
它打印什么?

假定输入文件的系统默认字符集可能是utf-8,例如。*在这种情况下,最多读取四个字节以“形成”从FileReader.read()获得的单个字符。
所以也许(只是在这一点上的猜测)这就是问题所在。您的php代码不采用任何编码,它只读取一个(8位)字节,其在java中的等效值为

fis = new  FileInputStream("ip.data");
country_id = fis.read(); // FileInputStream.read() is reading a byte, not a character

*)为了验证这一假设,请尝试

FileReader fr = new FileReader("ip.data")
System.out.println( fr.getEncoding() );
它打印什么?

试试这个:

String str = "t";
byte b = str.getBytes()[0];
int ascii = (int) b;
也许你必须使用char。

试试这个:

String str = "t";
byte b = str.getBytes()[0];
int ascii = (int) b;

可能您必须使用char。

请发布和示例
ip.data
文件,以及您在java和php中的差异请发布和示例
ip.data
文件,以及您在java和php中的差异