Summary of common data conversion methods for Android and MCU communication _android

Source: Internet
Author: User

Common data conversion methods for Android and MCU communication

1. Convert GB2312 into Chinese, such as bafac2dcb2b7→ carrots, two bytes to synthesize a text

public static string STRINGTOGBK (String string) throws Exception { 
  byte[] bytes = new Byte[string.length ()/2]; 
  for (int j = 0; J < Bytes.length; J +) { 
    byte high = Byte.parsebyte (String.substring (J * 2, J * 2 + 1); 
    byte low = Byte.parsebyte (String.substring (J * 2 + 1, J * 2 + 2) 
        ; 
    BYTES[J] = (byte) (High << 4 | low); 
  } 
  string result = new String (bytes, "GBK"); 
  return result; 
} 

 2. Convert Chinese to GB2312, and the results are returned in byte[] form, such as Carrot →new byte[]{ba FA C2 DC B2 B7}, one word is divided into two bytes

public static byte[] Gbktostring (String str) throws Exception {return 
  new string (Str.getbytes ("GBK"), "gb2312"). GetBytes ("gb2312"); 
} 

3. Convert hexadecimal byte[] to string, such as byte[]{0x7e,0x80,0x11,0x20}→7e801120, which can be used for log printing

public static String bytestohexstring (byte[] src) { 
  StringBuilder StringBuilder = new StringBuilder (""); 
  if (src = null | | src.length <= 0) {return 
    null; 
  } 
  for (int i = 0; i < src.length i++) { 
    int v = src[i] & 0xFF; 
    String HV = integer.tohexstring (v); 
    if (Hv.length () < 2) { 
      stringbuilder.append (0); 
    } 
    Stringbuilder.append (HV); 
  } 
  return stringbuilder.tostring (); 
} 

 4. Converts hexadecimal byte[] to string, and each byte is separated by a space, such as byte[]{0x7e,0x80,0x11,0x20}→7e 80 11 20, which can be used for log printing

public static StringBuilder Byte2hexstr (byte[] Data { 
 
  if (data!= null && data.length > 0) { 
    Stringbui Lder StringBuilder = new StringBuilder (data.length); 
    for (byte bytechar:data) { 
      stringbuilder.append (String.Format ("%02x", Bytechar)); 
    return stringBuilder; 
  } 
  return null; 
} 

 5. Convert byte[] Array to 8, 10, 16 and other systems, such as byte[0x11,0x20]→4384, approximately 1120 (16) →4384,radix represents the system

public static String bytestoallhex (byte[] bytes, int radix) {return 
  new BigInteger (1, bytes). toString (radix); 1 represents positive numbers 
} 

6. Converts the hexadecimal of a string to hexadecimal with byte, such as 7e20→new byte[]{0x7e,x20}

public static byte[] Hexstring2bytes (String src) { 
  byte[] ret = new Byte[src.length ()/2]; 
  byte[] tmp = Src.getbytes (); 
  for (int i = 0; i < TMP.LENGTH/2 i++) { 
    Ret[i] = unitebytes (Tmp[i * 2], Tmp[i * 2 + 1]); 
  return ret; 
} 

public static byte Unitebytes (byte src0, byte src1) { 
  byte _b0 = Byte.decode ("0x" + new String (new byte[) {SRC0}) C11/>.bytevalue (); 
  _b0 = (byte) (_b0 << 4); 
  byte _b1 = Byte.decode ("0x" + new String (new byte[] {SRC1})) 
      . Bytevalue (); 
  BYTE ret = (byte) (_b0 ^ _b1); 
  return ret; 

The above is on the Android and SCM Communication data collation, follow-up continue to supplement the relevant information thank you for your support of this site!

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.