Android Open Source - UsbSerial Hex Data






From Project

Back to project page UsbSerial.

License

The source code is released under:

MIT License

If you think the Android project UsbSerial listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.felhr.utils;
//from  w ww. j a  v  a  2s  .c o  m
public class HexData 
{
  private static final String HEXES = "0123456789ABCDEF";
  private static final String HEX_INDICATOR = "0x";
  private static final String SPACE = " ";
  
  private HexData()
  {
    
  }
  
  public static String hexToString(byte[] data)
  {
    if(data != null)
    {
      StringBuilder hex = new StringBuilder(2*data.length);
      for(int i=0;i<=data.length-1;i++)
      {
        byte dataAtIndex = data[i];
        hex.append(HEX_INDICATOR);
        hex.append(HEXES.charAt((dataAtIndex & 0xF0) >> 4))
              .append(HEXES.charAt((dataAtIndex & 0x0F)));
        hex.append(SPACE);
      }
      return hex.toString();
    }else
    {
      return null;
    }
  }
  
  public static byte[] stringTobytes(String hexString)
  {
    String stringProcessed = hexString.trim().replaceAll("0x", "");
    stringProcessed = stringProcessed.replaceAll("\\s+","");
    byte[] data = new byte[stringProcessed.length()/2];
    int i = 0;
    int j = 0;
    while(i <= stringProcessed.length()-1)
    {
      byte character = (byte) Integer.parseInt(stringProcessed.substring(i, i+2), 16);
      data[j] = character;
      j++;
      i += 2;
    }
    return data;
  }
  
  public static String hex4digits(String id)
  {
    if(id.length() == 1) return "000" + id;
    if(id.length() == 2) return "00" + id;
    if(id.length() == 3) return "0" + id;
    else return id;
  }
}




Java Source Code List

com.felhr.deviceids.CP210xIds.java
com.felhr.deviceids.FTDISioIds.java
com.felhr.deviceids.PL2303Ids.java
com.felhr.usbserial.BLED112SerialDevice.java
com.felhr.usbserial.CDCSerialDevice.java
com.felhr.usbserial.CP2102SerialDevice.java
com.felhr.usbserial.FTDISerialDevice.java
com.felhr.usbserial.PL2303SerialDevice.java
com.felhr.usbserial.SerialBuffer.java
com.felhr.usbserial.UsbSerialDebugger.java
com.felhr.usbserial.UsbSerialDevice.java
com.felhr.usbserial.UsbSerialInterface.java
com.felhr.utils.HexData.java