Java Integer to Binary itob(int i, int j)

Here you can find the source of itob(int i, int j)

Description

Creates a byte representation of the first argument

License

Open Source License

Parameter

Parameter Description
i positive or 0 int to be converted.
j positive int length.

Return

byte array.

Declaration

public static byte[] itob(int i, int j) 

Method Source Code

//package com.java2s;
/*//from   w  ww. ja v a 2 s  . c om
* Copyright (c) 2000 - 2005 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description:  
*
*/

public class Main {
    /**
    * Creates a byte representation of the first argument
    *
    * @param  i positive or 0 int to be converted.
    * @param  j positive int length.
    * @return  byte array.
    */
    public static byte[] itob(int i, int j) {
        byte[] res = new byte[j];

        for (int n = j - 1; n >= 0; n--) {
            res[n] = (byte) ('0' + (i % 10));
            i = i / 10;
        }

        return res;
    }
}

Related

  1. intToBinary(int binary, int digits)
  2. intToBinary(int i)
  3. intToBinary(int i, int byteLength)
  4. intToBinary(long value, int bits)
  5. intToBinString(int val, int width)
  6. ItoB(int x)
  7. iToB(int[] intVals)
  8. itob(long integer, int[] arr)