Little endian format Conversion
Last Update:2018-07-26
Source: Internet
Author: User
Dataconverterle.java 01/**
02 * Data Format conversion (byte => short int long float double)
* Little Endian storage mode
* PACKAGE:cma.common.dataio
Description:little Endian data Format conversion
* MODIFIER: Liu Zejun
* email:bj0773@gmail.com
* date:2007-05-04 00:55:43
Update:
Ten * Reference:jxutil.sourceforge.net
11 */
12
13
Package Cma.common.dataio;
14
15
Import Java.math.BigInteger;
16
17
Public class Dataconverterle {
Dataconverterle () {
19}
20
21st
Public Static String Gethex (
byte [] buf) {
22
return Gethex (buf, 0, 2);
23}
24
Public Static String Gethex (
byte [] buf,
int Index) {
25
return Gethex (buf, index, 2);
26}
27
Public Static
Short Getshort (
byte [] buf) {//2 bytes
28
return Getshort (buf, 0);
29}
30
Public Static
int GetInt (
byte [] buf) {//4 bytes
31
return GetInt (buf, 0);
32}
33
Public Static
Long Getlong (
byte [] buf) {//8 bytes
34
return Getlong (buf, 0);
35}
36
Public Static
float GetFloat (
byte [] buf) {//4 bytes
37
return GetFloat (buf, 0);
38}
39
Public Static
Double GetDouble (
byte [] buf) {//8 bytes
40
return GetDouble (buf, 0);
41}
42
43
Public Static String Gethex (
byte [] buf,
int Ofs
int Len) {
The String hexstr = "0123456789ABCDEF";
StringBuffer bufstr =
New StringBuffer ();
46
for (
int i=0;i<len;i++) {
Bufstr.append (Hexstr.charat (Buf[ofs + i] >> 4) & 0xf);
Bufstr.append (Hexstr.charat (Buf[ofs + i) & 0xf));
49}
50
return (Bufstr.tostring ());
51}
52
Public Static
Short Getshort (
byte [] buf,
int OFS) {
53
return (
Short ) (
Si (Buf[ofs] & 0xFF)
+ ((buf[ofs+1] & 0xFF) << 8)
56);
57}
58
Public Static
int GetInt (
byte [] buf,
int OFS) {
59
return (
(Buf[ofs] & 0xFF)
+ ((Buf[ofs + 1] & 0xFF) << 8)
+ ((Buf[ofs + 2] & 0xFF) << 16)
+ ((Buf[ofs + 3] & 0xFF) << 24)
64);
65}
66
Public Static
Long Getlong (
byte [] buf,
int OFS) {
67
return (
68 ((
Long ) Buf[ofs] & 0xFF)
69 + ((
Long ) Buf[ofs + 1] & 0xFF) << 8)
70 + ((
Long ) Buf[ofs + 2] & 0xFF) << 16)
71 + ((
Long ) Buf[ofs + 3] & 0xFF) << 24)
72 + ((
Long ) Buf[ofs + 4] & 0xFF) << 32)
73 + ((
Long ) Buf[ofs + 5] & 0xFF) << 40)
74 + ((
Long ) Buf[ofs + 6] & 0xFF) << 48)
75 + ((
Long ) Buf[ofs + 7] & 0xFF) << 56)
76);
77}
78
Public Static
float GetFloat (
byte [] buf,
int OFS) {
79
return Float.intbitstofloat (GetInt (buf, OFS));
80}
81
Public Static
Double GetDouble (
byte [] buf,
int OFS) {
82
return Double.longbitstodouble (Getlong (buf, OFS));
83}
84
85}