第二种
<span style="background-color: rgb(225, 225, 225);"><span class="kwd" style="font-family:Arial,宋体;font-size:14px;color:#008b;">public</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="kwd" style="font-family:Arial,宋体;font-size:14px;color:#008b;">class</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="typ" style="font-family:Arial,宋体;font-size:14px;color:#2b91af;">ByteUtil</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">{</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="com" style="font-family:Arial,宋体;font-size:14px;color:gray;">/** * 整型转换为4位字节数组 * @param intValue * @return */</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="kwd" style="font-family:Arial,宋体;font-size:14px;color:#008b;">public</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="kwd" style="font-family:Arial,宋体;font-size:14px;color:#008b;">static</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="kwd" style="font-family:Arial,宋体;font-size:14px;color:#008b;">byte</span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">[]</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> int2Byte</span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">(</span><span class="kwd" style="font-family:Arial,宋体;font-size:14px;color:#008b;">int</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> intValue</span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">)</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">{</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="kwd" style="font-family:Arial,宋体;font-size:14px;color:#008b;">byte</span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">[]</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> b </span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">=</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="kwd" style="font-family:Arial,宋体;font-size:14px;color:#008b;">new</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="kwd" style="font-family:Arial,宋体;font-size:14px;color:#008b;">byte</span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">[</span><span class="lit" style="font-family:Arial,宋体;font-size:14px;color:#8000;">4</span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">];</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="kwd" style="font-family:Arial,宋体;font-size:14px;color:#008b;">for</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">(</span><span class="kwd" style="font-family:Arial,宋体;font-size:14px;color:#008b;">int</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> i </span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">=</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="lit" style="font-family:Arial,宋体;font-size:14px;color:#8000;">0</span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">;</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> i </span><span class="pun" style="font-family:Arial,宋体;font-size:14px;"><</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="lit" style="font-family:Arial,宋体;font-size:14px;color:#8000;">4</span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">;</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> i</span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">++)</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">{</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> b</span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">[</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;">i</span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">]</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">=</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">(</span><span class="kwd" style="font-family:Arial,宋体;font-size:14px;color:#008b;">byte</span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">)</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">(</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;">intValue </span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">>></span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="lit" style="font-family:Arial,宋体;font-size:14px;color:#8000;">8</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">*</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">(</span><span class="lit" style="font-family:Arial,宋体;font-size:14px;color:#8000;">3</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">-</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> i</span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">)</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">&</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="lit" style="font-family:Arial,宋体;font-size:14px;color:#8000;">0xFF</span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">);</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="com" style="font-family:Arial,宋体;font-size:14px;color:gray;">//System.out.print(Integer.toBinaryString(b[i])+" ");</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="com" style="font-family:Arial,宋体;font-size:14px;color:gray;">//System.out.print((b[i] & 0xFF) + " ");</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">}</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="kwd" style="font-family:Arial,宋体;font-size:14px;color:#008b;">return</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> b</span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">;</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">}</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="com" style="font-family:Arial,宋体;font-size:14px;color:gray;">/** * 4位字节数组转换为整型 * @param b * @return */</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="kwd" style="font-family:Arial,宋体;font-size:14px;color:#008b;">public</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="kwd" style="font-family:Arial,宋体;font-size:14px;color:#008b;">static</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="kwd" style="font-family:Arial,宋体;font-size:14px;color:#008b;">int</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> byte2Int</span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">(</span><span class="kwd" style="font-family:Arial,宋体;font-size:14px;color:#008b;">byte</span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">[]</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> b</span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">)</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">{</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="kwd" style="font-family:Arial,宋体;font-size:14px;color:#008b;">int</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> intValue </span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">=</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="lit" style="font-family:Arial,宋体;font-size:14px;color:#8000;">0</span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">;</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="kwd" style="font-family:Arial,宋体;font-size:14px;color:#008b;">for</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">(</span><span class="kwd" style="font-family:Arial,宋体;font-size:14px;color:#008b;">int</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> i </span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">=</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="lit" style="font-family:Arial,宋体;font-size:14px;color:#8000;">0</span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">;</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> i </span><span class="pun" style="font-family:Arial,宋体;font-size:14px;"><</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> b</span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">.</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;">length</span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">;</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> i</span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">++)</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">{</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> intValue </span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">+=</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">(</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;">b</span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">[</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;">i</span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">]</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">&</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="lit" style="font-family:Arial,宋体;font-size:14px;color:#8000;">0xFF</span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">)</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="pun" style="font-family:Arial,宋体;font-size:14px;"><<</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">(</span><span class="lit" style="font-family:Arial,宋体;font-size:14px;color:#8000;">8</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">*</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">(</span><span class="lit" style="font-family:Arial,宋体;font-size:14px;color:#8000;">3</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">-</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> i</span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">));</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">}</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="kwd" style="font-family:Arial,宋体;font-size:14px;color:#008b;">return</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> intValue</span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">;</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">}</span><span class="pln" style="font-family:Arial,宋体;font-size:14px;"> </span><span class="pun" style="font-family:Arial,宋体;font-size:14px;">}</span></span>