2010-06-23 21 views
9

मुझे जावा कोड में एक int में 2 बाइट्स (2 के पूरक) को कनवर्ट करने की आवश्यकता है। मैं यह कैसे करुं?जावा में बाइट्स को int में परिवर्तित करने के लिए

 
toInt(byte hb, byte lb) 
{ 

} 
+0

आप खुद ही इसे लागू करने, क्योंकि आप अपने शिक्षक या कुछ और के द्वारा एक विशेष कार्य है करना चाहते हैं? या आप बस बिल्ड-इन-जावा क्लास का उपयोग करना चाहते हैं? – TheChange

+0

यदि बिल्ड-इन-जावा क्लास में 2 बाइट्स को एक पूर्णांक में बदलने की विधि है, तो निश्चित रूप से मुझे बताएं। – user121196

+2

संभावित डुप्लिकेट [कनवर्ट 4 बाइट्स से int] (http://stackoverflow.com/questions/2383265/convert-4-bytes-to-int) – OscarRyz

उत्तर

15
return ((int)hb << 8) | ((int)lb & 0xFF); 

सभी मामलों में सही संचालन के छात्र के लिए एक व्यायाम के रूप में छोड़ दिया जाता है।

public int toInt(byte hb, byte lb) { 
    ByteBuffer bb = ByteBuffer.wrap(new byte[] {hb, lb}); 
    return bb.getShort(); // Implicitly widened to an int per JVM spec. 
} 

इस वर्ग उपयोगी यदि आप डेटा का एक बहुत डिकोडिंग रहे हैं हो सकता है:

+5

उन कास्ट दोनों अनावश्यक हैं। –

+1

मान्य बिंदु, कभी-कभी स्पष्ट होने के लिए यह स्पष्ट है। –

+1

यह ध्यान दिया जाना चाहिए कि यह समाधान केवल हस्ताक्षरित लघु पूर्णांक प्रतिनिधित्व के साथ काम करता है, और टीसीपी हेडर को डीकोड करने के लिए उपयुक्त नहीं होगा जहां एक हस्ताक्षरित प्रारूप में बंदरगाहों का प्रतिनिधित्व किया जाता है। –

0
public int toInt(byte hb, byte lb) 
{ 
    return ((int)hb)<<8 + lb; 
} 
+0

क्या यह उत्तर थिएटरस के समान है? – user121196

+3

नहीं, एलबी नकारात्मक होने पर यह गलत जवाब के साथ आता है। – ILMTitan

+0

उत्तर संपादित किया गया, समाधान << ऑपरेटर के चारों ओर कोष्ठक जोड़ने का था, उम्मीद है कि इसे जल्द ही अनुमोदित किया जाएगा और अन्य लोग इस स्निपेट का उपयोग करने वाले कोड को डीबग करने में एक दिन नहीं बिताएंगे :) –

10

तुम भी ByteBuffer वर्ग का उपयोग कर सकते हैं।

+0

लेकिन यदि दो बाइट 2 में हैं तारीफ, क्या वह एचबी के पहले बाइट को साइन बिट नहीं करेगा? तो आपका 'नया बाइट [] {ए, बी, सी, डी} 'एक = एचबी & 0x80; बी = 0 होगा; सी = एचबी & 0x7F; डी = एलबी; '? मैं यहाँ आधार से दूर हो सकता है। – corsiKa

+0

अच्छा बिंदु, 'एचबी' का पहला बिट 'ए' के बाईं ओर खींचा जाना चाहिए, इसलिए मेरा समाधान शायद वह नहीं है जिसे वह ढूंढ रहा है। मैं बस बाइटबफर वर्ग को इंगित करना चाहता था ... – maerics

+0

'getInt' के बजाय' getShort' का उपयोग करें। जब आप परिणामस्वरूप 'शॉर्ट' को 'int' पर डालते हैं तो यह सही परिणाम देने के लिए साइन-विस्तार किया जाएगा। – finnw

1

उपयोग:

public class Test { 

    public static void main(String[] args) { 
     byte[] b = new byte[] { -83, 0, 0, 0 }; 

     int i = GattUtils.getIntValue(b, GattUtils.FORMAT_UINT32, 0); 

     System.out.println(i); //will print 173 
    } 


public class GattUtils { 
    public static final long leastSigBits = 0x800000805f9b34fbL; 

    public static final int FIRST_BITMASK = 0x01; 
    public static final int SECOND_BITMASK = FIRST_BITMASK << 1; 
    public static final int THIRD_BITMASK = FIRST_BITMASK << 2; 
    public static final int FOURTH_BITMASK = FIRST_BITMASK << 3; 
    public static final int FIFTH_BITMASK = FIRST_BITMASK << 4; 
    public static final int SIXTH_BITMASK = FIRST_BITMASK << 5; 
    public static final int SEVENTH_BITMASK = FIRST_BITMASK << 6; 
    public static final int EIGTH_BITMASK = FIRST_BITMASK << 7; 

