欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

利用Java在將整型數(shù)與byte[]數(shù)組進行轉換-創(chuàng)新互聯(lián)

這篇文章將為大家詳細講解有關利用Java在將整型數(shù)與byte[]數(shù)組進行轉換,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。

創(chuàng)新互聯(lián)公司IDC提供業(yè)務:服務器托管,成都服務器租用,服務器托管,重慶服務器租用等四川省內主機托管與主機租用業(yè)務;數(shù)據(jù)中心含:雙線機房,BGP機房,電信機房,移動機房,聯(lián)通機房。

具體如下:

工作項目需要在java和c/c++之間進行socket通信,socket通信是以字節(jié)流或者字節(jié)包進行的,socket發(fā)送方須將數(shù)據(jù)轉換為字節(jié)流或者字節(jié)包,而接收方則將字節(jié)流和字節(jié)包再轉換回相應的數(shù)據(jù)類型。如果發(fā)送方和接收方都是同種語言,則一般只涉及到字節(jié)序的調整。而對于java和c/c++的通信,則情況就要復雜一些,主要是因為java中沒有unsigned類型,并且java和c在某些數(shù)據(jù)類型上的長度不一致。

針對這種情況,本文整理了java數(shù)據(jù)類型和網(wǎng)絡字節(jié)流或字節(jié)包(相當于java的byte數(shù)組)之間轉換方法。實際上網(wǎng)上這方面的資料不少,但往往不全,甚至有些有錯誤,于是就花了點時間對java整型數(shù)和網(wǎng)絡字節(jié)序的byte[]之間轉換的各種情況做了一些驗證和整理。整理出來的函數(shù)如下:

public class ByteConvert {
  // 以下 是整型數(shù) 和 網(wǎng)絡字節(jié)序的 byte[] 數(shù)組之間的轉換
  public static byte[] longToBytes(long n) {
    byte[] b = new byte[8];
    b[7] = (byte) (n & 0xff);
    b[6] = (byte) (n >> 8 & 0xff);
    b[5] = (byte) (n >> 16 & 0xff);
    b[4] = (byte) (n >> 24 & 0xff);
    b[3] = (byte) (n >> 32 & 0xff);
    b[2] = (byte) (n >> 40 & 0xff);
    b[1] = (byte) (n >> 48 & 0xff);
    b[0] = (byte) (n >> 56 & 0xff);
    return b;
  }
  public static void longToBytes( long n, byte[] array, int offset ){
    array[7+offset] = (byte) (n & 0xff);
    array[6+offset] = (byte) (n >> 8 & 0xff);
    array[5+offset] = (byte) (n >> 16 & 0xff);
    array[4+offset] = (byte) (n >> 24 & 0xff);
    array[3+offset] = (byte) (n >> 32 & 0xff);
    array[2+offset] = (byte) (n >> 40 & 0xff);
    array[1+offset] = (byte) (n >> 48 & 0xff);
    array[0+offset] = (byte) (n >> 56 & 0xff);
  }
  public static long bytesToLong( byte[] array )
  {
    return ((((long) array[ 0] & 0xff) << 56)
       | (((long) array[ 1] & 0xff) << 48)
       | (((long) array[ 2] & 0xff) << 40)
       | (((long) array[ 3] & 0xff) << 32)
       | (((long) array[ 4] & 0xff) << 24)
       | (((long) array[ 5] & 0xff) << 16)
       | (((long) array[ 6] & 0xff) << 8)
       | (((long) array[ 7] & 0xff) << 0));
  }
  public static long bytesToLong( byte[] array, int offset )
  {
    return ((((long) array[offset + 0] & 0xff) << 56)
       | (((long) array[offset + 1] & 0xff) << 48)
       | (((long) array[offset + 2] & 0xff) << 40)
       | (((long) array[offset + 3] & 0xff) << 32)
       | (((long) array[offset + 4] & 0xff) << 24)
       | (((long) array[offset + 5] & 0xff) << 16)
       | (((long) array[offset + 6] & 0xff) << 8)
       | (((long) array[offset + 7] & 0xff) << 0));
  }
  public static byte[] intToBytes(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;
  }
  public static void intToBytes( int n, byte[] array, int offset ){
    array[3+offset] = (byte) (n & 0xff);
    array[2+offset] = (byte) (n >> 8 & 0xff);
    array[1+offset] = (byte) (n >> 16 & 0xff);
    array[offset] = (byte) (n >> 24 & 0xff);
  }
  public static int bytesToInt(byte b[]) {
    return  b[3] & 0xff
        | (b[2] & 0xff) << 8
        | (b[1] & 0xff) << 16
        | (b[0] & 0xff) << 24;
  }
  public static int bytesToInt(byte b[], int offset) {
    return  b[offset+3] & 0xff
        | (b[offset+2] & 0xff) << 8
        | (b[offset+1] & 0xff) << 16
        | (b[offset] & 0xff) << 24;
  }
  public static byte[] uintToBytes( long 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;
  }
  public static void uintToBytes( long n, byte[] array, int offset ){
    array[3+offset] = (byte) (n );
    array[2+offset] = (byte) (n >> 8 & 0xff);
    array[1+offset] = (byte) (n >> 16 & 0xff);
    array[offset]  = (byte) (n >> 24 & 0xff);
  }
  public static long bytesToUint(byte[] array) {
    return ((long) (array[3] & 0xff))
       | ((long) (array[2] & 0xff)) << 8
       | ((long) (array[1] & 0xff)) << 16
       | ((long) (array[0] & 0xff)) << 24;
  }
  public static long bytesToUint(byte[] array, int offset) {
    return ((long) (array[offset+3] & 0xff))
       | ((long) (array[offset+2] & 0xff)) << 8
       | ((long) (array[offset+1] & 0xff)) << 16
       | ((long) (array[offset]  & 0xff)) << 24;
  }
  public static byte[] shortToBytes(short n) {
    byte[] b = new byte[2];
    b[1] = (byte) ( n    & 0xff);
    b[0] = (byte) ((n >> 8) & 0xff);
    return b;
  }
  public static void shortToBytes(short n, byte[] array, int offset ) {
    array[offset+1] = (byte) ( n    & 0xff);
    array[offset] = (byte) ((n >> 8) & 0xff);
  }
  public static short bytesToShort(byte[] b){
    return (short)( b[1] & 0xff
           |(b[0] & 0xff) << 8 );
  }
  public static short bytesToShort(byte[] b, int offset){
    return (short)( b[offset+1] & 0xff
           |(b[offset]  & 0xff) << 8 );
  }
  public static byte[] ushortToBytes(int n) {
    byte[] b = new byte[2];
    b[1] = (byte) ( n    & 0xff);
    b[0] = (byte) ((n >> 8) & 0xff);
    return b;
  }
  public static void ushortToBytes(int n, byte[] array, int offset ) {
    array[offset+1] = (byte) ( n    & 0xff);
    array[offset] = (byte)  ((n >> 8) & 0xff);
  }
  public static int bytesToUshort(byte b[]) {
    return  b[1] & 0xff
        | (b[0] & 0xff) << 8;
  }
  public static int bytesToUshort(byte b[], int offset) {
    return  b[offset+1] & 0xff
        | (b[offset]  & 0xff) << 8;
  }
  public static byte[] ubyteToBytes( int n ){
    byte[] b = new byte[1];
    b[0] = (byte) (n & 0xff);
    return b;
  }
  public static void ubyteToBytes( int n, byte[] array, int offset ){
    array[0] = (byte) (n & 0xff);
  }
  public static int bytesToUbyte( byte[] array ){
    return array[0] & 0xff;
  }
  public static int bytesToUbyte( byte[] array, int offset ){
    return array[offset] & 0xff;
  }
  // char 類型、 float、double 類型和 byte[] 數(shù)組之間的轉換關系還需繼續(xù)研究實現(xiàn)。
}

