//可直接在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上图