Conversion between "Go" javabyte[] array and hexadecimal string util------include case and code

Source: Internet
Author: User

Original URL: http://blog.csdn.net/caijunjun1006/article/details/11740223

In Java, Byte uses a binary representation of 8 bits, and we know that each character of the 16 binary needs to be represented by a 4-bit bits (23 + 22 + 21 + 20 = 15), so we can convert each byte to two corresponding 16 characters, That is, the high 4-bit and low 4 bits of byte are converted to the corresponding 16-character H and L, and combined to get the result of byte conversion to the 16 binary string, new string (H) + new String (L). That is, byte represents only 2 bits in hexadecimal notation.

In the same way, the opposite conversion also converts two 16 characters into a byte, as in the same principle.

Based on the above, we can convert the byte[] array to a 16 binary string, and of course we can convert the 16 binary string to the byte[] array.

Here is the tool class that implements conversion between byte[] arrays and hexadecimal strings:

[Java]View Plaincopy
  1. Package text.com;
  2. Public class Bytesutil {
  3. /** 
  4. * Convert byte[] to hex string. Convert a byte array to a string
  5. * Here we can convert byte to int, then use integer.tohexstring (int) to convert to 16 binary string.
  6. * @param src byte[] Data
  7. * @return Hex string
  8. */
  9. public static String bytestohexstring (byte[] src) {
  10. StringBuilder StringBuilder = new StringBuilder ("");
  11. if (src = = Null | | src.length <= 0) {
  12. return null;
  13. }
  14. For (int i = 0; i < src.length; i++) {
  15. int v = src[i] & 0xFF;
  16. String HV = integer.tohexstring (v);
  17. if (Hv.length () < 2) {
  18. Stringbuilder.append (0);
  19. }
  20. Stringbuilder.append (hv+"");
  21. }
  22. return stringbuilder.tostring ();
  23. }
  24. /** 
  25. * Convert hex string to byte[] converts a string to a byte array
  26. * @param hexstring the hex string
  27. * @return byte[]
  28. */
  29. public static byte[] Hexstringtobytes (String hexstring) {
  30. if (hexstring = = Null | | hexstring.equals ("")) {
  31. return null;
  32. }
  33. hexstring = Hexstring.touppercase ();
  34. int length = Hexstring.length ()/ 2;
  35. char[] Hexchars = Hexstring.tochararray ();
  36. byte[] D = new byte[length];
  37. For (int i = 0; i < length; i++) {
  38. int pos = i * 2;
  39. D[i] = (byte) (Chartobyte (Hexchars[pos]) << 4 | chartobyte (Hexchars[pos + 1]));
  40. }
  41. return D;
  42. }
  43. /** 
  44. * Convert Char to Byte
  45. * @param c Char
  46. * @return Byte
  47. */
  48. private static byte chartobyte (char c) {
  49. return (byte) "0123456789ABCDEF". IndexOf (c);
  50. }
  51. }

Here is the test program:

[Java]View Plaincopy
  1. Package text.com;
  2. Public class Test {
  3. public static void Main (string[] args) {
  4. byte B1 = 11;
  5. byte b2 = 21;
  6. byte B3 = 31;
  7. byte b4 = 41;
  8. byte B5 = 51;
  9. byte B6 = 61;
  10. byte B7 = 71;
  11. byte B8 = 81;
  12. byte[] bytes = new byte[] {b1, b2, B3, B4, B5, B6, B7, B8};
  13. String hexstring = "0b 1f 3d 47 51";
  14. String str = bytesutil.bytestohexstring (bytes);
  15. SYSTEM.OUT.PRINTLN ("str--->" + str);
  16. byte[] hexstringtobytes = bytesutil.hexstringtobytes (hexstring);
  17. For (int i = 0; i < hexstringtobytes.length; i++) {
  18. System.out.println ("hexstringtobytes---->" + hexstringtobytes[i]);
  19. }
  20. }
  21. }


Console output:

STR--->0b 1f 3d 47 51
Hexstringtobytes---->11
Hexstringtobytes---->21
Hexstringtobytes---->31
Hexstringtobytes---->41
Hexstringtobytes---->51
Hexstringtobytes---->61
Hexstringtobytes---->71
Hexstringtobytes---->81

ok!

Conversion between "Go" javabyte[] array and hexadecimal string util------include case and code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.