Java Integer to intTo2LengthBytes(int i)

Here you can find the source of intTo2LengthBytes(int i)

Description

int To Length Bytes

License

Open Source License

Declaration

public static byte[] intTo2LengthBytes(int i) 

Method Source Code

//package com.java2s;
/*/*from w ww . ja  v a  2 s .  c o m*/
 * @(#)ByteConvertUtil.java   V0.0.1 2015-2-3, ????1:37:07
 *
 * Copyright 2015 www.ifood517.com. All rights reserved.
 * www.ifood517.com PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

public class Main {

    public static byte[] intTo2LengthBytes(int i) {
        short s = (short) i;
        return shortToBytes(s);
    }

    public static byte[] intTo2LengthBytes(int i, byte[] data, int index) {
        short s = (short) i;
        return shortToBytes(s, data, index);
    }

    public static byte[] shortToBytes(short s) {
        byte[] shortBytes = new byte[2];
        shortBytes[0] = (byte) (s >> 8);
        shortBytes[1] = (byte) (s >> 0);
        return shortBytes;
    }

    public static byte[] shortToBytes(short s, byte[] data, int index) {
        data[index] = (byte) (s >> 8);
        data[index + 1] = (byte) (s >> 0);
        return data;
    }
}

Related

  1. int2path(int v)
  2. int2rgb(final int color)
  3. Int2Short(int i)
  4. int2sortableStr(int val)
  5. int2TwoChars(int i)
  6. intTo2UnsignedBytes(int val)
  7. intTo8HexString(int i)
  8. intToAlgebraicLoc(int loc)
  9. intToAlpha(Integer no)