分享文章:利用Java在將整型數(shù)與byte[]數(shù)組進行轉換-創(chuàng)新互聯(lián)
地址分享:http://aaarwkj.com/article0/dpidio.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供App設計、響應式網(wǎng)站、做網(wǎng)站、網(wǎng)站設計公司標簽優(yōu)化、網(wǎng)頁設計公司

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)
日韩中字在线一区二区| 永久免费观看黄色录像| 加勒比在线观看欧美一区| 午夜黄色福利在线观看| 国产成人综合亚洲不卡| 精品一区二区日本高清| 亚洲精品一品区二品区三| 午夜福利日本一区二区| 91人妻人澡人人爽| 日韩在线视频一区二区三| 亚洲国产偷拍在线观看| 国产又粗又猛又爽黄老大爷 | 日本精品女优一区二区三区四区| 五月天丁香婷婷深爱| 免费午夜福利一区二区| 国产成人一区二区二区三区| 九九视频免费观看91| 亚洲成人av福利网站| av黄色资源在线观看| 亚洲综合实力最强的国家| 国产亚洲精品福利视频| 超碰av之男人的天堂| 日韩午夜免费一区二区蜜桃| 国产一区二区三区的网站| 少妇诱惑一区二区三区| 中文字幕精品高清中国| 国产男女做爰在线视频| 色噜噜狠狠狠久久综合一区| 久久国产成人精品免费看| 亚洲国产成人一区二区精品区| 欧美日韩国产激情另类| 日本经典三级视频在线观看 | 国产av日韩精品一区二区三区| 欧美成人高清在线播放| 欧美日韩免费爱爱视频| 美女爽到高潮久久久| 在线免费观看日韩黄片| 欧美亚洲国产另类第一页| 免费黄片视频大全在线播放| 久久精品国产亚洲夜色av网站| 日韩精品高清视频在线观看|