程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java與C底層數據類型轉換

Java與C底層數據類型轉換

編輯:關於JAVA

前段時間一直忙著做J2EE服務器與C++客戶端的項目。終於,項目告一段落,有一些收獲 在這裡與大家分享。

Java代碼 

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
/**
  * 僅僅適用於 Java 與 C++ 通訊中,網絡流解析與生成使用
  *
  * 高低位互換(Big-Endian 大頭在前 & Little-Endian 小頭在前)。
  * 舉例而言,有一個4字節的數據0x01020304,要存儲在內存中或文件中編號 0˜3字節的位置,兩種字節序的排列方式分別如下:
  *
  * Big Endian
  *
  * 低地址              高地址
  * ---------------------------------------------------->
  * 地址編號
  * |   0   |   1   |   2    |   3  |
  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  * |   01   |   02  |   03   |   04  |
  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  *
  * Little Endian
  *
  * 低地址              高地址
  * ---------------------------------------------------->
  * 地址編號
  * |   0   |   1   |   2    |   3  |
  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  * |   04   |   03  |   02   |   01  |
  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  *
  * Java則統一使用big模式
  * c中的unsigned short 對應著java中的char兩個字節,無符號
  * c的無符號int,short,byte字節數組,相應轉換成java的long,char,short
  *
  * @author Snowolf
  * @version 1.0
  * @since 1.0
  */
public abstract class CIOUtil {
public static final String CHARSET = "UTF-8";

/**
  * 從輸入流中讀布爾 
  *
  * @param is
  * @return
  * @throws IOException
  */
public static boolean readBoolean(DataInputStream is) throws IOException  {
return is.readBoolean();
}

/**
  * 從流中讀定長度字節數組
  *
  * @param is
  * @param s
  * @return
  * @throws IOException
  */
public static byte[] readBytes(DataInputStream is, int i)

throws IOException {
byte[] data = new byte[i];
is.readFully(data);

return data;
}

/**
  * 從輸入流中讀字符 
  *
  * @param is
  * @return
  * @throws IOException
  */
public static char readChar(DataInputStream is) throws IOException {
return (char) readShort(is);
}

/**
  * 從輸入流中讀雙精度 
  *
  * @param is
  * @return
  * @throws IOException
  */
public static double readDouble(DataInputStream is) throws IOException  {
return Double.longBitsToDouble(readLong(is));
}

/**
  * 從輸入流中讀單精度
  *
  * @param is
  * @return
  * @throws IOException
  */
public static float readFloat(DataInputStream is) throws IOException  {
return Float.intBitsToFloat(readInt(is));
}

/**
  * 從流中讀整型
  *
  * @param is
  * @return
  * @throws IOException
  */
public static int readInt(DataInputStream is) throws IOException {
return Integer.reverseBytes(is.readInt());
}

/**
  * 從流中讀長整型
  *
  * @param is
  * @return
  * @throws IOException
  */
public static long readLong(DataInputStream is) throws IOException {
return Long.reverseBytes(is.readLong());
}

/**
  * 從流中讀短整型
  *
  * @param is
  * @return
  * @throws IOException
  */
public static short readShort(DataInputStream is) throws IOException  {
return Short.reverseBytes(is.readShort());
}

/**
  * 從輸入流中讀字符串 字符串 結構為一個指定字符串字節長度的短整型+實際字符 串
  *
  * @param is
  * @return
  * @throws IOException
  */
public static String readUTF(DataInputStream is) throws IOException  {
short s = readShort(is);
byte[] str = new byte[s];

is.readFully(str);

return new String(str, CHARSET);
}

/**
  * 向輸出流中寫布爾
  *
  * @param os
  * @param b
  * @throws IOException
  */
public static void writeBoolean(DataOutputStream os, boolean b)

throws IOException {
os.writeBoolean(b);
}

/**
  * 向輸出流中寫字節數組
  *
  * @param os
  * @param data
  * @throws IOException
  */
public static void writeBytes(DataOutputStream os, byte[] data)

throws IOException {
os.write(data);
}

/**
  * 向輸出流中寫字符
  *
  * @param os
  * @param b
  * @throws IOException
  */
public static void writeChar(DataOutputStream os, char b)

throws IOException {
writeShort(os, (short) b);
}

/**
  * 向輸出流中寫雙精度
  *
  * @param os
  * @param d
  * @throws IOException
  */
public static void writeDouble(DataOutputStream os, double d)

throws IOException {
writeLong(os, Double.doubleToLongBits(d));
}

/**
  * 向輸出流中寫單精度
  *
  * @param os
  * @param f
  * @throws IOException
  */
public static void writeFloat(DataOutputStream os, float f)

throws IOException {
writeInt(os, Float.floatToIntBits(f));
}

/**
  * 向輸出流中寫整型
  *
  * @param os
  * @param i
  * @throws IOException
  */
public static void writeInt(DataOutputStream os, int i) throws  IOException {
os.writeInt(Integer.reverseBytes(i));
}

/**
  * 向輸出流中寫長整型
  *
  * @param os
  * @param l
  * @throws IOException
  */
public static void writeLong(DataOutputStream os, long l)

throws IOException {
os.writeLong(Long.reverseBytes(l));
}

/**
  * 向輸出流中寫短整型
  *
  * @param os
  * @param s
  * @throws IOException
  */
public static void writeShort(DataOutputStream os, short s)

throws IOException {
os.writeShort(Short.reverseBytes(s));
}

/**
  * 向輸出流中寫字符串 字符串 結構 為 一個指定字符串字節長度的短整型+實際 字符串
  *
  * @param os
  * @param str
  * @throws IOException
  */
public static void writeUTF(DataOutputStream os, String str)

throws IOException {
byte[] data = str.getBytes(CHARSET);
writeShort(os, (short) data.length);
os.write(data);
}
}

