天天看点

不同进制的数字字符串互相转换

1.import java.io.ByteArrayInputStream;

2.import java.io.ByteArrayOutputStream;

3.import java.io.IOException;

4.import java.io.ObjectInputStream;

5.import java.io.ObjectOutputStream;

6.import java.io.Serializable;

7.import java.security.MessageDigest;

8.import java.security.NoSuchAlgorithmException;

9.

10.public final class Converts {

11.

12. public final static char[] BToA = "0123456789abcdef".toCharArray() ;

13. private Converts() {

14. }

15.

16. /**

17. * 把16进制字符串转换成字节数组

18. * @param hex

19. * @return

20. */

21. public static byte[] hexStringToByte(String hex) {

22. int len = (hex.length() / 2);

23. byte[] result = new byte[len];

24. char[] achar = hex.toCharArray();

25. for (int i = 0; i < len; i++) {

26. int pos = i * 2;

27. result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));

28. }

29. return result;

30. }

31.

32. private static byte toByte(char c) {

33. byte b = (byte) "0123456789ABCDEF".indexOf(c);

34. return b;

35. }

36.

37. /**

38. * 把字节数组转换成16进制字符串

39. * @param bArray

40. * @return

41. */

42. public static final String bytesToHexString(byte[] bArray) {

43. StringBuffer sb = new StringBuffer(bArray.length);

44. String sTemp;

45. for (int i = 0; i < bArray.length; i++) {

46. sTemp = Integer.toHexString(0xFF & bArray[i]);

47. if (sTemp.length() < 2)

48. sb.append(0);

49. sb.append(sTemp.toUpperCase());

50. }

51. return sb.toString();

52. }

53.

54. /**

55. * 把字节数组转换为对象

56. * @param bytes

57. * @return

58. * @throws IOException

59. * @throws ClassNotFoundException

60. */

61. public static final Object bytesToObject(byte[] bytes) throws IOException, ClassNotFoundException {

62. ByteArrayInputStream in = new ByteArrayInputStream(bytes);

63. ObjectInputStream oi = new ObjectInputStream(in);

64. Object o = oi.readObject();

65. oi.close();

66. return o;

67. }

68.

69. /**

70. * 把可序列化对象转换成字节数组

71. * @param s

72. * @return

73. * @throws IOException

74. */

75. public static final byte[] objectToBytes(Serializable s) throws IOException {

76. ByteArrayOutputStream out = new ByteArrayOutputStream();

77. ObjectOutputStream ot = new ObjectOutputStream(out);

78. ot.writeObject(s);

79. ot.flush();

80. ot.close();

81. return out.toByteArray();

82. }

83.

84. public static final String objectToHexString(Serializable s) throws IOException{

85. return bytesToHexString(objectToBytes(s));

86. }

87.

88. public static final Object hexStringToObject(String hex) throws IOException, ClassNotFoundException{

89. return bytesToObject(hexStringToByte(hex));

90. }

91.

92. /**

93. * @函数功能: BCD码转为10进制串(阿拉伯数据)

94. * @输入参数: BCD码

95. * @输出结果: 10进制串

96. */

97. public static String bcd2Str(byte[] bytes){

98. StringBuffer temp=new StringBuffer(bytes.length*2);

99.

100. for(int i=0;i<bytes.length;i++){

101. temp.append((byte)((bytes[i]& 0xf0)>>>4));

102. temp.append((byte)(bytes[i]& 0x0f));

103. }

104. return temp.toString().substring(0,1).equalsIgnoreCase("0")?temp.toString().substring(1):temp.toString();

105. }

106.

107. /**

108. * @函数功能: 10进制串转为BCD码

109. * @输入参数: 10进制串

110. * @输出结果: BCD码

111. */

112. public static byte[] str2Bcd(String asc) {

113. int len = asc.length();

114. int mod = len % 2;

115.

116. if (mod != 0) {

117. asc = "0" + asc;

118. len = asc.length();

119. }

120.

121. byte abt[] = new byte[len];

122. if (len >= 2) {

123. len = len / 2;

124. }

125.

126. byte bbt[] = new byte[len];

127. abt = asc.getBytes();

128. int j, k;

129.

130. for (int p = 0; p < asc.length()/2; p++) {

131. if ( (abt[2 * p] >= '0') && (abt[2 * p] <= '9')) {

132. j = abt[2 * p] - '0';

133. } else if ( (abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) {

134. j = abt[2 * p] - 'a' + 0x0a;

135. } else {

136. j = abt[2 * p] - 'A' + 0x0a;

137. }

138.

139. if ( (abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) {

140. k = abt[2 * p + 1] - '0';

141. } else if ( (abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) {

142. k = abt[2 * p + 1] - 'a' + 0x0a;

143. }else {

144. k = abt[2 * p + 1] - 'A' + 0x0a;

145. }

146.

147. int a = (j << 4) + k;

148. byte b = (byte) a;

149. bbt[p] = b;

150. }

151. return bbt;

152. }

153.

154. public static String BCD2ASC(byte[] bytes) {

155. StringBuffer temp = new StringBuffer(bytes.length * 2);

156.

157. for (int i = 0; i < bytes.length; i++) {

158. int h = ((bytes[i] & 0xf0) >>> 4);

159. int l = (bytes[i] & 0x0f);

160. temp.append(BToA[h]).append( BToA[l]);

161. }

162. return temp.toString() ;

163. }

164.

165. /**

166. * MD5加密字符串,返回加密后的16进制字符串

167. * @param origin

168. * @return

169. */

170. public static String MD5EncodeToHex(String origin) {

171. return bytesToHexString(MD5Encode(origin));

172. }

173.

174. /**

175. * MD5加密字符串,返回加密后的字节数组

176. * @param origin

177. * @return

178. */

179. public static byte[] MD5Encode(String origin){

180. return MD5Encode(origin.getBytes());

181. }

182.

183. /**

184. * MD5加密字节数组,返回加密后的字节数组

185. * @param bytes

186. * @return

187. */

188. public static byte[] MD5Encode(byte[] bytes){

189. MessageDigest md=null;

190. try {

191. md = MessageDigest.getInstance("MD5");

192. return md.digest(bytes);

193. } catch (NoSuchAlgorithmException e) {

194. e.printStackTrace();

195. return new byte[0];

196. }

197.

198. }

199.}

200.

201.