導航:首頁 > 編程語言 > pythonsocket接收16進制

pythonsocket接收16進制

發布時間:2022-08-17 13:52:22

『壹』 python socket recv 到的一個數據包中有 print repr出來有16進的有正常的字元串

struct包裡面有pack和unpack,字元串之間可以用+連接起來。

『貳』 請問通過netty或者socket怎麼接收16進制的位元組碼

socket接收的本來就是byte數組,直接處理byte數組就好。一般原始的socket代碼都是用byte的。只有外界一些簡化的代碼,才會直接把byte數組轉換成字元再處理。
不過唯一的問題是,你這樣沒有tcp頭的數據。容易發生tcp拆包。

『叄』 求教各位大仙socket如何發送16進制數據

我們定義發送者和接收者,發送者作為客戶端,接收者作為服務端。
Sender.java


importjava.io.DataOutputStream;
importjava.io.IOException;
importjava.net.Socket;
importjava.util.Arrays;

publicclassSender{

publicstaticvoidmain(String[]args)throwsException{
//127.0.0.1代表本機地址,在8888埠上監聽
Sendersender=newSender("127.0.0.1",8888);
byte[]bytes={15,16,17,120};//對應的十六進制是0F101178
sender.send(bytes);
System.out.println("發送"+Arrays.toString(bytes)+"完畢!");
}

privatefinalStringhost;
privatefinalintport;

publicSender(Stringhost,intport){
this.host=host;
this.port=port;
}

privatevoidsend(byte[]bytes)throwsIOException{
Socketsocket=newSocket(host,port);//建立和服務端的socket

try(DataOutputStreamdos//建立輸出流
=newDataOutputStream(socket.getOutputStream())){
dos.write(bytes,0,bytes.length);//向輸出流寫入bytes
}
}
}


Receiver.java

importjava.io.DataInputStream;
importjava.io.IOException;
importjava.net.ServerSocket;
importjava.net.Socket;

publicclassReceiver{

publicstaticvoidmain(String[]args)throwsException{
Receiverreceiver=newReceiver(8888);
receiver.receive();
}

;

publicReceiver(intport)throwsIOException{
serverSocket=newServerSocket(port);
}

privatevoidreceive()throwsIOException{
System.out.println("等待客戶端連接...");
Socketsocket=serverSocket.accept();
try(DataInputStreamdis=newDataInputStream(socket.getInputStream())){
byte[]bytes=newbyte[1024];//假設發送的位元組數不超過1024個
intsize=dis.read(bytes);//size是讀取到的位元組數

Stringhex=bytesToHex(bytes,0,size);
System.out.println("接收到的byte數組的十六進制:"+hex);
}
}

/**
*將byte數組轉化為十六進制字元串
*
*@parambytesbyte[]數組
*@parambegin起始位置
*@paramend結束位置
*@returnbyte數組的十六進制字元串表示
*/
privateStringbytesToHex(byte[]bytes,intbegin,intend){
StringBuilderhexBuilder=newStringBuilder(2*(end-begin));
for(inti=begin;i<end;i++){
hexBuilder.append(Character.forDigit((bytes[i]&0xF0)>>4,16));//轉化高四位
hexBuilder.append(Character.forDigit((bytes[i]&0x0F),16));//轉化低四位
hexBuilder.append('');//加一個空格將每個位元組分隔開
}
returnhexBuilder.toString().toUpperCase();
}

}


運行,首先啟動服務端:然後啟動客戶端:查看接收結果:

『肆』 java的socket通信如何直接接收16進制數據

發送消息之前轉碼為16進制
Java code
public static final String encodeHex(String msg) {
byte[] bytes = null;
try {
bytes = msg.getBytes("GBK");
} catch (java.io.UnsupportedEncodingException e) {
e.printStackTrace();
}
StringBuffer buff = new StringBuffer(bytes.length * 4);
String b;
char a;
int n = 0;
int m = 0;
for (int i = 0; i < bytes.length; i++) {
b = Integer.toHexString(bytes[i]);
if (bytes[i] > 0) {
buff.append("00");
buff.append(b);
n = n + 1;
} else {
a = msg.charAt((i - n) / 2 + n);
m = a;
b = Integer.toHexString(m);
buff.append(b.substring(0, 4));
i = i + 1;
}
}
return buff.toString();
}

