Big endian format Conversion

Source: Internet
Author: User
Tags float double stringbuffer
Dataconverterbe.java01/**
02 * Data Format conversion (byte => short int long float double)
* Big endian storage mode
* PACKAGE:cma.common.dataio
Description:big Endian data Format conversion
* MODIFIER: Liu Zejun
* email:bj0773@gmail.com
* date:2007-05-04 00:56:01
Update:
Ten * Reference:jxutil.sourceforge.net
11 */
12
13 PackageCma.common.dataio;
14
15 ImportJava.math.BigInteger;
16
17 Public classDataconverterbe {
Dataconverterbe () {
19}
20
21st Public StaticString Gethex ( byte[] buf) {
22 returnGethex (buf, 0, 2);
23}
24 Public StaticString Gethex ( byte[] buf, intIndex) {
25 returnGethex (buf, index, 2);
26}
27 Public Static ShortGetshort ( byte[] buf) {//2 bytes
28 returnGetshort (buf, 0);
29}
30 Public Static intGetInt ( byte[] buf) {//4 bytes
31 returnGetInt (buf, 0);
32}
33 Public Static LongGetlong ( byte[] buf) {//8 bytes
34 returnGetlong (buf, 0);
35}
36 Public Static floatGetFloat ( byte[] buf) {//4 bytes
37 returnGetFloat (buf, 0);
38}
39 Public Static DoubleGetDouble ( byte[] buf) {//8 bytes
40 returnGetDouble (buf, 0);
41}
42
43 Public StaticString Gethex ( byte[] buf, intOfs intLen) {
The String hexstr = "0123456789ABCDEF";
StringBuffer bufstr = NewStringBuffer ();
46 for( inti=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 ShortGetshort ( byte[] buf, intOFS) {
53 return( Short) (
((Buf[ofs] & 0xFF) << 8)
+ ((Buf[ofs + 1] & 0xFF))
56);
57}
58 Public Static intGetInt ( byte[] buf, intOFS) {
59 return(
((Buf[ofs] & 0xFF) << 24)
+ ((Buf[ofs + 1] & 0xFF) << 16)
+ ((Buf[ofs + 2] & 0xFF) << 8)
+ ((Buf[ofs + 3] & 0xFF))
64);
65}
66 Public Static LongGetlong ( byte[] buf, intOFS) {
67 return(
68 (( Long) Buf[ofs] & 0xFF) << 56)
69 + (( Long) Buf[ofs + 1] & 0xFF) << 48)
70 + (( Long) Buf[ofs + 2] & 0xFF) << 40)
71 + (( Long) Buf[ofs + 3] & 0xFF) << 32)
72 + (( Long) Buf[ofs + 4] & 0xFF) << 24)
73 + (( Long) Buf[ofs + 5] & 0xFF) << 16)
74 + (( Long) Buf[ofs + 6] & 0xFF) << 8)
75 + (( Long) Buf[ofs + 7] & 0xFF)
76);
77}
78 Public Static floatGetFloat ( byte[] buf, intOFS) {
79 returnFloat.intbitstofloat (GetInt (buf, OFS));
80}
81 Public Static DoubleGetDouble ( byte[] buf, intOFS) {
82 returnDouble.longbitstodouble (Getlong (buf, OFS));
83}
84
85}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.