一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

从字符串中取一个字符作为数组元素

时间:2011-04-26 编辑:简简单单 来源:一聚教程网

从字符串中取一个字符作为数组元素
public class mainclass {

  public static void main(string[] arg) {
    string text = "to be or not to be";        // define a string
    byte[] textarray = text.getbytes();
   
    for(byte b: textarray){
      system.out.println(b);
     
    }
  }
 
给数据增加值

class substringcons {
  public static void main(string args[]) {
    byte ascii[] = { 65, 66, 67, 68, 69, 70 };

    string s1 = new string(ascii);
    system.out.println(s1);

    string s2 = new string(ascii, 2, 3);
    system.out.println(s2);
  }
}

转换成字节来一个十六进制字符串


import java.io.unsupportedencodingexception;
/**
 * format validation
 *
 * this class encodes/decodes hexadecimal data
 * @author jeffrey rodriguez
 * @version $id: hexbin.java 125124 2005-01-14 00:23:54z kkrouse $
 */
public class main {
  static private final int  baselength   = 255;
  static private final int  lookuplength = 16;
  static private byte [] hexnumbertable    = new byte[baselength];
  static private byte [] lookuphexalphabet = new byte[lookuplength];


  static {
      for (int i = 0; i           hexnumbertable[i] = -1;
      }
      for ( int i = '9'; i >= '0'; i--) {
          hexnumbertable[i] = (byte) (i-'0');
      }
      for ( int i = 'f'; i>= 'a'; i--) {
          hexnumbertable[i] = (byte) ( i-'a' + 10 );
      }
      for ( int i = 'f'; i>= 'a'; i--) {
         hexnumbertable[i] = (byte) ( i-'a' + 10 );
      }

      for(int i = 0; i<10; i++ )
          lookuphexalphabet[i] = (byte) ('0'+i );
      for(int i = 10; i<=15; i++ )
          lookuphexalphabet[i] = (byte) ('a'+i -10);
  }

  /**
   * converts bytes to a hex string
   */
  static public string bytestostring(byte[] binarydata)
  {
      if (binarydata == null)
          return null;
      return new string(encode(binarydata));
  }
  /**
   * array of byte to encode
   *
   * @param binarydata
   * @return return encode binary array
   */
  static public byte[] encode(byte[] binarydata) {
      if (binarydata == null)
          return null;
      int lengthdata   = binarydata.length;
      int lengthencode = lengthdata * 2;
      byte[] encodeddata = new byte[lengthencode];
      for( int i = 0; i           encodeddata[i*2] = lookuphexalphabet[(binarydata[i] >> 4) & 0xf];
          encodeddata[i*2+1] = lookuphexalphabet[ binarydata[i] & 0xf];
      }
      return encodeddata;
  }
}

热门栏目