//可直接在main方法中運行
//將8080 端口轉爲2字節byte
byte[] bytes=new byte[2];
short port = (short) Integer.parseInt("8080");
bytes[0] = (byte)((port >> 8) & 0x00ff);
bytes[1] = (byte)(port & 0x00ff);
//測試結果
//將byte的值轉string
StringBuffer st=new StringBuffer();
for (int i=0;i<bytes.length;i++){
String hex = Integer.toHexString(bytes[i] & 0xFF);
if(hex.length() < 2){
st.append(0);
}
st.append(hex);
}
System.out.println("byte值爲:"+String.valueOf(st));
//轉10進制顯示端口號
System.out.println("端口號爲:"+Integer.parseInt(String.valueOf(st),16));
![在這裏插入圖片描述](https://img-blog.csdnimg.cn/20200529173548684.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQyNzU0MzQ3,size_16,color_FFFFFF,t_70#pic_center)
//ok上圖