从Byte []读取一个数字

时间:2012-10-26 15:55:04

标签: java byte

  

可能重复:
  Retreive my number after make it in Byte[]

我希望在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 ?
}

4 个答案:

答案 0 :(得分:1)

首先,使用getBytes()而不指定编码 - 它将使用平台默认编码,这几乎不是你想要的。

鉴于你有字节是数字的 text 表示的二进制表示,听起来你基本上应该将它转换回字符串,然后使用Double.parseDouble(...)new BigDecimal(...)

如果你有一些“真正的二元”代表的数字,那将是另一回事 - 但这是一个内心的文本表示。

答案 1 :(得分:0)

如果你想要的是将s1集合转换为数字的表示,请执行以下操作:

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

如果你想要一个数字类型:

double dbl = Double.parseDouble(number);

答案 2 :(得分:0)

Double number = Double.parseDouble(new String(s1.toArray(new Byte [s1.size])))

答案 3 :(得分: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       }