Java Integer Pad Zero zeroPadArray(byte[] source, int size)

Here you can find the source of zeroPadArray(byte[] source, int size)

Description

Pad a byte array with zeroes.

License

Open Source License

Parameter

Parameter Description
source The source array.
size The final array size.

Return

The array of size elements eventually zero-padded.

Declaration

public static byte[] zeroPadArray(byte[] source, int size) 

Method Source Code

//package com.java2s;
/*/*from w ww .j av a  2s  .c  om*/
 * Copyright (c) Fabio Falcinelli 2016.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Pad a byte array with zeroes. If Source array length exceed size, then truncate it.
     *
     * @param source The source array.
     * @param size   The final array size.
     * @return The array of size elements eventually zero-padded.
     */
    public static byte[] zeroPadArray(byte[] source, int size) {
        byte[] data = new byte[size];
        for (int i = 0; i < data.length; i++) {
            if (i < source.length)
                data[i] = source[i];
            else
                data[i] = 0;
        }
        return data;
    }
}

Related

  1. zeroPad(int n, int base, int width)
  2. zeroPad(int n, int len)
  3. zeroPad(int number, int places)
  4. ZeroPad(int number, int width)
  5. zeroPad(int value, int padding)
  6. ZeroPadded(Integer i)
  7. zeroPaddedHex(int n, int length)
  8. zeroPaddedInt(int num, int length)
  9. zeroPadInteger(int i)