将双精度数组转换为字节数组:C#Buffer.BlockCopy的Java方式是什么?
我需要在Java中将double数组序列化为base64。我有以下C语言的方法# 在Java中如何做到这一点?我试过了将双精度数组转换为字节数组:C#Buffer.BlockCopy的Java方式是什么?,java,c#,arrays,base64,Java,C#,Arrays,Base64,我需要在Java中将double数组序列化为base64。我有以下C语言的方法# 在Java中如何做到这一点?我试过了 Byte[] bytes = new Byte[abundaceArray.length * Double.SIZE]; System.arraycopy(abundaceArray, 0, bytes, 0, bytes.length); abundanceValues = Base64.encodeBase64String(bytes); 但是,这会导致IndexOut
Byte[] bytes = new Byte[abundaceArray.length * Double.SIZE];
System.arraycopy(abundaceArray, 0, bytes, 0, bytes.length);
abundanceValues = Base64.encodeBase64String(bytes);
但是,这会导致IndexOutofBoundsException
如何在Java中实现这一点
编辑:
Buffer.BlockCopy在字节级别复制,最后一个参数是字节数。System.arraycopy最后一个参数是要复制的元素数。所以它应该是abundacarray.length,但随后会抛出ArrayStoreException
编辑2:
base64字符串必须与使用c#代码创建的ine相同 不确定这个C#函数的作用,但我怀疑您应该替换这一行
System.arraycopy(abundaceArray, 0, bytes, 0, bytes.length);
用这个
System.arraycopy(abundaceArray, 0, bytes, 0, abundaceArray.length);
不确定这个C#函数的作用,但我怀疑您应该替换这一行
System.arraycopy(abundaceArray, 0, bytes, 0, bytes.length);
用这个
System.arraycopy(abundaceArray, 0, bytes, 0, abundaceArray.length);
当方法上的数组类型不是相同的基元时,会出现ArrayStoreException,因此double to byte将不起作用。这里有一个我修补过的解决方法,似乎很有效。我不知道java core中有哪种方法可以自动将原语转换为字节块:
public class CUSTOM {
public static void main(String[] args) {
double[] arr = new double[]{1.1,1.3};
byte[] barr = toByteArray(arr);
for(byte b: barr){
System.out.println(b);
}
}
public static byte[] toByteArray(double[] from) {
byte[] output = new byte[from.length*Double.SIZE/8]; //this is reprezented in bits
int step = Double.SIZE/8;
int index = 0;
for(double d : from){
for(int i=0 ; i<step ; i++){
long bits = Double.doubleToLongBits(d); // first transform to a primitive that allows bit shifting
byte b = (byte)((bits>>>(i*8)) & 0xFF); // bit shift and keep adding
int currentIndex = i+(index*8);
output[currentIndex] = b;
}
index++;
}
return output;
}
}
公共类自定义{
公共静态void main(字符串[]args){
double[]arr=新的double[]{1.1,1.3};
字节[]barr=toByteArray(arr);
for(字节b:barr){
系统输出打印ln(b);
}
}
公共静态字节[]toByteArray(双[]自){
byte[]输出=新字节[from.length*Double.SIZE/8];//以位表示
int step=Double.SIZE/8;
int指数=0;
用于(双d:从){
for(int i=0;i>>(i*8))&0xFF);//位移位并继续添加
int currentIndex=i+(索引*8);
输出[currentIndex]=b;
}
索引++;
}
返回输出;
}
}
如果方法上的数组类型不同于原语,则会出现ArrayStoreException,因此双字节转换将不起作用。这里有一个我修补过的解决方法,似乎很有效。我不知道java core中有哪种方法可以自动将原语转换为字节块:
public class CUSTOM {
public static void main(String[] args) {
double[] arr = new double[]{1.1,1.3};
byte[] barr = toByteArray(arr);
for(byte b: barr){
System.out.println(b);
}
}
public static byte[] toByteArray(double[] from) {
byte[] output = new byte[from.length*Double.SIZE/8]; //this is reprezented in bits
int step = Double.SIZE/8;
int index = 0;
for(double d : from){
for(int i=0 ; i<step ; i++){
long bits = Double.doubleToLongBits(d); // first transform to a primitive that allows bit shifting
byte b = (byte)((bits>>>(i*8)) & 0xFF); // bit shift and keep adding
int currentIndex = i+(index*8);
output[currentIndex] = b;
}
index++;
}
return output;
}
}
公共类自定义{
公共静态void main(字符串[]args){
double[]arr=新的double[]{1.1,1.3};
字节[]barr=toByteArray(arr);
for(字节b:barr){
系统输出打印ln(b);
}
}
公共静态字节[]toByteArray(双[]自){
byte[]输出=新字节[from.length*Double.SIZE/8];//以位表示
int step=Double.SIZE/8;
int指数=0;
用于(双d:从){
for(int i=0;i>>(i*8))&0xFF);//位移位并继续添加
int currentIndex=i+(索引*8);
输出[currentIndex]=b;
}
索引++;
}
返回输出;
}
}
我猜您使用的是apache commons Base64类。只有接受字节数组(基元类型)而不是字节数组(基元类型周围的对象包装器)的方法
现在还不清楚你的“Abundacarray”是什么类型的——是双打还是双打
无论哪种方式,都不能使用System.arraycopy在不同基元类型的数组之间进行复制
我认为最好的办法是将数组对象序列化为字节数组,然后对其进行base64编码
例如:
当然,在另一端有一个ObjectInputStream用于向另一个方向移动。我猜您使用的是apache commons Base64类。只有接受字节数组(基元类型)而不是字节数组(基元类型周围的对象包装器)的方法 现在还不清楚你的“Abundacarray”是什么类型的——是双打还是双打 无论哪种方式,都不能使用System.arraycopy在不同基元类型的数组之间进行复制 我认为最好的办法是将数组对象序列化为字节数组,然后对其进行base64编码 例如:
当然,在另一端有一个ObjectInputStream用于向另一个方向移动。Double.SIZE get 64是我建议像这样初始化数组的位数
Byte[] bytes = new Byte[abundaceArray.length * 8];
Double.SIZE get 64是我建议像这样初始化数组的位数
Byte[] bytes = new Byte[abundaceArray.length * 8];
不工作:引发ArrayStoreException。也可以在主帖子中看到我的编辑。不起作用:引发ArrayStoreException。另请参阅我在主要帖子中的编辑。如果您不想将
double
转换为long
和long
转换为byte[],则建议使用ByteBuffer
的其中一个或两个的副本
手动。如果您不想将double
转换为long
并将long
转换为byte[]
手动,则建议使用ByteBuffer
中的一个或两个的副本。如果不是字符串丰度值=Base64,这实际上会起作用。encodeBase64String(b.toByteArray());您可以这样做byte[]vals=b.toByteArray()。原始问题的最终结果是一个base64编码的字符串,而不是一个字节数组,只是试图复制它……如果不是字符串丰度values=base64.encodeBase64String(b.toByteArray()),这实际上是可行的;您可以这样做byte[]vals=b.toByteArray()原始问题的最终结果是一个base64编码的字符串,而不是一个字节数组,只是试图复制它……这是真的+1.c#中的sizeof(double)也是8。但这并不能解决问题,这是真的+1.c#中的sizeof(double)也是8。但这并不能解决问题。我如何做反向操作?将字节数组转换为双数组如何执行反向操作?将字节数组转换为双数组