Javaバイトストリームと基本データ型間の変換例を詳しく解説

怪我咯
リリース: 2017-07-02 10:18:38
オリジナル
2389 人が閲覧しました

この記事では主に Java バイトストリームと基本的なデータ型の間の変換の例を紹介します。興味のある方は参考にしてください。

実際の開発では、組み込み通信の場合によく問題が発生します。一部の組み込みデバイスの処理能力が低いため、データはバイナリ データ ストリームの形式で送信されることがよくあります。これらの一般的な変換の概要を次に示します。

注: デフォルトではリトルエンディアンモードが送信に使用されます


バイトストリームをint型データに変換

public static int getInt(byte[] bytes) {
  return (0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)) | (0xff0000 & (bytes[2] << 16))
      | (0xff000000 & (bytes[3] << 24));
}
ログイン後にコピー

バイトストリームをlong型データに変換

public static long getLong(byte[] bytes) {
  return ((0xffL & (long) bytes[0]) | (0xff00L & ((long) bytes[1] << 8)) | (0xff0000L & ((long) bytes[2] << 16))
      | (0xff000000L & ((long) bytes[3] << 24)) | (0xff00000000L & ((long) bytes[4] << 32))
      | (0xff0000000000L & ((long) bytes[5] << 40)) | (0xff000000000000L & ((long) bytes[6] << 48))
      | (0xff00000000000000L & ((long) bytes[7] << 56)));
}
ログイン後にコピー

バイトストリームを

float型データに変換

public static float getFloat(byte[] bytes){
  int temp=getInt(bytes);
  return Float.intBitsToFloat(temp);
}
ログイン後にコピー

バイトを変換ストリームをdouble型データに変換

public static double getDouble(byte[] bytes){
  long temp=getLong(bytes);
  return Double.longBitsToDouble(temp);
}
ログイン後にコピー

int型データをバイトストリームに変換

public static byte[] getByteFromInt(int data){
  byte[] temp=new byte[4];
  temp[0]=(byte)(0xFF&(data));
  temp[1]=(byte)(0xFF&(data>>8));
  temp[2]=(byte)(0xFF&(data>>16));
  temp[3]=(byte)(0xFF&(data>>24));
  return temp;
}
ログイン後にコピー

long型データをバイトストリームに変換

public static byte[] getByteFromLong(long data){
  byte[] temp=new byte[8];
  temp[0]=(byte)(0xFF&(data));
  temp[1]=(byte)(0xFF&(data>>8));
  temp[2]=(byte)(0xFF&(data>>16));
  temp[3]=(byte)(0xFF&(data>>24));
  temp[4]=(byte)(0xFF&(data>>32));
  temp[5]=(byte)(0xFF&(data>>40));
  temp[6]=(byte)(0xFF&(data>>48));
  temp[7]=(byte)(0xFF&(data>>56));
  return temp;
}
ログイン後にコピー

float型データをバイトストリームに変換

public static byte[] getByteFromFloat(float data){
  byte[] temp=new byte[4];
  int tempInt=Float.floatToIntBits(data);
  temp[0]=(byte)(0xFF&(tempInt));
  temp[1]=(byte)(0xFF&(tempInt>>8));
  temp[2]=(byte)(0xFF&(tempInt>>16));
  temp[3]=(byte)(0xFF&(tempInt>>24));
  return temp;
}
ログイン後にコピー

double型データをバイトストリームに変換

public static byte[] getByteFromDouble(double data){
  byte[] temp=new byte[8];
  long tempLong=Double.doubleToLongBits(data);
  temp[0]=(byte)(0xFF&(tempLong));
  temp[1]=(byte)(0xFF&(tempLong>>8));
  temp[2]=(byte)(0xFF&(tempLong>>16));
  temp[3]=(byte)(0xFF&(tempLong>>24));
  temp[4]=(byte)(0xFF&(tempLong>>32));
  temp[5]=(byte)(0xFF&(tempLong>>40));
  temp[6]=(byte)(0xFF&(tempLong>>48));
  temp[7]=(byte)(0xFF&(tempLong>>56));
  return temp;
}
ログイン後にコピー

以上がJavaバイトストリームと基本データ型間の変換例を詳しく解説の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!