Buffer an Unsigned Integer in Java
-
04-10-2022 - |
题
I am trying to add an unsigned integer in Java, I know they are not supported, but I feel they would help in our case and make it easier than a long.
I would like to make an int of getUInt(), etc.
Our current code:
//--- functions ---
public int unsigned(int index){
byte b = buf[index];
if (b>=0) return b;
return 256 + b;
}
public void setInt(int index,int val){
buf[index+0] = (byte)(val&0xff);
buf[index+1] = (byte)((val>>8)&0xff);
buf[index+2] = (byte)((val>>16)&0xff);
buf[index+3] = (byte)((val>>24)&0xff);
}
public void setShort(int index,int val){
buf[index+0] = (byte)(val&0xff);
buf[index+1] = (byte)((val>>8)&0xff);
}
public int getInt(int index){
return (buf[index+3]<<24) | (unsigned(index+2)<<16) | (unsigned(index+1)<<8) | unsigned(index+0);
}
public int getShort(int index){
return (buf[index+1]<<8) | unsigned(index+0);
}
public int getUShort(int index){
return (unsigned(index+1)<<8) | unsigned(index+0);
}
How would I go about doing so, every time I get into the wrong buffer and get zeros.
Thanks for all your help, -Michael
没有正确的解决方案
不隶属于 StackOverflow