Javaの基本バイトの簡単な例[]さまざまなデータ型で互いに変換する
以下は、byte []タイプの長い、int、double、float、short、cahr、object、string型への変換の例です。
ソケットの開発中、通常、特定の値(これらの値はさまざまなJavaタイプ)をBYTE []タイプに変換する必要があります。このため、次の例を要約して、頻繁に読むことができるように投稿しました。
パブリッククラスのテストケース{ /***バイトアレイ変換が短い。 */ public static byte [] shorttobyte(short number){int temp = number; byte [] b = new byte [2]; for(int i = 0; i <b.length; i ++){b [i] = new integer(temp&0xff).bytevalue(); //最低ビットの最低ビットを最低ビット= temp >> 8; //右に移動} return b; } /***バイトアレイからショートへの変換。 */ public static short bytetoshort(byte [] b){short s = 0; short s0 =(short)(b [0]&0xff); //最低ビット短いs1 =(short)(b [1]&0xff); S1 << = 8; s =(short)(s0 | s1); s; } /*** intからバイト配列への変換。 */ public static byte [] inttobyte(int number){int temp = number; byte [] b = new byte [4]; for(int i = 0; i <b.length; i ++){b [i] = new integer(temp&0xff).bytevalue(); //最低ビットの最低ビットを最低ビット= temp >> 8; //右に移動} return b; } /***バイト配列からintへの変換。 */ public static int bytetoint(byte [] b){int s = 0; int s0 = b [0]&0xff; //最低ビットint s1 = b [1]&0xff; int s2 = b [2]&0xff; int s3 = b [3]&0xff; S3 << = 24; S2 << = 16; S1 << = 8; S = S0 | S1 | S2 | S3; s; } / ***長いタイプをバイト配列に変換* / public static byte [] longtobyte(long number){long temp = number; byte [] b = new byte [8]; for(int i = 0; i <b.length; i ++){b [i] = new long(temp&0xff)。 } /***バイト配列から長い変換。 */ public static long bytetolong(byte [] b){long s = 0; long s0 = b [0]&0xff; //最低ビット長いs1 = b [1] long s2 = b [2]&0xff; long s3 = b [3]&0xff; long s4 = b [4]&0xff; //最低ビット長いs5 = b [5]&0xff; long s6 = b [6]&0xff; long s7 = b [7]&0xff; // S0は変更されていませんs1 << = 8; S2 << = 16; S3 << = 24; S4 << = 8 * 4; S5 << = 8 * 5; S6 << = 8 * 6; S7 << = 8 * 7; S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7; s; } /***ダブルからバイト配列への変換。 */ public static byte [] doubletobyte(double num){byte [] b = new byte [8]; long l = double.doubletolongbits(num); for(int i = 0; i <8; i ++){b [i] = new long(l).bytevalue(); l = l >> 8; } burten b; } /***バイト配列からダブルへの変換。 */ public static double getDouble(byte [] b){long m; m = b [0]; m&= 0xff; m | =((long)b [1] << 8); m&= 0xffffffff; m | =((long)b [2] << 16); m&= 0xffffffffff; m | =((long)b [3] << 24); m&= 0xfffffffffffl; m | =((long)b [4] << 32); m&= 0xffffffffffffl; m | =((long)b [5] << 40); m&= 0xffffffffffffl; m | =((long)b [6] << 48); m&= 0xffffffffffffffl; m | =((long)b [7] << 56); double.longbitStodouble(m)を返します。 } /***フロートからバイト配列への変換。 * /public static void floattobyte(float x){// float.floattointbits(f)に変換} /***バイト配列からフロートへの変換。 */ public static float getfloat(byte [] b){// 4バイトint accum = 0; for(int shiftby = 0; shiftby <4; shiftby ++){accum | =(b [shiftby]&0xff)<< shiftby * 8; } float.intbitstofloat(ascum)を返します。 } /*** charからバイト配列への変換。 */ public static byte [] chartobyte(char c){byte [] b = new byte [2]; b [0] =(byte)((c&0xff00)>> 8); b [1] =(byte)(c&0xff); bを返します。 } /***バイト配列からcharへの変換。 */ public static char bytetochar(byte [] b){char c =(char)(((b [0]&0xff)<< 8)|(b [1]&0xff)); c; } /***文字列からバイト配列への変換。 */ public static byte [] Stringtobyte(string str)throws unsupportedencodingException {return str.getBytes( "gbk"); } /***バイト配列から文字列への変換。 */ public static string bytestostring(byte [] str){string keyword = null; try {keyword = new String(str、 "gbk"); } catch(unsupportedencodingexception e){e.printstacktrace(); }キーワードを返します。 } / ***オブジェクトからバイト配列への変換* / @test public void testObject2bytearray()throws ioexception、classNotFoundException {// object obj = ""; integer [] obj = {1、3、4}; // // bytearrayへのオブジェクトbytearrayoutputStream bo = new bytearrayoutputStream(); ObjectOutputStream OO = new objectOutputStream(bo); oo.writeobject(obj); byte [] bytes = bo.tobytearray(); bo.close(); oo.close(); system.out.println(arrays.tostring(bytes)); integer [] intarr =(integer [])testbytearray2object(bytes); system.out.println(arrays.aslist(intar)); byte [] b2 = inttobyte(123); system.out.println(arrays.tostring(b2)); int a = bytetoint(b2); System.out.println(a); } /***バイト配列からオブジェクトへの変換。 */ private Object testbytearray2Object(byte [] bytes)throws ioexception、classNotFoundException {// byte [] bytes = null;オブジェクトobj; // bytearray to bytearrayinputStream bi = new bytearrayinputStream(bytes); ObjectInputStream oi = new ObjectInputStream(bi); obj = oi.readobject(); bi.close(); oi.close(); System.out.println(obj); OBJを返します。 }}読んでくれてありがとう、私はそれがあなたを助けることができることを願っています。このサイトへのご支援ありがとうございます!