易截截图软件、单文件、免安装、纯绿色、仅160KB

C#到Java byte类型冲突的解决

最近要改写一个核心加密认证类,从C#改写成Java。
发现在调试时,加密的数据无论如何也对不上。
经过跟踪,发现问题出在C#和Java byte类型的区别上:在C#里 byte类型是无符号的,而Java里是有符号的,所以C#里的129到Java里就成了负数。
发现了问题,解决就比较容易了,针对Java的byte,采用Int来进行存储。
通过如下代码从byte到int进行转换:
/**
* from byte to int, because of byte in java is signed
*/
private static int toInt(int b) {
return b >= 0 ? (int)b : (int)(b + 256);
}
对于下面C#的代码:
private static AuthenticationTicket fromByteArray(byte[] buf)
{
MemoryStream ms = new MemoryStream(buf);
BinaryReader reader = new BinaryReader(ms);
short version = reader.ReadInt16();
short scope = reader.ReadInt16();
int key = reader.ReadInt32();
}
改写为如下形式,相当于重新实现BinaryReader的ReadInt16和ReadInt32方法。
private static AuthenticationTicket fromByteArray(int[] bufInt)
{
int version = readInt16(bufInt);
int scope = readInt16(bufInt);
long key = readInt32(bufInt);
}

private static int readInt16(int[] bufInt) {
int i = 0;
for(int j = 0; j < 2; readArrayIndex++, j++) {
i += bufInt[readArrayIndex] << (j << 3);
}
return i;
}
private static long readInt32(int[] bufInt) {
long i = 0;
for(int j = 0; j < 4; readArrayIndex++, j++) {
i += bufInt[readArrayIndex] << (j << 3);
}
return i;
}
上面的例子说明,c#和Java虽然非常相像,但是一些关键细节的不同是需要仔细考虑的。


相关文档:

java script 获得Label控件内容

 var ProjectName = document.getElementById("<%=ProjectName.ClientID%>").innerText;
       ProjectName = ProjectName.replace(/(^\s*)|(\s*$)/g, ""); // 相当于Trim()函数 ......

关于java的http协议文件上传实用例题一

关于java的http协议文件上传实用例题一
(2006-07-25 16:43:56)
转载

 

分类:
java
关于java的http协议上传:(简单实用而且健壮;速度快)
此方法比apache的文件上传包(uploadfile1.1:就文件上传功能而言)要强多了
1.只需要一个MultipartRequest.java基本文件就行。
2.前台html的基本格式
<html ......

JAVA常用操作语句 项目中的总结四


 eclipse选一个变量后,这个类里的该变量不变色问题解决:
alt + shift + o

/**
* 写入日志
* filePath 日志文件的路径
* code 要写入日志文件的内容

*/

public

static

boolean
print(String filePath,String code) {

try
{
File tofile
=
new
File(filePath);
......

Java的求质数

package stone;
public class PrimerNumber {
public static void main(String[] args) {
int MAX_NUMBER = 100;
boolean[] notPrimer= new boolean[MAX_NUMBER];
for(int i=2;i<=MAX_NUMBER;i++){
if(!notPrimer[i-1]){
for(int j=2*i;j<=MAX_NUMBER;j++){
if(j%i==0) notPrimer[j-1]=tr ......

Java语言的26个细节


Java语言的26个细节[转载]
Java作为一门优秀的面向对象的程序设计语言,正在被越来越多的人使用。本文试图列出作者在实际开发中碰到的一些Java语言的容易被人忽视的细节,希望能给正在学习Java语言的人有所帮助。
1,位移运算越界怎么处理
    考察下面的代码输出结果是多少?
    int a=5;
  ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号