您的排序與zip排序不一樣。
所以第一步應該得到所有名稱,然後再按照新的順序來讀
如果只是解壓到某處,新的順序一點用處都沒有!
❷ JAVA 16進制轉換為ASCII的問題
new String(message)返回的就是「114524」了
❸ java中如何實現BCD碼字元串與16進制字元串的互轉
nt main(void)
4{
5 unsigned char array[4] = {"0x0","0x0","0x02","0xe7"};
6 unsigned long num;
7 num = 0;
8 for(int i=0; i<sizeof(array); i++)
9 {
10 num<<=8;
11 num |= array[i];
12 }
13 printf("num = %d",num);
14 return 0;
15
16}
二進制,位元組數組,字元,十六進制,BCD編碼轉換
* 把16進制字元串轉換成位元組數組
* @param hex
* @return
*/
public static byte[] hexStringToByte(String hex) {
int len = (hex.length() / 2);
byte[] result = new byte[len];
char[] achar = hex.toCharArray();
for (int i = 0; i < len; i++) {
int pos = i * 2;
result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
}
return result;
}
private static byte toByte(char c) {
byte b = (byte) "0123456789ABCDEF".indexOf(c);
return b;
}
public static final String bytesToHexString(byte[] bArray) {
StringBuffer sb = new StringBuffer(bArray.length);
String sTemp;
for (int i = 0; i < bArray.length; i++) {
sTemp = Integer.toHexString(0xFF & bArray[i]);
if (sTemp.length() < 2)
sb.append(0);
sb.append(sTemp.toUpperCase());
}
return sb.toString();
}
public static final Object bytesToObject(byte[] bytes) throws IOException, ClassNotFoundException {
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
ObjectInputStream oi = new ObjectInputStream(in);
Object o = oi.readObject();
oi.close();
return o;
}
public static final byte[] objectToBytes(Serializable s) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream ot = new ObjectOutputStream(out);
ot.writeObject(s);
ot.flush();
ot.close();
return out.toByteArray();
}
public static final String objectToHexString(Serializable s) throws IOException{
return bytesToHexString(objectToBytes(s));
}
public static final Object hexStringToObject(String hex) throws IOException, ClassNotFoundException{
return bytesToObject(hexStringToByte(hex));
}
public static String bcd2Str(byte[] bytes){
StringBuffer temp=new StringBuffer(bytes.length*2);
for(int i=0;i<bytes.length;i++){
temp.append((byte)((bytes[i]& 0xf0)>>>4));
temp.append((byte)(bytes[i]& 0x0f));
}
return temp.toString().substring(0,1).equalsIgnoreCase("0")?temp.toString().substring(1):temp.toString();
}
public static byte[] str2Bcd(String asc) {
int len = asc.length();
int mod = len % 2;
if (mod != 0) {
asc = "0" + asc;
len = asc.length();
}
byte abt[] = new byte[len];
if (len >= 2) {
len = len / 2;
}
byte bbt[] = new byte[len];
abt = asc.getBytes();
int j, k;
for (int p = 0; p < asc.length()/2; p++) {
if ( (abt[2 * p] >= '0') && (abt[2 * p] <= '9')) {
j = abt[2 * p] - '0';
} else if ( (abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) {
j = abt[2 * p] - 'a' + 0x0a;
} else {
j = abt[2 * p] - 'A' + 0x0a;
}
if ( (abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) {
k = abt[2 * p + 1] - '0';
} else if ( (abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) {
k = abt[2 * p + 1] - 'a' + 0x0a;
}else {
k = abt[2 * p + 1] - 'A' + 0x0a;
}
int a = (j << 4) + k;
byte b = (byte) a;
bbt[p] = b;
}
return bbt;
}
public static String BCD2ASC(byte[] bytes) {
StringBuffer temp = new StringBuffer(bytes.length * 2);
for (int i = 0; i < bytes.length; i++) {
int h = ((bytes[i] & 0xf0) >>> 4);
int l = (bytes[i] & 0x0f);
temp.append(BToA[h]).append( BToA[l]);
}
return temp.toString() ;
}
public static String MD5EncodeToHex(String origin) {
return bytesToHexString(MD5Encode(origin));
}
public static byte[] MD5Encode(String origin){
return MD5Encode(origin.getBytes());
}
public static byte[] MD5Encode(byte[] bytes){
MessageDigest md=null;
try {
md = MessageDigest.getInstance("MD5");
return md.digest(bytes);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return new byte[0];
}
}
//關於byte: signed byte 把 0x00 ~ 0xff 映射成范圍 0~127和 -128~-1 兩段,比較簡單的辦法用 (b+256)%256的辦法令其值回到0~255,或者用&0xff並賦給一個int
❹ java里,二進制、十進制、八進制、十六進制互相轉換的問題
10進制適合人類使用
16進制適合編譯器使用和底層程序員,因為和二進制對應著,比如匯編語言
二進制適合CPU使用,因為所有的數據和代碼最終都是二進制的。
竅門沒有,常見的記住就行了。
8進制我很少見到
❺ java詭異問題 高手解答
Java中的簡單浮點數類型float和double不能夠進行運算。不光是Java,在其它很多編程語言中也有這樣的問題。在大多數情況下,計算的結果是准確的,但是多試幾次(可以做一個循環)就可以試出類似上面的錯誤。現在終於理解為什麼要有BCD碼了。
這個問題相當嚴重,假如你有0.8999999999999999元,你的計算機是不會認為你可以購買0.9元的商品的。
在有的編程語言中提供了專門的貨幣類型來處理這種情況,但是Java沒有。現在讓我們看看如何解決這個問題。
四捨五入
我們的第一個反應是做四捨五入。Math類中的round方法不能設置保留幾位小數,我們只能象這樣(保留兩位):
public double round(double value){
return Math.round(value*100)/100.0;
}
非常不幸,上面的代碼並不能正常工作,給這個方法傳入4.015它將返回4.01而不是4.02,如我們在上面看到的
4.015*100=401.49999999999994
因此假如我們要做到精確的四捨五入,不能利用簡單類型做任何運算
java.text.DecimalFormat也不能解決這個問題:
System.out.println(new java.text.DecimalFormat("0.00").format(4.025));
輸出是4.02
BigDecimal
在《Effective Java》這本書中也提到這個原則,float和double只能用來做科學計算或者是工程計算,在商業計算中我們要用java.math.BigDecimal。BigDecimal一共有4個夠造方法,我們不關心用BigInteger來夠造的那兩個,那麼還有兩個,它們是:
BigDecimal(double val)
Translates a double into a BigDecimal.
BigDecimal(String val)
Translates the String repre sentation of a BigDecimal into a BigDecimal.
上面的API簡要描述相當的明確,而且通常情況下,上面的那一個使用起來要方便一些。我們可能想都不想就用上了,會有什麼問題呢?等到出了問題的時候,才發現上面哪個夠造方法的具體說明中有這么一段:
Note: the results of this constrUCtor can be somewhat unpredictable. One might assume that new BigDecimal(.1) is exactly equal to .1, but it is actually equal to .. This is so because .1 cannot be represented exactly as a double (or, for that matter, as a binary fraction of any finite length). Thus, the long value that is being passed in to the constructor is not exactly equal to .1, appearances nonwithstanding.
The (String) constructor, on the other hand, is perfectly predictable: new BigDecimal(".1") is exactly equal to .1, as one would eXPect. Therefore, it is generally recommended that the (String) constructor be used in preference to this one.
原來我們假如需要精確計算,非要用String來夠造BigDecimal不可!在《Effective Java》一書中的例子是用String來夠造BigDecimal的,但是書上卻沒有強調這一點,這也許是一個小小的失誤吧。
解決方案
現在我們已經可以解決這個問題了,原則是使用BigDecimal並且一定要用String來夠造。
但是想像一下吧,假如我們要做一個加法運算,需要先將兩個浮點數轉為String,然後夠造成BigDecimal,在其中一個上調用add方法,傳入另一個作為參數,然後把運算的結果(BigDecimal)再轉換為浮點數。你能夠忍受這么煩瑣的過程嗎?下面我們提供一個工具類Arith來簡化操作。它提供以下靜態方法,包括加減乘除和四捨五入:
public static double add(double v1,double v2)
public static double sub(double v1,double v2)
public static double mul(double v1,double v2)
public static double div(double v1,double v2)
public static double div(double v1,double v2,int scale)
public static double round(double v,int scale)
資料引用:http://www.knowsky.com/362313.html
❻ 誰那有java把漢字的String轉BCD 的再把BCD轉回漢字String的代碼例子
轉二進制碼?
❼ java 使用socket編程,讀出7個位元組的流,如何轉化為日期格式
算是一種畸形的日期格式了,用十六進制數記錄日期。效率不高
public class Test {
static public String decodeWeirdDate(final byte a[]){
StringBuffer buf=new StringBuffer();
for(byte b:a)
buf.append(String.format("%02X",b));
return buf.toString();
}
public static void main(String[] args){
byte []a={32, 18, 7, 5, 0, 64, 69};
System.out.println(decodeWeirdDate(a));
}
}
=======
20120705004045
❽ java 如何判斷一個byte型的數為BCD碼
你將0~9的數字也轉換成同樣類型的數據,並且放在一個map中,用Byte的BCD嘛對map取值,如果取map返回的對象不為null,就說明達到了你的效果了
❾ JAVA關於計算的輸出問題
1、原因在小數的位數問題,因為double輸入的數字是3.14,兩位小數,3.14*5=18.4,不足兩位小數,數據不夠,
輸出語句上面增加幾句代碼,
NumberFormatnf=NumberFormat.getNumberInstance();
nf.setMaximumIntegerDigits(2);//設置保留兩位小數
j2=Double.parseDouble(nf.format(j2)); //轉化後是字元串,再轉為double型
這樣對數據進行一次操作,當結果是兩位或一位小數時就不存在問題了,3位或更多還是會出現問題,數據的位數始終都是需要注意的,這種測試題目可以適當保留多一些,一般不會出錯
❿ java 如何實現 十六進制 轉為壓縮 BCD碼。
用和底層程序員,因為和二進制對應著,比如匯編語言
二進制適合CPU使用,因為所有的數據和代碼最終都是二進制的。
竅門沒有,常見的記住就行了。
8進制我很少見到