Java binary, byte array, character, hexadecimal, BCD encoding conversion, GPS, GIS

Source: Internet
Author: User

 

Java binary, byte array, character, hexadecimal, BCD encoding conversion /***//**
* Convert a hexadecimal string to a byte array.
* @ Param hex
* @ Return
*/
Public static byte [] hexstringtobyte (string HEX ){
Int Len = (Hex. Length ()/2 );
Byte [] result = new byte [Len];
Char [] Achar = Hex. tochararray ();
For (INT I = 0; I <Len; I ++ ){
Int Pos = I * 2;
Result [I] = (byte) (tobyte (Achar [POS]) <4 | tobyte (Achar [POS + 1]);
}
Return result;
}

Private Static byte tobyte (char c ){
Byte B = (byte) "0123456789 abcdef". indexof (C );
Return B;
}

/***//**
* Converts a byte array to a hexadecimal string.
* @ Param barray
* @ Return
*/
Public static final string bytestohexstring (byte [] barray ){
Stringbuffer sb = new stringbuffer (barray. Length );
String stemp;
For (INT I = 0; I <barray. length; I ++ ){
Stemp = integer. tohexstring (0xff & barray [I]);
If (stemp. Length () <2)
SB. append (0 );
SB. append (stemp. touppercase ());
}
Return sb. tostring ();
}

/***//**
* Converts a byte array to an object.
* @ Param bytes
* @ Return
* @ Throws ioexception
* @ Throws classnotfoundexception
*/
Public static final object bytestoobject (byte [] bytes) throws ioexception, classnotfoundexception {
Bytearrayinputstream in = new bytearrayinputstream (bytes );
Objectinputstream OI = new objectinputstream (in );
Object o = Oi. readobject ();
Oi. Close ();
Return O;
}

/***//**
* Convert a serializable object to a byte array
* @ Param s
* @ Return
* @ Throws ioexception
*/
Public static final byte [] objecttobytes (serializable s) throws ioexception {
Bytearrayoutputstream out = new bytearrayoutputstream ();
Objectoutputstream ot = new objectoutputstream (out );
Ot. writeobject (s );
Ot. Flush ();
Ot. Close ();
Return out. tobytearray ();
}

Public static final string objecttohexstring (serializable s) throws ioexception {
Return bytestohexstring (objecttobytes (s ));
}

Public static final object hexstringtoobject (string HEX) throws ioexception, classnotfoundexception {
Return bytestoobject (hexstringtobyte (HEX ));
}

/***//**
* @ Function: Convert BCD code into a 10-digit string (Arabic data)
* @ Input parameter: BCD code
* @ Output result: 10-digit string
*/
Public static string bcd2str (byte [] bytes ){
Stringbuffer temp = new stringbuffer (bytes. length * 2 );

For (INT I = 0; I <bytes. length; I ++ ){
Temp. append (byte) (Bytes [I] & 0xf0) >>> 4 ));
Temp. append (byte) (Bytes [I] & 0x0f ));
}
Return temp. tostring (). substring (0, 1). Then signorecase ("0 ")? Temp. tostring (). substring (1): temp. tostring ();
}

/***//**
* @ Function: convert a 10-digit string to a BCD code
* @ Input parameter: 10 hexadecimal string
* @ Output result: BCD code
*/
Public static byte [] str2bcd (string ASC ){
Int Len = ASC. Length ();
Int mod = Len % 2;

If (mod! = 0 ){
ASC = "0" + ASC;
Len = ASC. Length ();
}

Byte abt [] = new byte [Len];
If (LEN> = 2 ){
Len = Len/2;
}

Byte bbt [] = new byte [Len];
Abt = ASC. getbytes ();
Int J, K;

For (INT p = 0; P <ASC. Length ()/2; p ++ ){
If (ABT [2 * p]> = '0') & (ABT [2 * p] <= '9 ')){
J = abt [2 * p]-'0 ';
} Else if (ABT [2 * p]> = 'A') & (ABT [2 * p] <= 'Z ')){
J = abt [2 * p]-'A' + 0x0a;
} Else {
J = abt [2 * p]-'A' + 0x0a;
}

If (ABT [2 * p + 1]> = '0') & (ABT [2 * p + 1] <= '9 ')){
K = abt [2 * p + 1]-'0 ';
} Else if (ABT [2 * p + 1]> = 'A') & (ABT [2 * p + 1] <= 'Z ')){
K = abt [2 * p + 1]-'A' + 0x0a;
} Else {
K = abt [2 * p + 1]-'A' + 0x0a;
}

Int A = (j <4) + K;
Byte B = (byte);
Bbt [p] = B;
}
Return bbt;
}
/***//**
* @ Function: Convert BCD code to ASC code
* @ Input parameter: BCD string
* @ Output result: ASC code
*/
Public static string bcd2asc (byte [] bytes ){
Stringbuffer temp = new stringbuffer (bytes. length * 2 );

For (INT I = 0; I <bytes. length; I ++ ){
Int H = (Bytes [I] & 0xf0) >>> 4 );
Int L = (Bytes [I] & 0x0f );
Temp. append (btoa [H]). append (btoa [l]);
}
Return temp. tostring ();
}

/***//**
* MD5 encrypted string. The encrypted hexadecimal string is returned.
* @ Param Origin
* @ Return
*/
Public static string md5encodetohex (string origin ){
Return bytestohexstring (md5encode (origin ));
}

/***//**
* MD5 encrypted string. The encrypted byte array is returned.
* @ Param Origin
* @ Return
*/
Public static byte [] md5encode (string origin ){
Return md5encode (origin. getbytes ());
}

/***//**
* MD5 encrypted byte array. The encrypted byte array is returned.
* @ Param bytes
* @ Return
*/
Public static byte [] md5encode (byte [] bytes ){
Messagedigest MD = NULL;
Try {
MD = messagedigest. getinstance ("MD5 ");
Return Md. Digest (bytes );
} Catch (nosuchalgorithmexception e ){
E. printstacktrace ();
Return new byte [0];
}

}
// About byte: signed byte 0x00 ~ 0xff ing to 0 ~ 127 and-128 ~ -For two segments in 1, it is relatively simple to use (B + 256) % 256 to bring the value back to 0 ~ 255, or use & 0xff and assign it to an int. Reference http://www.jsfsoft.com: 8080/beyond-pebble/pinxue/2006/08/23/1156309692525 .html

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.