Java與其他語(yǔ)言數(shù)據(jù)類型之間的轉(zhuǎn)換方法_JSP教程
推薦:Java多媒體框架設(shè)計(jì)自動(dòng)播放機(jī)如果把Java媒體框架(http://java.sun.com/jmf)看作客戶端安裝的媒體播放機(jī)的話,那么這種框架并不是非常激動(dòng)人心。它只支持少量媒體類型,并且事實(shí)上這些媒體類型都被象Windows媒體播放機(jī)以
Java與其他語(yǔ)言數(shù)據(jù)類型之間的轉(zhuǎn)換方法實(shí)例程序
/**
* 通信格式轉(zhuǎn)換
*
* Java和一些windows編程語(yǔ)言如c、c 、delphi所寫的網(wǎng)絡(luò)程序進(jìn)行通訊時(shí),需要進(jìn)行相應(yīng)的轉(zhuǎn)換
* 高、低字節(jié)之間的轉(zhuǎn)換
* windows的字節(jié)序?yàn)榈妥止?jié)開(kāi)頭
* linux,unix的字節(jié)序?yàn)楦咦止?jié)開(kāi)頭
* java則無(wú)論平臺(tái)變化,都是高字節(jié)開(kāi)頭
*/
public class FormatTransfer {
/**
* 將int轉(zhuǎn)為低字節(jié)在前,高字節(jié)在后的byte數(shù)組
* @param n int
* @return byte[]
*/
public static byte[] toLH(int n) {
byte[] b = new byte[4];
b[0] = (byte) (n & 0xff);
b[1] = (byte) (n >> 8 & 0xff);
b[2] = (byte) (n >> 16 & 0xff);
b[3] = (byte) (n >> 24 & 0xff);
return b;
}
/**
* 將int轉(zhuǎn)為高字節(jié)在前,低字節(jié)在后的byte數(shù)組
* @param n int
* @return byte[]
*/
public static byte[] toHH(int n) {
byte[] b = new byte[4];
b[3] = (byte) (n & 0xff);
b[2] = (byte) (n >> 8 & 0xff);
b[1] = (byte) (n >> 16 & 0xff);
b[0] = (byte) (n >> 24 & 0xff);
return b;
}
/**
* 將short轉(zhuǎn)為低字節(jié)在前,高字節(jié)在后的byte數(shù)組
* @param n short
* @return byte[]
*/
public static byte[] toLH(short n) {
byte[] b = new byte[2];
b[0] = (byte) (n & 0xff);
b[1] = (byte) (n >> 8 & 0xff);
return b;
}
/**
* 將short轉(zhuǎn)為高字節(jié)在前,低字節(jié)在后的byte數(shù)組
* @param n short
* @return byte[]
*/
public static byte[] toHH(short n) {
byte[] b = new byte[2];
b[1] = (byte) (n & 0xff);
b[0] = (byte) (n >> 8 & 0xff);
return b;
}
/**
* 將將int轉(zhuǎn)為高字節(jié)在前,低字節(jié)在后的byte數(shù)組
public static byte[] toHH(int number) {
int temp = number;
byte[] b = new byte[4];
for (int i = b.length - 1; i > -1; i--) {
b = new Integer(temp & 0xff).byteValue();
temp = temp >> 8;
}
return b;
}
public static byte[] IntToByteArray(int i) {
byte[] abyte0 = new byte[4];
abyte0[3] = (byte) (0xff & i);
abyte0[2] = (byte) ((0xff00 & i) >> 8);
abyte0[1] = (byte) ((0xff0000 & i) >> 16);
abyte0[0] = (byte) ((0xff000000 & i) >> 24);
return abyte0;
}
*/
/**
* 將float轉(zhuǎn)為低字節(jié)在前,高字節(jié)在后的byte數(shù)組
*/
public static byte[] toLH(float f) {
return toLH(Float.floatToRawIntBits(f));
}
/**
* 將float轉(zhuǎn)為高字節(jié)在前,低字節(jié)在后的byte數(shù)組
*/
public static byte[] toHH(float f) {
return toHH(Float.floatToRawIntBits(f));
}
/**
* 將String轉(zhuǎn)為byte數(shù)組
*/
public static byte[] stringToBytes(String s, int length) {
while (s.getBytes().length < length) {
s = " ";
}
return s.getBytes();
}
/**
* 將字節(jié)數(shù)組轉(zhuǎn)換為String
* @param b byte[]
* @return String
*/
public static String bytesToString(byte[] b) {
StringBuffer result = new StringBuffer("");
int length = b.length;
for (int i=0; i
}
return result.toString();
}
/**
* 將字符串轉(zhuǎn)換為byte數(shù)組
* @param s String
* @return byte[]
*/
public static byte[] stringToBytes(String s) {
return s.getBytes();
}
/**
* 將高字節(jié)數(shù)組轉(zhuǎn)換為int
* @param b byte[]
* @return int
*/
public static int hBytesToInt(byte[] b) {
int s = 0;
for (int i = 0; i < 3; i ) {
if (b >= 0) {
s = s b;
} else {
s = s 256 b;
}
s = s * 256;
}
if (b[3] >= 0) {
s = s b[3];
} else {
s = s 256 b[3];
}
return s;
}
/**
* 將低字節(jié)數(shù)組轉(zhuǎn)換為int
* @param b byte[]
* @return int
*/
public static int lBytesToInt(byte[] b) {
int s = 0;
for (int i = 0; i < 3; i ) {
if (b[3-i] >= 0) {
s = s b[3-i];
} else {
s = s 256 b[3-i];
}
s = s * 256;
}
if (b[0] >= 0) {
s = s b[0];
} else {
s = s 256 b[0];
}
return s;
}
/**
* 高字節(jié)數(shù)組到short的轉(zhuǎn)換
* @param b byte[]
* @return short
*/
public static short hBytesToShort(byte[] b) {
int s = 0;
if (b[0] >= 0) {
s = s b[0];
} else {
s = s 256 b[0];
}
s = s * 256;
if (b[1] >= 0) {
s = s b[1];
} else {
s = s 256 b[1];
}
short result = (short)s;
return result;
}
/**
* 低字節(jié)數(shù)組到short的轉(zhuǎn)換
* @param b byte[]
* @return short
*/
public static short lBytesToShort(byte[] b) {
int s = 0;
if (b[1] >= 0) {
s = s b[1];
} else {
s = s 256 b[1];
}
s = s * 256;
if (b[0] >= 0) {
s = s b[0];
} else {
s = s 256 b[0];
}
short result = (short)s;
return result;
}
/**
* 高字節(jié)數(shù)組轉(zhuǎn)換為float
* @param b byte[]
* @return float
*/
public static float hBytesToFloat(byte[] b) {
int i = 0;
Float F = new Float(0.0);
i = ((((b[0]&0xff)<<8 | (b[1]&0xff))<<8) | (b[2]&0xff))<<8 | (b[3]&0xff);
return F.intBitsToFloat(i);
}
/**
* 低字節(jié)數(shù)組轉(zhuǎn)換為float
* @param b byte[]
* @return float
*/
public static float lBytesToFloat(byte[] b) {
int i = 0;
Float F = new Float(0.0);
i = ((((b[3]&0xff)<<8 | (b[2]&0xff))<<8) | (b[1]&0xff))<<8 | (b[0]&0xff);
return F.intBitsToFloat(i);
}
/**
* 將byte數(shù)組中的元素倒序排列
*/
public static byte[] bytesReverseOrder(byte[] b) {
int length = b.length;
byte[] result = new byte[length];
for(int i=0; i
}
return result;
}
/**
* 打印byte數(shù)組
*/
public static void printBytes(byte[] bb) {
int length = bb.length;
for (int i=0; i
}
System.out.println("");
}
public static void logBytes(byte[] bb) {
int length = bb.length;
String out = "";
for (int i=0; i
}
}
/**
* 將int類型的值轉(zhuǎn)換為字節(jié)序顛倒過(guò)來(lái)對(duì)應(yīng)的int值
* @param i int
* @return int
*/
public static int reverseInt(int i) {
int result = FormatTransfer.hBytesToInt(FormatTransfer.toLH(i));
return result;
}
/**
* 將short類型的值轉(zhuǎn)換為字節(jié)序顛倒過(guò)來(lái)對(duì)應(yīng)的short值
* @param s short
* @return short
*/
public static short reverseShort(short s) {
short result = FormatTransfer.hBytesToShort(FormatTransfer.toLH(s));
return result;
}
/**
* 將float類型的值轉(zhuǎn)換為字節(jié)序顛倒過(guò)來(lái)對(duì)應(yīng)的float值
* @param f float
* @return float
*/
public static float reverseFloat(float f) {
float result = FormatTransfer.hBytesToFloat(FormatTransfer.toLH(f));
return result;
}
}
分享:如何實(shí)現(xiàn)javabean的屬性拷貝在struts的實(shí)踐過(guò)程中,經(jīng)常兩個(gè)javabean交換數(shù)據(jù)的情況,如ActionForm與數(shù)據(jù)庫(kù)中的表相關(guān)的bean交換數(shù)據(jù)。通常情況下要寫很多get和set語(yǔ)句,一個(gè)個(gè)屬性依次拷貝。這樣的話,如此重復(fù)繁重的工
- jsp response.sendRedirect不跳轉(zhuǎn)的原因分析及解決
- JSP指令元素(page指令/include指令/taglib指令)復(fù)習(xí)整理
- JSP腳本元素和注釋復(fù)習(xí)總結(jié)示例
- JSP FusionCharts Free顯示圖表 具體實(shí)現(xiàn)
- 網(wǎng)頁(yè)模板:關(guān)于jsp頁(yè)面使用jstl的異常分析
- JSP頁(yè)面中文傳遞參數(shù)使用escape編碼
- 基于jsp:included的使用與jsp:param亂碼的解決方法
- Java Web項(xiàng)目中連接Access數(shù)據(jù)庫(kù)的配置方法
- JDBC連接Access數(shù)據(jù)庫(kù)的幾種方式介紹
- 網(wǎng)站圖片路徑的問(wèn)題:絕對(duì)路徑/虛擬路徑
- (jsp/html)網(wǎng)頁(yè)上嵌入播放器(常用播放器代碼整理)
- jsp下顯示中文文件名及絕對(duì)路徑下的圖片解決方法
- 相關(guān)鏈接:
- 教程說(shuō)明:
JSP教程-Java與其他語(yǔ)言數(shù)據(jù)類型之間的轉(zhuǎn)換方法
。