2012-10-26 63 views
0

可能重複:
Retreive my number after make it in Byte[]從字節讀一些[]

我想以後以檢索基本數量,使其在字節]

public static void main(String[] args) throws IOException { 
     LinkedList<Byte> s1 = new LinkedList<Byte>(); 
     String a = "0.111112345"; 
     for (byte bb : a.getBytes()) { 
      s1.add(bb); 
      } 
//how to retrieve "0.111112345"; from s1 ? 
} 
+1

[*你嘗試過什麼?*](http://mattgemmell.com/2008/12/08/what-have-you-tried/) –

回答

1

首先,不要使用getBytes()而不指定編碼 - 它將使用平臺的默認編碼,這幾乎從來不是你想要的。

既然你已經有了這許多的文本表示的二進制表示的字節,這聽起來像你應該基本上將其轉換回字符串,然後使用Double.parseDouble(...)new BigDecimal(...)

如果你有一些「真正的二進制」表示的數字,這將是一個不同的問題 - 但這是一個文字表示的核心。

+0

在我的算法,我必須用鏈表! – Mehdi

+0

@Mehdi:然後將'LinkedList '轉換回'byte []',然後返回到'String'。 *爲什麼*您是否必須使用'LinkedList ',您是否真的需要使用基於文本的表示? –

+0

你可以給我一個例子,請將LinkedList 轉換爲一個字節[],然後轉換爲一個字符串並最終檢索我的基本數字。 – Mehdi

0

如果你想什麼s1集合轉換爲數字的表示,這樣做:

String number = ""; 
for(byte b : s1) 
    number += (char) b; 
System.out.println("The number is:" + number); 

如果你想在多個類型:

double dbl = Double.parseDouble(number); 
0

雙號=雙.parseDouble(new String(s1.toArray(new Byte [s1.size])))

0

你沒有得到它。這並不簡單。你的代碼中的問題是你正在轉換String to bytes。只給你一個例子

System.out.println(Arrays.toString("10".getBytes())); 

將打印[49, 48]這是因爲編碼所以從String to Bytes

下面沒有直接的映射代碼從FloatingDecimal這確實是你可以看到它的工作實在太困難比你現在正在做的事情要好。

 999  public static FloatingDecimal 
    1000  readJavaFormatString(String in) throws NumberFormatException { 
    1001   boolean isNegative = false; 
    1002   boolean signSeen = false; 
    1003   int  decExp; 
    1004   char c; 
    1005 
    1006  parseNumber: 
    1007   try{ 
    1008    in = in.trim(); // don't fool around with white space. 
    1009        // throws NullPointerException if null 
    1010    int l = in.length(); 
    1011    if (l == 0) throw new NumberFormatException("empty String"); 
    1012    int i = 0; 
    1013    switch (c = in.charAt(i)){ 
    1014    case '-': 
    1015     isNegative = true; 
    1016     //FALLTHROUGH 
    1017    case '+': 
    1018     i++; 
    1019     signSeen = true; 
    1020    } 
    1021 
    1022    // Check for NaN and Infinity strings 
    1023    c = in.charAt(i); 
    1024    if(c == 'N' || c == 'I') { // possible NaN or infinity 
    1025     boolean potentialNaN = false; 
    1026     char targetChars[] = null; // char array of "NaN" or "Infinity" 
    1027 
    1028     if(c == 'N') { 
    1029      targetChars = notANumber; 
    1030      potentialNaN = true; 
    1031     } else { 
    1032      targetChars = infinity; 
    1033     } 
    1034 
    1035     // compare Input string to "NaN" or "Infinity" 
    1036     int j = 0; 
    1037     while(i < l && j < targetChars.length) { 
    1038      if(in.charAt(i) == targetChars[j]) { 
    1039       i++; j++; 
    1040      } 
    1041      else // something is amiss, throw exception 
    1042       break parseNumber; 
    1043     } 
    1044 
    1045     // For the candidate string to be a NaN or infinity, 
    1046     // all characters in input string and target char[] 
    1047     // must be matched ==> j must equal targetChars.length 
    1048     // and i must equal l 
    1049     if((j == targetChars.length) && (i == l)) { // return NaN or infinity 
    1050      return (potentialNaN ? new FloatingDecimal(Double.NaN) // NaN has no sign 
    1051        : new FloatingDecimal(isNegative? 
    1052              Double.NEGATIVE_INFINITY: 
    1053              Double.POSITIVE_INFINITY)) ; 
    1054     } 
    1055     else { // something went wrong, throw exception 
    1056      break parseNumber; 
    1057     } 
    1058 
    1059    } else if (c == '0') { // check for hexadecimal floating-point number 
    1060     if (l > i+1) { 
    1061      char ch = in.charAt(i+1); 
    1062      if (ch == 'x' || ch == 'X') // possible hex string 
    1063       return parseHexString(in); 
    1064     } 
    1065    } // look for and process decimal floating-point string 
    1066 
    1067    char[] digits = new char[ l ]; 
    1068    int nDigits= 0; 
    1069    boolean decSeen = false; 
    1070    int decPt = 0; 
    1071    int nLeadZero = 0; 
    1072    int nTrailZero= 0; 
    1073   digitLoop: 
    1074    while (i < l){ 
    1075     switch (c = in.charAt(i)){ 
    1076     case '0': 
    1077      if (nDigits > 0){ 
    1078       nTrailZero += 1; 
    1079      } else { 
    1080       nLeadZero += 1; 
    1081      } 
    1082      break; // out of switch. 
    1083     case '1': 
    1084     case '2': 
    1085     case '3': 
    1086     case '4': 
    1087     case '5': 
    1088     case '6': 
    1089     case '7': 
    1090     case '8': 
    1091     case '9': 
    1092      while (nTrailZero > 0){ 
    1093       digits[nDigits++] = '0'; 
    1094       nTrailZero -= 1; 
    1095      } 
    1096      digits[nDigits++] = c; 
    1097      break; // out of switch. 
    1098     case '.': 
    1099      if (decSeen){ 
    1100       // already saw one ., this is the 2nd. 
    1101       throw new NumberFormatException("multiple points"); 
    1102      } 
    1103      decPt = i; 
    1104      if (signSeen){ 
    1105       decPt -= 1; 
    1106      } 
    1107      decSeen = true; 
    1108      break; // out of switch. 
    1109     default: 
    1110      break digitLoop; 
    1111     } 
    1112     i++; 
    1113    } 
    1114    /* 
    1115    * At this point, we've scanned all the digits and decimal 
    1116    * point we're going to see. Trim off leading and trailing 
    1117    * zeros, which will just confuse us later, and adjust 
    1118    * our initial decimal exponent accordingly. 
    1119    * To review: 
    1120    * we have seen i total characters. 
    1121    * nLeadZero of them were zeros before any other digits. 
    1122    * nTrailZero of them were zeros after any other digits. 
    1123    * if (decSeen), then a . was seen after decPt characters 
    1124    * (including leading zeros which have been discarded) 
    1125    * nDigits characters were neither lead nor trailing 
    1126    * zeros, nor point 
    1127    */ 
    1128    /* 
    1129    * special hack: if we saw no non-zero digits, then the 
    1130    * answer is zero! 
    1131    * Unfortunately, we feel honor-bound to keep parsing! 
    1132    */ 
    1133    if (nDigits == 0){ 
    1134     digits = zero; 
    1135     nDigits = 1; 
    1136     if (nLeadZero == 0){ 
    1137      // we saw NO DIGITS AT ALL, 
    1138      // not even a crummy 0! 
    1139      // this is not allowed. 
    1140      break parseNumber; // go throw exception 
    1141     } 
    1142 
    1143    } 
    1144 
    1145    /* Our initial exponent is decPt, adjusted by the number of 
    1146    * discarded zeros. Or, if there was no decPt, 
    1147    * then its just nDigits adjusted by discarded trailing zeros. 
    1148    */ 
    1149    if (decSeen){ 
    1150     decExp = decPt - nLeadZero; 
    1151    } else { 
    1152     decExp = nDigits+nTrailZero; 
    1153    } 
    1154 
    1155    /* 
    1156    * Look for 'e' or 'E' and an optionally signed integer. 
    1157    */ 
    1158    if ((i < l) && (((c = in.charAt(i))=='e') || (c == 'E'))){ 
    1159     int expSign = 1; 
    1160     int expVal = 0; 
    1161     int reallyBig = Integer.MAX_VALUE/10; 
    1162     boolean expOverflow = false; 
    1163     switch(in.charAt(++i)){ 
    1164     case '-': 
    1165      expSign = -1; 
    1166      //FALLTHROUGH 
    1167     case '+': 
    1168      i++; 
    1169     } 
    1170     int expAt = i; 
    1171    expLoop: 
    1172     while (i < l ){ 
    1173      if (expVal >= reallyBig){ 
    1174       // the next character will cause integer 
    1175       // overflow. 
    1176       expOverflow = true; 
    1177      } 
    1178      switch (c = in.charAt(i++)){ 
    1179      case '0': 
    1180      case '1': 
    1181      case '2': 
    1182      case '3': 
    1183      case '4': 
    1184      case '5': 
    1185      case '6': 
    1186      case '7': 
    1187      case '8': 
    1188      case '9': 
    1189       expVal = expVal*10 + ((int)c - (int)'0'); 
    1190       continue; 
    1191      default: 
    1192       i--;   // back up. 
    1193       break expLoop; // stop parsing exponent. 
    1194      } 
    1195     } 
    1196     int expLimit = bigDecimalExponent+nDigits+nTrailZero; 
    1197     if (expOverflow || (expVal > expLimit)){ 
    1198      // 
    1199      // The intent here is to end up with 
    1200      // infinity or zero, as appropriate. 
    1201      // The reason for yielding such a small decExponent, 
    1202      // rather than something intuitive such as 
    1203      // expSign*Integer.MAX_VALUE, is that this value 
    1204      // is subject to further manipulation in 
    1205      // doubleValue() and floatValue(), and I don't want 
    1206      // it to be able to cause overflow there! 
    1207      // (The only way we can get into trouble here is for 
    1208      // really outrageous nDigits+nTrailZero, such as 2 billion.) 
    1209      // 
    1210      decExp = expSign*expLimit; 
    1211     } else { 
    1212      // this should not overflow, since we tested 
    1213      // for expVal > (MAX+N), where N >= abs(decExp) 
    1214      decExp = decExp + expSign*expVal; 
    1215     } 
    1216 
    1217     // if we saw something not a digit (or end of string) 
    1218     // after the [Ee][+-], without seeing any digits at all 
    1219     // this is certainly an error. If we saw some digits, 
    1220     // but then some trailing garbage, that might be ok. 
    1221     // so we just fall through in that case. 
    1222     // HUMBUG 
    1223     if (i == expAt) 
    1224      break parseNumber; // certainly bad 
    1225    } 
    1226    /* 
    1227    * We parsed everything we could. 
    1228    * If there are leftovers, then this is not good input! 
    1229    */ 
    1230    if (i < l && 
    1231     ((i != l - 1) || 
    1232     (in.charAt(i) != 'f' && 
    1233     in.charAt(i) != 'F' && 
    1234     in.charAt(i) != 'd' && 
    1235     in.charAt(i) != 'D'))) { 
    1236     break parseNumber; // go throw exception 
    1237    } 
    1238 
    1239    return new FloatingDecimal(isNegative, decExp, digits, nDigits, false); 
    1240   } catch (StringIndexOutOfBoundsException e){ } 
    1241   throw new NumberFormatException("For input string: \"" + in + "\""); 
    1242  }