java socket 報文解析_java socket解析和發送二進制報文工具(附java和C++轉化問題)

解析:

首先是讀取字節:

/*** 讀取輸入流中指定字節的長度

*

* 輸入流

*

*@paramlength 指定長度

*@return指定長度的字節數組*/

public static byte[] readBytesFromTo(byte[] buffer, int from, intlength) {byte[] sub = new byte[length];int cur = 0;for (int i = from; i < length + from; i++) {

sub[cur]=buffer[i];

cur++;

}returnsub;

}

讀取之后轉為字符串或者整型:

/*** byte[]轉int

*

*@parambytes 報文

*@returnInteger*/

public static int byteArrayToInt(byte[] bytes) {int value = 0;//由高位到低位

for (int i = 0; i < 4; i++) {int shift = (4 - 1 - i) * 8;

value+= (bytes[i] & 0x000000FF) << shift;//往高位游

}returnvalue;

}/*** 字節轉字符串

*@parambytes 報文*/

public static String byteArrayToString(byte[] bytes,int start , int end){return newString(bytes, start, end);

}

發送報文:

將java類型轉化為二進制:

/***@parami

*@return

*/

public static byte[] intToByteArray(inti) {byte[] result = new byte[4];

result[0] = (byte) ((i >> 24) & 0xFF);

result[1] = (byte) ((i >> 16) & 0xFF);

result[2] = (byte) ((i >> 8) & 0xFF);

result[3] = (byte) (i & 0xFF);returnresult;

}/***

*@params

*@return

*/

public static byte[] StringToByteArray(String s) {returns.getBytes();

}

整合二進制數組:

/***

*@parambytes

*@return

*/

public static byte[] combineByte(byte[] ... bytes){int length = 0;for (byte[] b : bytes) {

length+=b.length;

}byte[] allByte=new byte[length];int positon=0;for (byte[] b : bytes) {

System.arraycopy(b,0, allByte, positon, b.length);

positon+=b.length;

}returnallByte;

}

求校驗和:

/***

*@paraminput

*@return

*/

public static int getCheckSum(byte[]... input) {int re = 0;for (byte[] b : input) {for (byteaB : b) {

re+= aB & 0x0FF;//注意此處要轉化為無符號

}

}returnre;

}

二進制內容有時候要在不同的環境下解析和發送,下面是C++和java的字符差異

下面給出在不同字符集編碼下的字節數:

英文字母:

字節數 : 1;編碼:GB2312 ? ? ? ? ? ? 字節數: 1;編碼:GBK??????????? 字節數 : 1;編碼:GB18030

字節數 : 1;編碼:ISO-8859-1 ? ? ? ?字節數: 1;編碼:UTF-8???????? 字節數 : 4;編碼:UTF-16

字節數 : 2;編碼:UTF-16BE?????????? 字節數: 2;編碼:UTF-16LE

中文漢字:

字節數 : 2;編碼:GB2312????????????? 字節數: 2;編碼:GBK??????????? 字節數 : 2;編碼:GB18030

字節數 : 1;編碼:ISO-8859-1??????? 字節數: 3;編碼:UTF-8???????? 字節數 : 4;編碼:UTF-16

字節數 : 2;編碼:UTF-16BE?????????? 字節數: 2;編碼:UTF-16LE

編碼不同會影響解析的方式的差異,有時候還是蠻頭疼的,比如我們常用的中文問題,C++默認用GB2312編碼,所以java的解析要變一下:

String describe = new String(bytes, start += 4, describeLength, Charset.forName("GB2312"));

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/279183.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/279183.shtml
英文地址,請注明出處:http://en.pswp.cn/news/279183.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Ubuntu防火墻:ufw

原始linux的防火墻是iptables&#xff0c;以為過于繁瑣&#xff0c;各個發行版幾乎都有自己的方案; ubuntu下的防火墻是ufw[ubuntu fireward的縮寫]&#xff0c;centos的防火墻是fireward ubuntu下&#xff1a; 啟用或者關閉防火墻 sudo ufw enable|disable sudo ufw default d…

如何使自己的不和諧機器人

Discord has an excellent API for writing custom bots, and a very active bot community. Today we’ll take a look at how to get started making your own. Discord具有出色的用于編寫自定義機器人的API&#xff0c;以及非常活躍的機器人社區。 今天&#xff0c;我們將探…

?css3屬性選擇器總結

css3屬性選擇器總結 &#xff08;1&#xff09;E[attr]只使用屬性名&#xff0c;但沒有確定任何屬性值 <p miaov"a1">111111</p> <p miaov"a2">111111</p> p[miaov]{background: red;} /*所有屬性為miaov的元素都會被背景變紅&a…

java復合賦值運算符_Java 之復合賦值運算符

1.引入問題切入正題&#xff0c;看下面代碼&#xff0c;結果應該是怎么樣的public class App{public static void main( String[] args ){byte a1 ;int b 10;a ab;System.out.println(a);ab;System.out.println(a);}}這段代碼的執行結果是什么&#xff1f;&#xff1f;2. 執行…

程序代碼初學者_初學者:如何使用熱鍵在Windows中啟動任何程序

程序代碼初學者Assigning shortcut keys to launch programs in Windows is probably one of the oldest geek tricks in the book, but in true geek fashion we are going to show you how to do it in Windows 8. 分配快捷鍵以在Windows中啟動程序可能是本書中最古老的怪胎技…

