Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - How to realize the conversion between BCD code string and 16 binary string in java?
How to realize the conversion between BCD code string and 16 binary string in java?
New territories trunk line (empty)

4{

5 unsigned character array [4] = {"0x0 ","0x0 ","0x02 ","0xe7 "};

6 unsigned long numbers;

7 num = 0;

8 for(int I = 0; I & ltsizeof (array); i++)

9 {

10 num & lt; & lt=8;

1 1 num | = array[I];

12 }

13 printf("num = %d ",number);

14 returns 0;

15

16}

Binary, byte array, character, hexadecimal, BCD coding conversion

* Convert 16 binary string into byte array.

* @param hex

* @ Return

*/

Public static byte [] Hexstringtobyte (string hexadecimal) {

int len =(hex . length()/2);

Byte[] result = new byte [len];

char[]achar = hex . tochararray();

for(int I = 0; I & ltleni++) {

int pos = I * 2;

result[I]=(byte)(toByte(achar[pos])& lt; & lt4 | toByte(achar[pos+ 1]);

}

Return the result;

}

Private static byte-to-byte (char c) (

byte b =(byte)" 0 123456789 abcdef "。 (c) Index of;

Return to b;

}

Public static final string bytestohexstring (byte [] barray) {

string buffer sb = new string buffer(barray . length);

String sTemp

for(int I = 0; I & ltbArray. Length; i++) {

sTemp = integer . tohexstring(0x ff & amp; bArray[I]);

if(stemp . length()& lt; 2)

sb . append(0);

sb . append(stemp . toupper case());

}

return sb . tostring();

}

Public static final object bytesToObject(byte[] bytes) throws IOException, ClassNotFoundException {

ByteArrayInputStream in = New ByteArrayInputStream (bytes);

ObjectInputStream oi = new ObjectInputStream (in);

object o = oi . read object();

oi . close();

Return o;

}

The public static final byte [] object byte (serializable) throws IOException {

ByteArrayOutputStream out = new ByteArrayOutputStream();

object output stream ot = new object output stream(out);

ot.writeObject

ot . flush();

ot . close();

return out . tobytearray();

}

The public static final string objecttohexstring (serializables) throws IOException{

Returns bytestohexstring (objecttobytes (s));

}

The public static final object hexstringtoobject (stringhex) throws IOException, ClassNotFoundException{

Returns bytestoobject (Hexstringtobyte (hex));

}

Public static string bcd2Str(byte[] bytes){

string buffer temp = new string buffer(bytes . length * 2);

for(int I = 0; I< bytes. Length; i++){

temp . append((byte)((bytes[I]& amp; 0xf0)>& gt& gt4));

temp . append((byte)(bytes[I]& amp; 0x0f));

}

Returns temp.toString (). Substring (0, 1). equalsIgnoreCase("0 ")? temp.toString()。 substring( 1):temp . tostring();

}

Common 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 & gt; = 2) {

len = len/2;

}

Byte bbt[] = new byte [len];

abt = ASC . getbytes();

int j,k;

for(int p = 0; p & ltASC . length()/2; p++) {

if((abt[2 * p]& gt; = ' 0 ')& amp; & amp(abt[2 * p]& lt; = '9')) {

j = abt[2 * p]--0;

} else if((abt[2 * p]& gt; = ' a ')& amp; & amp(abt[2 * p]& lt; = ' z '){

j = abt[2 * p]-' a '+0;

} Otherwise {

j = abt[2 * p]-' A '+0;

}

if((abt[2 * p+ 1]>; = ' 0 ')& amp; & amp(abt[2 * p+ 1]& lt; = '9')) {

k = abt[2 * p+ 1]-' 0 ';

} else if((abt[2 * p+ 1]& gt; = ' a ')& amp; & amp(abt[2 * p+ 1]& lt; = ' z '){

k = abt[2 * p+ 1]-' a '+0;

} Otherwise {

k = abt[2 * p+ 1]-' A '+0;

}

int a =(j & lt; & lt4)+k;

Byte b = (byte) a;

bbt[p]= b;

}

Return to bbt

}

Public static string BCD2ASC(byte[] bytes) {

string buffer temp = new string buffer(bytes . length * 2);

for(int I = 0; I< bytes. Length; i++) {

int h =((bytes[I]& amp; 0xf0)>& gt& gt4);

int l =(bytes[I]& amp; 0x0f);

temp.append(BToA[h])。 append(BToA[l]);

}

return temp . tostring();

}

Public static string MD5EncodeToHex (string source) (

Returns bytestohexstring (Md5encode (origin));

}

Public static byte [] MD5Encode (string source) {

Returns md5encode (origin. getbytes ());

}

Common static byte [] MD5Encode (byte [] byte) {

MessageDigest md = null

Try {

MD = message digest . getinstance(" MD5 ");

Returns md.digest (bytes);

} catch(nosuch algorithm exception e){

e . printstacktrace();

Returns a new byte [0];

}

}

//About byte: signed bytes map 0x00 ~ 0xff into two segments: 0~ 127 and-128~- 1. A simpler method is to use (b+256)%256 to return its value to 0~255, or use&; 0xff and assigned to an int.