前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >笔记75 | Java字节、十进制、十六进制、字符串之间的相互转换

笔记75 | Java字节、十进制、十六进制、字符串之间的相互转换

作者头像
项勇
发布2018-06-19 16:31:56
1.5K0
发布2018-06-19 16:31:56
举报
文章被收录于专栏:项勇
  1. /*
  2. * 字节转10进制
  3. */
  4. public static int byte2Int(byte b){
  5. int r = (int)b;
  6. return r;
  7. }
  8. /*
  9. * 10进制转字节
  10. */
  11. public static byte int2Byte(int i){
  12. byte r = (byte)i;
  13. return r;
  14. }
  15. /*
  16. * 字节数组转16进制字符串
  17. */
  18. public static String bytes2HexString(byte[] b){
  19. String r = " ";
  20. for (int i = 0; i < b.length; i++) {
  21. String hex = Integer.toHexString(b[i] & 0xFF);
  22. if (hex.length() == 1) {
  23. hex = "0"+hex;
  24. }
  25. r += hex.toUpperCase();
  26. }
  27. return r;
  28. }
  29. /*
  30. * 10进制字符串转字节数组
  31. */
  32. //字符转为字节
  33. private static byte charToByte(char c){
  34. return (byte)"0123456789ABCDEF".indexOf(c);
  35. }
  36. //16进制字符串转字节数组
  37. public static byte[] hexString2Bytes(String hex){
  38. if ((hex==null)||(hex.equals(""))) {
  39. return null;
  40. }else if (hex.length()%2 != 0) {
  41. return null;
  42. }else{
  43. hex = hex.toUpperCase();
  44. int len = hex.length()/2;
  45. byte[] b = new byte[len];
  46. char[] hc = hex.toCharArray();
  47. for (int i = 0; i < len; i++) {
  48. int p=2*i;
  49. b[i] = (byte)(charToByte(hc[p])<<4 | charToByte(hc[p+1]));
  50. }
  51. return b;
  52. }
  53. }
  54. /*
  55. * 字节数组转字符串
  56. */
  57. public static String bytes2String(byte[] b) throws UnsupportedEncodingException{
  58. String r = null;
  59. r = new String(b,"GBK");
  60. return r;
  61. }
  62. /*
  63. * 字符串转字节组
  64. */
  65. public static byte[] string2Bytes(String s){
  66. byte[] r = s.getBytes();
  67. return r;
  68. }
  69. /*
  70. * 16进组字符串转字符串
  71. */
  72. public static String hex2String(String hex){
  73. String r = bytes2HexString(hexString2Bytes(hex));
  74. return r;
  75. }
  76. /*
  77. * 字符串转16进组字符串
  78. */
  79. public static String string2HexString(String s){
  80. String r = bytes2HexString(string2Bytes(s));
  81. return r;
  82. }
  83. private void LoandFragment(Fragment fragment){
  84. FragmentManager fm = getFragmentManager();
  85. FragmentTransaction ft = fm.beginTransaction();
  86. ft.replace(R.id.fm, fragment);
  87. ft.commitAllowingStateLoss();
  88. }
  89. private void initDate(){
  90. byte b1 = (byte) 45;
  91. Log.i("md", "1.字节转10进制:" + byte2Int(b1));
  92. int i = 89;
  93. Log.i("md", "2.10进制转字节:" + int2Byte(i));
  94. byte[] b2 = new byte[]{(byte)0xFF, (byte)0x5F, (byte)0x6, (byte)0x5A};
  95. Log.i("md", "3.字节数组转16进制字符串:" + bytes2HexString(b2));
  96. String s1 = new String("1DA47C");
  97. Log.i("md", "4.16进制字符串转字节数组:" + Arrays.toString(hexString2Bytes(s1)));
  98. try {
  99. Log.i("md", "5.字节数组转字符串:" + bytes2String(b2));
  100. } catch (UnsupportedEncodingException e) {
  101. // TODO Auto-generated catch block
  102. e.printStackTrace();
  103. }
  104. Log.i("md", "6.字符串转字节数组:" + Arrays.toString(string2Bytes(s1)));
  105. Log.i("md", "7.16进制字符串转字符串:" + hex2String(s1));
  106. String s2 = new String("Hello!");
  107. Log.i("md", "8.字符串转16进制字符串:" + string2HexString(s2));
  108. }
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2018-05-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 项勇 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档