android 数据类型转换大全

论坛 期权论坛     
选择匿名的用户   2021-6-2 17:51   331   0
<pre style="color:rgb(0,0,0); font-family:&#39;宋体&#39;; font-size:11.3pt; background-color:rgb(255,255,255)">
<span style="color:rgb(0,0,128); font-weight:bold">import </span>android.text.TextUtils;
<span style="color:rgb(0,0,128); font-weight:bold">import </span>android.util.Log;

<span style="color:rgb(0,0,128); font-weight:bold">import </span>java.io.File;
<span style="color:rgb(0,0,128); font-weight:bold">import </span>java.io.FileInputStream;
<span style="color:rgb(0,0,128); font-weight:bold">import </span>java.io.FileNotFoundException;
<span style="color:rgb(0,0,128); font-weight:bold">import </span>java.io.IOException;
<span style="color:rgb(0,0,128); font-weight:bold">import </span>java.io.InputStream;

<span style="color:rgb(128,128,128); font-style:italic">/**
</span><span style="color:rgb(128,128,128); font-style:italic"> * Created by snake on 2016/10/11.
</span><span style="color:rgb(128,128,128); font-style:italic"> */
</span><span style="color:rgb(128,128,128); font-style:italic">
</span><span style="color:rgb(0,0,128); font-weight:bold">public class </span>UtilsDataConversion {


    <span style="color:rgb(0,0,128); font-weight:bold">private static final </span>String <span style="color:rgb(102,14,122); font-style:italic; font-weight:bold">TAG </span>&#61; <span style="color:rgb(0,128,0); font-weight:bold">&#34;UtilsDataConversion&#34;</span>;

    <span style="color:rgb(128,128,128); font-style:italic">//
</span><span style="color:rgb(128,128,128); font-style:italic">    /**
</span><span style="color:rgb(128,128,128); font-style:italic">     * 字节到字符转换
</span><span style="color:rgb(128,128,128); font-style:italic">     *
</span><span style="color:rgb(128,128,128); font-style:italic">     * </span><span style="color:rgb(128,128,128); font-style:italic; font-weight:bold">&#64;param </span><span style="color:rgb(61,61,61); font-style:italic; font-weight:bold">b
</span><span style="color:rgb(61,61,61); font-style:italic; font-weight:bold">     </span><span style="color:rgb(128,128,128); font-style:italic">* </span><span style="color:rgb(128,128,128); font-style:italic; font-weight:bold">&#64;return
</span><span style="color:rgb(128,128,128); font-style:italic; font-weight:bold">     </span><span style="color:rgb(128,128,128); font-style:italic">*/
</span><span style="color:rgb(128,128,128); font-style:italic">    </span><span style="color:rgb(0,0,128); font-weight:bold">public static char </span>getChar(<span style="color:rgb(0,0,128); font-weight:bold">byte</span>[] b, <span style="color:rgb(0,0,128); font-weight:bold">int </span>index) {
        <span style="color:rgb(0,0,128); font-weight:bold">int </span>s &#61; <span style="color:rgb(0,0,255)">0</span>;
        <span style="color:rgb(0,0,128); font-weight:bold">if </span>(b[index &#43; <span style="color:rgb(0,0,255)">1</span>] &gt; <span style="color:rgb(0,0,255)">0</span>)
            s &#43;&#61; b[index &#43; <span style="color:rgb(0,0,255)">1</span>];
        <span style="color:rgb(0,0,128); font-weight:bold">else
</span><span style="color:rgb(0,0,128); font-weight:bold">            </span>s &#43;&#61; <span style="color:rgb(0,0,255)">256 </span>&#43; b[index &#43; <span style="color:rgb(0,0,255)">0</span>];
        s *&#61; <span style="color:rgb(0,0,255)">256</span>;
        <span style="color:rgb(0,0,128); font-weight:bold">if </span>(b[index &#43; <span style="color:rgb(0,0,255)">0</span>] &gt; <span style="color:rgb(0,0,255)">0</span>)
            s &#43;&#61; b[index &#43; <span style="color:rgb(0,0,255)">1</span>];
        <span style="color:rgb(0,0,128); font-weight:bold">else
</span><span style="color:rgb(0,0,128); font-weight:bold">            </span>s &#43;&#61; <span style="color:rgb(0,0,255)">256 </span>&#43; b[index &#43; <span style="color:rgb(0,0,255)">0</span>];
        <span style="color:rgb(0,0,128); font-weight:bold">char </span>ch &#61; (<span style="color:rgb(0,0,128); font-weight:bold">char</span>) s;
        <span style="color:rgb(0,0,128); font-weight:bold">return </span>ch;
    }

    <span style="color:rgb(128,128,128); font-style:italic">/**
</span><span style="color:rgb(128,128,128); font-style:italic">     * 将一个2个字节数组转换为intt。
</span><span style="color:rgb(128,128,128); font-style:italic">     *
</span><span style="color:rgb(128,128,128); font-style:italic">     * </span><span style="color:rgb(128,128,128); font-style:italic; font-weight:bold">&#64;param </span><span style="color:rgb(61,61,61); font-style:italic; font-weight:bold">b
</span><span style="color:rgb(61,61,61); font-style:italic; font-weight:bold">     </span><span style="color:rgb(128,128,128); font-style:italic">* </span><span style="color:rgb(128,128,128); font-style:italic; font-weight:bold">&#64;return
</span><span style="color:rgb(128,128,128); font-style:italic; font-weight:bold">     </span><span style="color:rgb(128,128,128); font-style:italic">*/
</span><span style="color:rgb(128,128,128); font-style:italic">    </span><span style="color:rgb(0,0,128); font-weight:bold">public static int </span>bytesTwo2Int(<span style="color:rgb(0,0,128); font-weight:bold">byte</span>[] b) {
        <span style="color:rgb(0,0,128); font-weight:bold">int </span>a1 &#61; b[<span style="color:rgb(0,0,255)">0</span>] &amp; <span style="color:rgb(0,0,255)">0xff</span>;
        <span style="color:rgb(0,0,128); font-weight:bold">int </span>a2 &#61; (b[<span style="color:rgb(0,0,255)">1</span>] &amp; <span style="color:
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP