Java Integer to Byte Array intToBytes(int val, int byteCount)

Here you can find the source of intToBytes(int val, int byteCount)

Description

int To Bytes

License

Apache License

Declaration

public static byte[] intToBytes(int val, int byteCount) 

Method Source Code

//package com.java2s;
//   Licensed under the Apache License, Version 2.0 (the "License");

public class Main {
    public static byte[] intToBytes(int val, int byteCount) {
        byte[] buffer = new byte[byteCount];

        int[] ints = new int[byteCount];

        for (int i = 0; i < byteCount; i++) {
            ints[i] = val & 0xFF;
            buffer[byteCount - i - 1] = (byte) ints[i];

            val = val >> 8;

            if (val == 0) {
                break;
            }//  ww  w . j a v a  2 s  .c  o m
        }

        return buffer;
    }
}

Related

  1. intToBytes(int v)
  2. intToBytes(int v, byte[] bytes)
  3. intToBytes(int v, final byte[] arr)
  4. intToBytes(int val)
  5. intToBytes(int val, byte[] bytes, int start)
  6. intToBytes(int value)
  7. intToBytes(int value)
  8. intToBytes(int value)
  9. intToBytes(int value, byte[] array, int offset)