Take a character from a string as an array element

Source: Internet
Author: User

Take a character from a string as an array element
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 );
     
    }
  }
 
Add value to data

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 );
  }
}

Convert to byte to a hexadecimal string


Import java. io. unsupportedencodingexception;
/**
* Format validation
 *
* This class encodes/decodes hexadecimal data
* @ Author jeffrey rodriguez
* @ Version $ id: hexbin. java 125124 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 <baselength; 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 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 <lengthdata; I ++ ){
Encodeddata [I * 2] = lookuphexalphabet [(binarydata [I]> 4) & 0xf];
Encodeddata [I * 2 + 1] = lookuphexalphabet [binarydata [I] & 0xf];
      }
Return encodeddata;
  }
}

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.