Java socket gets hexadecimal data round integers and strings

Source: Internet
Author: User
Tags stringbuffer

Recently used in the development of Java docking a temperature detection equipment, the business is to use the scan two-dimensional code to confirm the identity, and then detect body temperature, and finally submitted to the server, the process used to the hexadecimal string and integer, string, Chinese characters and other conversion between.

Socket Receive code:

The code is as follows Copy Code

InputStream input = null;


public void Run () {
System.out.println ("1111111111111111111111111111111111111111111111111111111");
try {
Using loops to read from the socket the data sent from the client.
Boolean done=true;
try {
byte[] buf = new byte[100];
int len = 0;
int serial=1;
while (len = Input.read (buf))!=-1) {
{
Byte[] type={buf[4]};
String a=byteutil.printhexstring (type);
SYSTEM.OUT.PRINTLN ("message type" +a);
try {
Switch (a) {
Case "02":
Tiwenservice.dotiwen (buf,socket,serial)//Body temperature information
Break
Case "03"://Get the name corresponding to the two-dimensional code
Tiwenservice.getchild (buf,socket,serial);
Break
Case "01"://Device startup information
Tiwenservice.startup (buf,socket,serial);
Default
Break

}
serial++;
catch (Exception e) {
E.printstacktrace ();
}
}

catch (Exception e) {
E.printstacktrace ();
}
}


Then share the conversion code:

The code is as follows Copy Code
public static int Bytestoint (byte[] brefarr) {
int ioutcome = 0;
BYTE bloop;

for (int i = 0; i < brefarr.length; i++) {
Bloop = Brefarr[i];
Ioutcome + = (bloop & 0xFF) << (8 * i);
}
return ioutcome;
}
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 ();
}

byte-digit spin string
public static String printhexstring (byte[] b) {
String Hexs = "";
for (int i = 0; i < b.length; i++) {

String hex = integer.tohexstring (B[i] & 0xFF);
if (hex.length () = = 1) {
Hex = ' 0 ' + hex;
}
Hexs + = hex;

}
return hexs;
}

/*
* 16 in-process string reshaping
*/
public static int Oxstringtoint (String ox) throws Exception {
Ox = Ox.tolowercase ();
if (Ox.startswith ("0x")) {
Ox = ox.substring (2, Ox.length ());
}
Integer ri = 0;
Integer Oxlen = Ox.length ();
for (int i = 0; i < Oxlen; i++) {
char C = Ox.charat (i);
int h;
if ((' 0 ' <= C && c <= ' 9 ')) {
h = c-48;
else if ((' A ' <= C && c <= ' F ')) {
h = c-87;

else if (' A ' <= c && c <= ' F ') {
h = c-55;
} else {
Throw (new Exception ("Not a Integer");
}
byte left = (byte) ((Oxlen-i-1) * 4);
RI |= (h << left);
}
Return RI;

}

16 in-process string
public static string hexstring2string (String src) {
String temp = "";
for (int i = 0; i < src.length ()/2; i++) {
temp = Temp
+ (char) integer.valueof (src.substring (i * 2, I * 2 + 2),
). Bytevalue ();
}
return temp;
}

Integer to byte array
public static byte[] Inttobytes (int var) {
Byte[] B = new byte[2];
B[0]= (Byte) (var>>24);
B[1]= (Byte) (var>>16);
B[2]= (Byte) (var>>8);
B[3]= (Byte) (VAR);
B[0] = (byte) (var >> 8);
B[1] = (byte) (VAR);
return b;
}

/**
* Generate 16 feed additive and check code
*
* @param data to remove the check digit
* @return
*/
public static string Makechecksum (string data) {
if (Stringutils.isempty (data)) {
Return "";
}
int total = 0;
int len = Data.length ();
int num = 0;
while (Num < len) {
String s = data.substring (num, num + 2);
Total + = Integer.parseint (s, 16);
num = num + 2;
}
/**
* Use 256 to calculate the maximum is 255, that is, 16 ff
*/
int mod = total;
String hex = integer.tohexstring (mod & 0xFF);
Len = Hex.length ();
If the length of the check bit is not enough, fill 0, here is a two-digit checksum
if (Len < 2) {
Hex = "0" + hex;
}
return hex;
}
/**
* 16 binary string converted to byte array
* @param 16 Feed string
* @return converted byte array
*/
public static byte[] Hex2byte (String hex) {
String digital = "0123456789ABCDEF";
char[] Hex2char = Hex.tochararray ();
byte[] bytes = new Byte[hex.length ()/2];
int temp;
for (int i = 0; i < bytes.length; i++) {
In fact, it's the same as the above function. Multiple 16 is 4 digits to the right, which makes it a high 4.
And then add the lower four digits, equivalent to the bitwise operation "|"
The added number carries a bit "&" operation to prevent the automatic expansion of negative numbers. {0xFF byte maximum number of representations}
temp = Digital.indexof (hex2char[2 * i]) * 16;
Temp + + digital.indexof (hex2char[2 * i + 1]);
Bytes[i] = (byte) (temp & 0xff);
}
return bytes;
}
Related Article

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.