import java.io.*;public class temp { public static void main(String args[]) {
int a=1; float b =3.14; double c = 1/3.14; String s = "abc";
DataInputStream in = new DataInputStream( new ByteArrayInputStream(s.getBytes()));//OK //但是我如何想把a,b,c 也弄进DataInputStream //我将如何做? } }
或才说.我如何利用 double[] 数组来创建一个 DataInputStream当然利用char[] 数组是方便的.比如: char cArray[] carray; String s = new String(carray); DataInputStream in = new DataInputStream( new ByteArrayInputStream(s.getBytes()));//OK可double数组就不是那么容易,如果其它对象数组呢?更难了. 哪位高手赐教!!
public static void main(String args[])
{
int a=1;
float b =3.14;
double c = 1/3.14;
String s = "abc";
DataInputStream in = new DataInputStream(
new ByteArrayInputStream(s.getBytes()));//OK
//但是我如何想把a,b,c 也弄进DataInputStream
//我将如何做?
}
}
char cArray[] carray;
String s = new String(carray);
DataInputStream in = new DataInputStream(
new ByteArrayInputStream(s.getBytes()));//OK可double数组就不是那么容易,如果其它对象数组呢?更难了.
哪位高手赐教!!
Float.floatToIntBits
Float.floatToRawIntBits
Double.doubleToLongBits
Double.doubleToRawLongBits
反过来是
Float.intBitsToFloat
Double.longBitsToDouble怎样把int、long截成byte就不用说了吧。
可是,我是意思是将double 型的数据存入8 个byte的数组 Byte[8]中啊!
好像并没有解决!
Sting x="1",int x=1;这两个变量字节方式存放是一样的吧?那么就可以把其他类型转换成String,再转换成byte,应该可以吧!
比如
long l = 0x0102030405060708L;
然后
byte [] b = new byte[8];b[0] = (byte) (l & 0x000000000000FFL);
b[1] = (byte) ((l & 0x0000000000FF00L) >> 8);
b[2] = (byte) ((l & 0x0000000000FF0000L) >> 16);
b[3] = (byte) ((l & 0x00000000FF000000L) >> 24);
b[4] = (byte) ((l & 0x000000FF00000000L) >> 32);
b[5] = (byte) ((l & 0x0000FF0000000000L) >> 40);
b[6] = (byte) ((l & 0x00FF000000000000L) >> 48);
b[7] = (byte) ((l & 0xFF00000000000000L) >> 56);
结果
for (int i = 0; i < 8; i ++) {
System.out.println(b[i]);
}
会得到
8
7
6
5
4
3
2
1
还原double的时候反过来就好了。