<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    JAVA基本數據類型與其他語言數據類型之間的轉換方法 (ZT)

     

    /**
      * 通信格式轉換
      *
      * Java和一些windows編程語言如c、c++、delphi所寫的網絡程序進行通訊時,需要進行相應的轉換
      * 高、低字節之間的轉換
      * windows的字節序為低字節開頭
      * linux,unix的字節序為高字節開頭
      * java則無論平臺變化,都是高字節開頭
       
    */
     

    public class FormatTransfer {
      
    /**
       * 將int轉為低字節在前,高字節在后的byte數組
       * 
    @param n int
       * 
    @return byte[]
       
    */

      
    public static byte[] toLH(int n) {
        
    byte[] b = new byte[4];
        b[
    0= (byte) (n & 0xff);
        b[
    1= (byte) (n >> 8 & 0xff);
        b[
    2= (byte) (n >> 16 & 0xff);
        b[
    3= (byte) (n >> 24 & 0xff);
        
    return b;
      }
     

      
    /**
       * 將int轉為高字節在前,低字節在后的byte數組
       * 
    @param n int
       * 
    @return byte[]
       
    */

      
    public static byte[] toHH(int n) {
        
    byte[] b = new byte[4];
        b[
    3= (byte) (n & 0xff);
        b[
    2= (byte) (n >> 8 & 0xff);
        b[
    1= (byte) (n >> 16 & 0xff);
        b[
    0= (byte) (n >> 24 & 0xff);
        
    return b;
      }
     

      
    /**
       * 將short轉為低字節在前,高字節在后的byte數組
       * 
    @param n short
       * 
    @return byte[]
       
    */

      
    public static byte[] toLH(short n) {
        
    byte[] b = new byte[2];
        b[
    0= (byte) (n & 0xff);
        b[
    1= (byte) (n >> 8 & 0xff);
        
    return b;
      }
     

      
    /**
       * 將short轉為高字節在前,低字節在后的byte數組
       * 
    @param n short
       * 
    @return byte[]
       
    */

      
    public static byte[] toHH(short n) {
        
    byte[] b = new byte[2];
        b[
    1= (byte) (n & 0xff);
        b[
    0= (byte) (n >> 8 & 0xff);
        
    return b;
      }
     

     

      
    /**
       * 將將int轉為高字節在前,低字節在后的byte數組 
      
    */

      
    public static byte[] toHH(int number) {
        
    int temp = number;
        
    byte[] b = new byte[4];
        
    for (int i = b.length - 1; i > -1; i--{
          b[i] 
    = new Integer(temp & 0xff).byteValue();
          temp 
    = temp >> 8;
        }

        
    return b;
      }
     

      
    public static byte[] IntToByteArray(int i) {
            
    byte[] abyte0 = new byte[4];
            abyte0[
    3= (byte) (0xff & i);
            abyte0[
    2= (byte) ((0xff00 & i) >> 8);
            abyte0[
    1= (byte) ((0xff0000 & i) >> 16);
            abyte0[
    0= (byte) ((0xff000000 & i) >> 24);
            
    return abyte0;
      }
     


       

      
    /**
       * 將float轉為低字節在前,高字節在后的byte數組
       
    */

      
    public static byte[] toLH(float f) {
        
    return toLH(Float.floatToRawIntBits(f));
      }
     

      
    /**
       * 將float轉為高字節在前,低字節在后的byte數組
       
    */

      
    public static byte[] toHH(float f) {
        
    return toHH(Float.floatToRawIntBits(f));
      }
     

      
    /**
       * 將String轉為byte數組
       
    */

      
    public static byte[] stringToBytes(String s, int length) {
        
    while (s.getBytes().length < length) {
          s 
    += " ";
        }

        
    return s.getBytes();
      }
     


      
    /**
       * 將字節數組轉換為String
       * 
    @param b byte[]
       * 
    @return String
       
    */

      
    public static String bytesToString(byte[] b) {
        StringBuffer result 
    = new StringBuffer("");
        
    int length = b.length;
        
    for (int i=0; i      result.append((char)(b[i] & 0xff));
        }

        
    return result.toString();
      }
     

      
    /**
       * 將字符串轉換為byte數組
       * 
    @param s String
       * 
    @return byte[]
       
    */

      
    public static byte[] stringToBytes(String s) {
        
    return s.getBytes();
      }
     

      
    /**
       * 將高字節數組轉換為int
       * 
    @param b byte[]
       * 
    @return int
       
    */

      
    public static int hBytesToInt(byte[] b) {
        
    int s = 0;
        
    for (int i = 0; i < 3; i++{
          
    if (b[i] >= 0{
            s 
    = s + b[i];
          }
     else {
            s 
    = s + 256 + b[i];
          }

          s 
    = s * 256;
        }

        
    if (b[3>= 0{
          s 
    = s + b[3];
        }
     else {
          s 
    = s + 256 + b[3];
        }

        
    return s;
      }
     

      
    /**
       * 將低字節數組轉換為int
       * 
    @param b byte[]
       * 
    @return int
       
    */

      
    public static int lBytesToInt(byte[] b) {
        
    int s = 0;
        
    for (int i = 0; i < 3; i++{
          
    if (b[3-i] >= 0{
            s 
    = s + b[3-i];
          }
     else {
            s 
    = s + 256 + b[3-i];
          }

          s 
    = s * 256;
        }

        
    if (b[0>= 0{
          s 
    = s + b[0];
        }
     else {
          s 
    = s + 256 + b[0];
        }

        
    return s;
      }
     


      
    /**
       * 高字節數組到short的轉換
       * 
    @param b byte[]
       * 
    @return short
       
    */

      
    public static short hBytesToShort(byte[] b) {
        
    int s = 0;
        
    if (b[0>= 0{
            s 
    = s + b[0];
          }
     else {
            s 
    = s + 256 + b[0];
          }

          s 
    = s * 256;
        
    if (b[1>= 0{
          s 
    = s + b[1];
        }
     else {
          s 
    = s + 256 + b[1];
        }

        
    short result = (short)s;
        
    return result;
      }
     

      
    /**
       * 低字節數組到short的轉換
       * 
    @param b byte[]
       * 
    @return short
       
    */

      
    public static short lBytesToShort(byte[] b) {
        
    int s = 0;
        
    if (b[1>= 0{
            s 
    = s + b[1];
          }
     else {
            s 
    = s + 256 + b[1];
          }

          s 
    = s * 256;
        
    if (b[0>= 0{
          s 
    = s + b[0];
        }
     else {
          s 
    = s + 256 + b[0];
        }

        
    short result = (short)s;
        
    return result;
      }
     

      
    /**
       * 高字節數組轉換為float
       * 
    @param b byte[]
       * 
    @return float
       
    */

      
    public static float hBytesToFloat(byte[] b) {
        
    int i = 0;
        Float F 
    = new Float(0.0);
        i 
    = ((((b[0]&0xff)<<8 | (b[1]&0xff))<<8| (b[2]&0xff))<<8 | (b[3]&0xff);
        
    return F.intBitsToFloat(i);
      }
     

      
    /**
       * 低字節數組轉換為float
       * 
    @param b byte[]
       * 
    @return float
       
    */

      
    public static float lBytesToFloat(byte[] b) {
        
    int i = 0;
        Float F 
    = new Float(0.0);
        i 
    = ((((b[3]&0xff)<<8 | (b[2]&0xff))<<8| (b[1]&0xff))<<8 | (b[0]&0xff);
        
    return F.intBitsToFloat(i);
      }
     

      
    /**
       * 將byte數組中的元素倒序排列
       
    */

      
    public static byte[] bytesReverseOrder(byte[] b) {
        
    int length = b.length;
        
    byte[] result = new byte[length];
        
    for(int i=0; i      result[length-i-1= b[i];
        }

        
    return result;
      } 

      
    /**
       * 打印byte數組
       
    */

      
    public static void printBytes(byte[] bb) {
        
    int length = bb.length;
        
    for (int i=0; i      System.out.print(bb[i] + " ");
        }

        System.out.println(
    "");
      } 

      
    public static void logBytes(byte[] bb) {
        
    int length = bb.length;
        String out 
    = "";
        
    for (int i=0; i      out = out + bb[i] + " ";
        }
     

      } 

      
    /**
       * 將int類型的值轉換為字節序顛倒過來對應的int值
       * 
    @param i int
       * 
    @return int
       
    */

      
    public static int reverseInt(int i) {
        
    int result = FormatTransfer.hBytesToInt(FormatTransfer.toLH(i));
        
    return result;
      }
     

      
    /**
       * 將short類型的值轉換為字節序顛倒過來對應的short值
       * 
    @param s short
       * 
    @return short
       
    */

      
    public static short reverseShort(short s) {
        
    short result = FormatTransfer.hBytesToShort(FormatTransfer.toLH(s));
        
    return result;
      }
     

      
    /**
       * 將float類型的值轉換為字節序顛倒過來對應的float值
       * 
    @param f float
       * 
    @return float
       
    */

      
    public static float reverseFloat(float f) {
        
    float result = FormatTransfer.hBytesToFloat(FormatTransfer.toLH(f));
        
    return result;
      }
     

    }

    posted on 2008-07-20 12:02 SeesSea 閱讀(323) 評論(0)  編輯  收藏 所屬分類: JAVA

    <2008年7月>
    293012345
    6789101112
    13141516171819
    20212223242526
    272829303112
    3456789

    導航

    統計

    常用鏈接

    留言簿(2)

    隨筆分類

    隨筆檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 国产va免费精品观看精品| 亚洲天堂一区二区三区| 四虎影视大全免费入口| 国产真人无码作爱视频免费| 免费精品国产自产拍在线观看 | 高潮毛片无遮挡高清免费| 色偷偷亚洲女人天堂观看欧| 亚洲欧洲国产日韩精品| 亚洲永久无码3D动漫一区| 免费一级大黄特色大片| 免费高清小黄站在线观看| jjizz全部免费看片| 国产无遮挡裸体免费视频在线观看| 成年网在线观看免费观看网址| 亚洲精品V天堂中文字幕| 亚洲AV无码国产精品色| 亚洲色图视频在线观看| 亚洲第一成年男人的天堂| 精品国产亚洲一区二区三区| 国产成人毛片亚洲精品| 亚洲成人影院在线观看| 免费观看午夜在线欧差毛片| 日韩免费福利视频| 美女黄网站人色视频免费国产| 久久久久久免费视频| 国产情侣激情在线视频免费看| 131美女爱做免费毛片| 精品免费久久久久久久| 国产v精品成人免费视频400条| 无遮免费网站在线入口| 57PAO成人国产永久免费视频| 蜜桃成人无码区免费视频网站 | 亚洲精品无码专区2| 亚洲成A人片77777国产| www国产亚洲精品久久久| 国产成人免费全部网站| 国产18禁黄网站免费观看| 亚洲国产小视频精品久久久三级 | 亚洲中文无码永久免| 一本天堂ⅴ无码亚洲道久久| 亚洲精品自偷自拍无码|