stevedore——啟用方式

2019獨角獸企業重金招聘Python工程師標準>>> setuptools維護的入口點注冊表列出了可用的插件&#xff0c;但是并沒有為最終用戶提供使用或啟用的方法。 下面將描述用于管理要使用的擴展集的公共模式。 通過安裝方式啟用 對于許多應用程序&#xff0c;僅僅安裝一個擴…

java 重置定時器_可重置Java定時器

我想有一個java.utils.Timer與一個可重置時間在java.I需要設置一次off事件發生在X秒。如果在創建定時器的時間和X秒之間沒有發生任何事情&#xff0c;則事件會正常發生。然而&#xff0c;如果在X秒之前&#xff0c;我決定該事件應該發生在Y秒后&#xff0c;然后我想要能夠告訴定…

C# -- 文件的壓縮與解壓(GZipStream)

文件的壓縮與解壓 需引入 System.IO.Compression; 1.C#代碼&#xff08;入門案例&#xff09; 1 Console.WriteLine("壓縮文件...............");2 using (FileStream fr File.OpenRead("d:\\test.txt"))3 {4 …

win7屏保文件.scr_如何將屏保添加到Ubuntu 12.04

win7屏保文件.scrUbuntu 12.04 doesn’t ship with any screen savers, just a black screen that appears when your system is idle. If you’d rather have screensavers, you can swap gnome-screensaver for XScreenSaver. Ubuntu 12.04沒有附帶任何屏幕保護程序&#xff…

簡單讀寫XML文件

IPAddress.xml 文件如下&#xff1a; <?xml version"1.0" encoding"utf-8"?><IP><IPAddress>192.168.0.120</IPAddress></IP> 在 Form 窗體(讀取XML配置.Designer.cs)中有如下控件&#xff1a; 代碼 privateSystem.Wind…

如何與Ubuntu One同步配置文件

Ubuntu One lets you easily synchronize files and folders, but it isn’t clear how to sync configuration files. Using Ubuntu One’s folder synchronization options or some symbolic links, you can synchronize configuration files across all your computers. Ubu…

java 輸入流關閉順序_Java IO流中先關閉輸出流還是先關閉輸入流?為什么?

java中需要手動釋放的資源bai常見的有以下兩個&#xff1a;流相du關資zhi源流相關資源一般遵循&#xff1a;1)先開后關dao&#xff0c;先開的輸入流&#xff0c;再開的輸出流&#xff0c;通過讀取輸入流寫入輸出流中&#xff0c;那么應該先關輸出流&#xff0c;再關輸入流&…

解析Linux操作系統文件目錄

隨著Linux的不斷發展&#xff0c;越來越多的人開始使用Linux&#xff0c;對于那些剛剛接觸的人來說&#xff0c;恐怕最先感到困惑的就是那些“不明不白”的目錄了。如果想熟練使用Linux&#xff0c;讓Linux聽命于自己&#xff0c;就必須掌握這些目錄&#xff0c;下面就以Xteam公…

智能家居設備_您的智能家居設備正在監視您嗎?

智能家居設備In a world where we’re all paranoid about devices spying on us (and rightfully so), perhaps no other devices receive more scrutiny than smarthome products. But is that scrutiny warranted? 在一個我們都對監視設備的人都抱有偏執的世界(理應如此)&a…

Jenkins忘記admin密碼處理方法

1、先找到enkins/config.xml文件&#xff0c;并備份。 此文件位于Jenkins系統設置的主目錄&#xff0c;根據自己的配置情況而定。我的位置如下 /data/temp/jenkins/config.xml2、然后編輯config.xml刪除<useSecurity>true</useSecurity>至</securityRealm>&a…

java讀取excel某個單元格的值_java poi怎么獲取excel單元格的內容

展開全部package edu.sjtu.erplab.poi;import java.io.InputStream&chww.xqy.chain" target"_blank" class"link-baike">FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;impor…

科研繪圖工具軟件_如何在Windows 10 Mail中使用繪圖工具

科研繪圖工具軟件Microsoft recently released a new feature for the Windows 10 Mail app that lets you convey messages with drawings right inside the body of an email. This is a great way to quickly sketch things like graphs or tables to get your point across…

子元素相對于父元素垂直居中對齊

記個筆記 1. 元素相對于瀏覽器居中 <style>.window-center {/* 將position設置為fixed&#xff0c;使元素相對于瀏覽器窗口定位 */position: fixed;/* 將margin設置為auto&#xff0c;使瀏覽器自動推算元素外邊距 */margin: auto;/* 將上下左右邊距&#xff08;相對于瀏覽…

SQL Server編程(06)觸發器

SQL Server 通過觸發器用來保證業務邏輯和數據的完整性。在SQL Server中&#xff0c;觸發器是一種特殊類型的存儲過程&#xff0c;可在執行語言事件時自動觸發。SQL Server中觸發器包括三種&#xff1a;DML觸發器、DDL觸發器和登錄觸發器。 DML觸發器&#xff1a;執行DML語句觸…

網站運行java_定制化Azure站點Java運行環境(5)

Java 8下PermGen及參數設置在上一章節中&#xff0c;我們定制化使用了Java 8環境&#xff0c;使用我們的測試頁面打印出了JVM基本參數&#xff0c;但如果我們自己觀察&#xff0c;會發現在MXBeans中&#xff0c;沒有出現PermGen的使用數據&#xff0c;初始大小等信息&#xff0…