Java与C底层数据类型转换(Java and C underlying data type conversion)
Java与C底层数据类型转换(Java and C underlying data type conversion)
进口java.io.bytearrayinputstream;
进口java.io.bytearrayoutputstream;
进口java.io.datainputstream;
进口java.io.dataoutputstream;
进口;
/ * *
*仅仅适用于java与C++通讯中,网络流解析与生成使用
*
*高低位互换(大端大头在前&;小端小头在前)。
*举例而言,有一个4字节的数据0要存储在内存中或文件中编号0~3字节的位置,两种字节序的排列方式分别如下:
*
* Big Endian
*
*低地址高地址
* ---------------------------------------------------- & gt;
*地址编号
* | 0 1 2 3 | | | |
的
* | 01 02 03 04 | | | |
的
*
* Little Endian
*
*低地址高地址
* ---------------------------------------------------- & gt;
*地址编号
* | 0 1 2 3 | | | |
的
* | 04 03 02 01 | | | |
的
*
* java则统一使用大模式
* C中的unsigned short对应着java中的焦两个字节,无符号
* C的无符号int,短,字节数组java字节,相应转换成的长、短焦,
*
作者snowolf * @
* @版本1
*自1起
* /
公共cioutil抽象类{
public static final String charset =“UTF-8”;
/ * *
*从输入流中读布尔
*
* @param是
*归
* @抛出IOException
* /
公共静态布尔readboolean(输入流是)throws IOException {
readboolean()回报;
}
/ * *
*从流中读定长度字节数组
*
* @param是
* @param的
*归
* @抛出IOException
* /
公共静态字节[] readbytes(DataInputStream,int i)
抛出IOException {
字节[数据] =新字节[ i ];
是readfully(数据);
返回数据;
}
/ * *
*从输入流中读字符
*
* @param是
*归
* @抛出IOException
* /
public static char readChar(输入流是)throws IOException {
返回(char)readshort(是);
}
/ * *
*从输入流中读双精度
*
* @param是
*归
* @抛出IOException
* /
public static double readdouble (datainputstream throws ioexception (is)
in double.longbitstodouble (readlong (is).
}
/ * *
* 从输入流中读单精度
*
* @ param is
* @ return
* * * * * * @ throws ioexception
* /
public static float readfloat (datainputstream throws ioexception (is)
in float.intbitstofloat (readint (is).
}
/ * *
* 从流中读整型
*
* @ param is
* @ return
* * * * * * @ throws ioexception
* /
public static int readint (datainputstream throws ioexception (is)
in integer.reversebytes (is.readint ());
}
/ * *
* 从流中读长整型
*
* @ param is
* @ return
* * * * * * @ throws ioexception
* /
public static long readlong (datainputstream throws ioexception (is)
in long.reversebytes (is.readlong ());
}
/ * *
* 从流中读短整型
*
* @ param is
* @ return
* * * * * * @ throws ioexceptio