> Java > java지도 시간 > 본문

Java 바이트 스트림과 기본 데이터 유형 간의 변환 예제에 대한 자세한 설명

怪我咯
풀어 주다: 2017-07-02 10:18:38
원래의
2414명이 탐색했습니다.

이 글은 주로 Java 바이트 스트림과 기본 데이터 유형 간의 변환 예를 소개합니다. 관심 있는 친구들이 참고할 수 있습니다.

실제 개발에서 임베디드 통신의 경우 종종 문제가 발생합니다. , 일부 임베디드 장치의 처리 성능이 좋지 않아 데이터가 바이너리 데이터 스트림 형식으로 전송되는 경우가 많습니다. 다음은 이러한 일반적인 변환을 요약한 것입니다.

참고: 전송에는 기본적으로 리틀 엔디안 모드가 사용됩니다.

바이트 스트림을 int 유형 데이터로 변환

public static int getInt(byte[] bytes) {
  return (0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)) | (0xff0000 & (bytes[2] << 16))
      | (0xff000000 & (bytes[3] << 24));
}
로그인 후 복사

바이트 스트림을 긴 유형 데이터로 변환

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);
}
로그인 후 복사

바이트 변환 stream을 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 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!