『伍』 python中想要把字母或數字轉為16進制\x30格式並且輸出,但是最終顯示卻還是字母是怎麼回事呢

給你一個函數試試。
def str_to_hex(s):
return ' '.join([hex(ord(c)).replace('0x', '') for c in s])

『陸』 請教用socket如何向伺服器端發送16進制數據

我們定義發送者和接收者,發送者作為客戶端,接收者作為服務端。
Sender.java


importjava.io.DataOutputStream;
importjava.io.IOException;
importjava.net.Socket;
importjava.util.Arrays;

publicclassSender{

publicstaticvoidmain(String[]args)throwsException{
//127.0.0.1代表本機地址,在8888埠上監聽
Sendersender=newSender("127.0.0.1",8888);
byte[]bytes={15,16,17,120};//對應的十六進制是0F101178
sender.send(bytes);
System.out.println("發送"+Arrays.toString(bytes)+"完畢!");
}

privatefinalStringhost;
privatefinalintport;

publicSender(Stringhost,intport){
this.host=host;
this.port=port;
}

privatevoidsend(byte[]bytes)throwsIOException{
Socketsocket=newSocket(host,port);//建立和服務端的socket

try(DataOutputStreamdos//建立輸出流
=newDataOutputStream(socket.getOutputStream())){
dos.write(bytes,0,bytes.length);//向輸出流寫入bytes
}
}
}

Receiver.java


importjava.io.DataInputStream;
importjava.io.IOException;
importjava.net.ServerSocket;
importjava.net.Socket;

publicclassReceiver{

publicstaticvoidmain(String[]args)throwsException{
Receiverreceiver=newReceiver(8888);
receiver.receive();
}

;

publicReceiver(intport)throwsIOException{
serverSocket=newServerSocket(port);
}

privatevoidreceive()throwsIOException{
System.out.println("等待客戶端連接...");
Socketsocket=serverSocket.accept();
try(DataInputStreamdis=newDataInputStream(socket.getInputStream())){
byte[]bytes=newbyte[1024];//假設發送的位元組數不超過1024個
intsize=dis.read(bytes);//size是讀取到的位元組數

Stringhex=bytesToHex(bytes,0,size);
System.out.println("接收到的byte數組的十六進制:"+hex);
}
}

/**
*將byte數組轉化為十六進制字元串
*
*@parambytesbyte[]數組
*@parambegin起始位置
*@paramend結束位置
*@returnbyte數組的十六進制字元串表示
*/
privateStringbytesToHex(byte[]bytes,intbegin,intend){
StringBuilderhexBuilder=newStringBuilder(2*(end-begin));
for(inti=begin;i<end;i++){
hexBuilder.append(Character.forDigit((bytes[i]&0xF0)>>4,16));//轉化高四位
hexBuilder.append(Character.forDigit((bytes[i]&0x0F),16));//轉化低四位
hexBuilder.append('');//加一個空格將每個位元組分隔開
}
returnhexBuilder.toString().toUpperCase();
}

}

運行,首先啟動服務端:

然後啟動客戶端:

查看接收結果:

『柒』 c socket 16進制接收

printf("***all***%s
",buffer);

你這一句不是表明了你buffer里的內容是字元串么

你確定客服傳給你的是字元串么?如果不是,你這么輸出當然是亂碼

應該這么改

printf("***all***%X
",buffer);
閱讀全文

與pythonsocket接收16進制相關的資料

熱點內容
pythonclass使用方法 瀏覽:221
移動加密軟體去哪下載 瀏覽:281
php彈出alert 瀏覽:207
吉林文檔課件加密費用 瀏覽:131
感測器pdf下載 瀏覽:284
隨車拍app綁定什麼設備 瀏覽:896
方維團購系統源碼 瀏覽:991
linux反彈shell 瀏覽:159
列印機介面加密狗還能用嗎 瀏覽:301
二板股票源碼 瀏覽:448
度人經pdf 瀏覽:902
怎麼配置android遠程伺服器地址 瀏覽:960
java程序員看哪些書 瀏覽:943
什麼app可以免費和外國人聊天 瀏覽:797
pdf手寫筆 瀏覽:182
別永遠傷在童年pdf 瀏覽:990
愛上北斗星男友在哪個app上看 瀏覽:421
主力散戶派發源碼 瀏覽:671
linux如何修復伺服器時間 瀏覽:61
榮縣優途網約車app叫什麼 瀏覽:479