再寫個測試類

Java代碼

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import org.junit.Test;
import static org.junit.Assert.*;
/**
  *
  * @author Snowolf
  * @version 1.0
  * @since 1.0
  */
public class CIOUtilTest {
/**
  * 測試布爾值
  *
  * @throws IOException
  */
@Test
public final void testBoolean() throws IOException {
boolean input = true;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream os = new DataOutputStream(baos);

CIOUtil.writeBoolean(os, input);

byte[] b = baos.toByteArray();
baos.flush();
baos.close();

ByteArrayInputStream bais = new ByteArrayInputStream(b);
DataInputStream is = new DataInputStream(bais);

boolean output = CIOUtil.readBoolean(is);

bais.close();

assertEquals(input, output);
}

/**
  * 測試字節數組
  *
  * @throws IOException
  */
@Test
public final void testBytes() throws IOException {
byte[] input = "中文".getBytes("UTF-8");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream os = new DataOutputStream(baos);

CIOUtil.writeBytes(os, input);

byte[] b = baos.toByteArray();
baos.flush();
baos.close();

ByteArrayInputStream bais = new ByteArrayInputStream(b);
DataInputStream is = new DataInputStream(bais);

byte[] output = CIOUtil.readBytes(is, 6);

bais.close();

assertArrayEquals(input, output);
}

/**
  * 測試字符
  *
  * @throws IOException
  */
@Test
public final void testChar() throws IOException {
char input = '中';
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream os = new DataOutputStream(baos);

CIOUtil.writeChar(os, input);

byte[] b = baos.toByteArray();
baos.flush();
baos.close();

ByteArrayInputStream bais = new ByteArrayInputStream(b);
DataInputStream is = new DataInputStream(bais);

char output = CIOUtil.readChar(is);

bais.close();

assertEquals(input, output);
}

/**
  * 測試雙精度
  *
  * @throws IOException
  */
@Test
public final void testDouble() throws IOException {
double input = 1.23456789d;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream os = new DataOutputStream(baos);

CIOUtil.writeDouble(os, input);

byte[] b = baos.toByteArray();
baos.flush();
baos.close();

ByteArrayInputStream bais = new ByteArrayInputStream(b);
DataInputStream is = new DataInputStream(bais);

double output = CIOUtil.readDouble(is);

bais.close();

assertEquals(input, output, 9);
}

/**
  * 測試單精度
  *
  * @throws IOException
  */
@Test
public final void testFloat() throws IOException {
float input = 1.23456789f;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream os = new DataOutputStream(baos);

CIOUtil.writeFloat(os, input);

byte[] b = baos.toByteArray();
baos.flush();
baos.close();

ByteArrayInputStream bais = new ByteArrayInputStream(b);
DataInputStream is = new DataInputStream(bais);

float output = CIOUtil.readFloat(is);

bais.close();

assertEquals(input, output, 9);
}

/**
  * 測試整型
  *
  * @throws IOException
  */
@Test
public final void testInt() throws IOException {
int input = 1;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream os = new DataOutputStream(baos);

CIOUtil.writeInt(os, input);

byte[] b = baos.toByteArray();
baos.flush();
baos.close();

ByteArrayInputStream bais = new ByteArrayInputStream(b);
DataInputStream is = new DataInputStream(bais);

int output = CIOUtil.readInt(is);

bais.close();

assertEquals(input, output);
}

/**
  * 測試長整型
  *
  * @throws IOException
  */
@Test
public final void testLong() throws IOException {
long input = 1l;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream os = new DataOutputStream(baos);

CIOUtil.writeLong(os, input);

byte[] b = baos.toByteArray();
baos.flush();
baos.close();

ByteArrayInputStream bais = new ByteArrayInputStream(b);
DataInputStream is = new DataInputStream(bais);

long output = CIOUtil.readLong(is);

bais.close();

assertEquals(input, output);
}

/**
  * 測試短整型
  *
  * @throws IOException
  */
@Test
public final void testShort() throws IOException {
short input = 1;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream os = new DataOutputStream(baos);

CIOUtil.writeShort(os, input);

byte[] b = baos.toByteArray();
baos.flush();
baos.close();

ByteArrayInputStream bais = new ByteArrayInputStream(b);
DataInputStream is = new DataInputStream(bais);

short output = CIOUtil.readShort(is);

bais.close();

assertEquals(input, output);
}

/**
  * 測試UTF-8字符串
  *
  * @throws IOException
  */
@Test
public final void testUTF() throws IOException {
String input = "中文支持";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream os = new DataOutputStream(baos);

CIOUtil.writeUTF(os, input);

byte[] b = baos.toByteArray();
baos.flush();
baos.close();

ByteArrayInputStream bais = new ByteArrayInputStream(b);
DataInputStream is = new DataInputStream(bais);

String output = CIOUtil.readUTF(is);

bais.close();

assertEquals(input, output);
}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved