OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 class ByteListUtils { |
| 6 static String toRawBytesAsString(List<int> bytes) { |
| 7 List<String> _bytes = []; |
| 8 for(int b in bytes) { |
| 9 _bytes.add(toHex(b)); |
| 10 } |
| 11 return Strings.join(_bytes, " "); |
| 12 } |
| 13 |
| 14 static final String HEX_CHARS = "0123456789abcdef"; |
| 15 static String toHex(int dec, [int minLength = 2]) { |
| 16 List<int> hexCodes = []; |
| 17 int len = 0; |
| 18 while (len < minLength || dec > 0) { |
| 19 hexCodes.add(HEX_CHARS.charCodeAt(dec & 0xf)); |
| 20 dec >>= 4; |
| 21 len ++; |
| 22 } |
| 23 List<int> strSrc = []; |
| 24 for (int i = hexCodes.length - 1; i >= 0; i--) { |
| 25 strSrc.add(hexCodes[i]); |
| 26 } |
| 27 return new String.fromCharCodes(strSrc); |
| 28 } |
| 29 |
| 30 static String toBits(int dec, [int minLength = 8]) { |
| 31 Queue<String> q = new Queue(); |
| 32 int len = 0; |
| 33 while (len < minLength || dec > 0) { |
| 34 if(len > 0 && len % 4 == 0) q.addFirst(" "); |
| 35 q.addFirst("${dec & 0x1}"); |
| 36 dec >>= 1; |
| 37 len ++; |
| 38 } |
| 39 StringBuffer sb = new StringBuffer(); |
| 40 for(String i in q) { |
| 41 sb.add(i); |
| 42 } |
| 43 return sb.toString(); |
| 44 } |
| 45 } |
OLD | NEW |