    public static final int FORMAT_UINT8 = 17; 
    public static final int FORMAT_UINT16 = 18; 
    public static final int FORMAT_UINT32 = 20; 
    public static final int FORMAT_SINT8 = 33; 
    public static final int FORMAT_SINT16 = 34; 
    public static final int FORMAT_SINT32 = 36; 
    public static final int FORMAT_SFLOAT = 50; 
    public static final int FORMAT_FLOAT = 52; 

    public static UUID toUuid(String uuidString) { 
     return UUID.fromString(uuidString); 
    } 

    public static UUID toUuid(long assignedNumber) { 
     return new UUID((assignedNumber << 32) | 0x1000, leastSigBits); 
    } 

    public static String toUuid128(long assignedNumber) { 
     return toUuid(assignedNumber).toString(); 
    } 

    public static String toUuid16(int assignedNumber) { 
     return Integer.toHexString(assignedNumber); 
    } 

    public static Integer getIntValue(byte[] value, int format, int position) { 
     if (value == null) 
      return null; 
     if (position + (format & 0xF) > value.length) 
      return null; 
     switch (format) { 
     case FORMAT_UINT8: 
      return Integer.valueOf(value[position] & 0xFF); 
     case FORMAT_UINT16: 
      return Integer.valueOf(add(value[position], value[(position + 1)])); 
     case FORMAT_UINT32: 
      return Integer.valueOf(add(value[position], value[(position + 1)], value[(position + 2)], value[(position + 3)])); 
     case FORMAT_SINT8: 
      return Integer.valueOf(signed(value[position] & 0xFF, 8)); 
     case FORMAT_SINT16: 
      return Integer.valueOf(signed(add(value[position], value[(position + 1)]), 16)); 
     case FORMAT_SINT32: 
      return Integer.valueOf(signed(add(value[position], value[(position + 1)], value[(position + 2)], value[(position + 3)]), 32)); 
     } 
     return null; 
    } 

    public static Float getFloatValue(byte[] value, int format, int position) { 
     if (value == null) 
      return null; 
     if (position + (format & 0xF) > value.length) 
      return null; 
     int i; 
     int mantissa; 
     int exponent; 
     switch (format) { 
     case FORMAT_SFLOAT: 
      i = value[(position + 1)]; 
      position = value[position]; 
      mantissa = signed((position & 0xFF) + ((i & 0xFF & 0xF) << 8), 12); 
      exponent = signed((i & 0xFF) >> 4, 4); 
      return Float.valueOf((float) (mantissa * Math.pow(10.0D, exponent))); 
     case FORMAT_FLOAT: 
      exponent = value[(position + 3)]; 
      mantissa = value[(position + 2)]; 
      i = value[(position + 1)]; 
      position = value[position]; 
      return Float.valueOf((float) ((format = signed((position & 0xFF) + ((i & 0xFF) << 8) + ((mantissa & 0xFF) << 16), 24)) * Math.pow(10.0D, exponent))); 
     } 
     return null; 
    } 

    public static String getStringValue(byte[] value, int position) { 
     if (value == null) 
      return null; 
     if (position > value.length) 
      return null; 
     byte[] arrayOfByte = new byte[value.length - position]; 
     for (int i = 0; i != value.length - position; i++) { 
      arrayOfByte[i] = value[(position + i)]; 
     } 
     return new String(arrayOfByte); 
    } 

    private static int add(byte byte1, byte byte2) { 
     return (byte1 & 0xFF) + ((byte2 & 0xFF) << 8); 
    } 

    private static int add(byte byte1, byte byte2, byte byte3, byte byte4) { 
     return (byte1 & 0xFF) + ((byte2 & 0xFF) << 8) + ((byte3 & 0xFF) << 16) + ((byte4 & 0xFF) << 24); 
    } 

    private static int signed(int value, int length) { 
     if ((value & 1 << length - 1) != 0) 
      value = -1 * ((1 << length - 1) - (value & (1 << length - 1) - 1)); 
     return value; 
    } 

    /** 
    * Convert hex byte array from motorola API to byte array. 
    * 
    * @param hexByteArray 
    * @return 
    */ 
    public static byte[] hexByteArrayToByteArray(byte[] hexByteArray) { 
     return hexStringToByteArray(new String(hexByteArray)); 
    } 

    /** 
    * Convert string from motorola API to a byte array. 
    * 
    * @param hexString 
    * @return 
    */ 
    public static byte[] hexStringToByteArray(String hexString) { 
     int len = hexString.length(); 
     byte[] data = new byte[len/2]; 
     for (int i = 0; i < len; i += 2) { 
      data[i/2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) + Character.digit(hexString.charAt(i + 1), 16)); 
     } 
     return data; 
    } 
} 
संबंधित मुद्दे