Java Long to String longToString(long longV)

Here you can find the source of longToString(long longV)

Description

Get a string presentation of the 64 bits in a long

License

Open Source License

Parameter

Parameter Description
integer a parameter

Declaration

public static String longToString(long longV) 

Method Source Code

//package com.java2s;
/*/*  ww  w  .j a v a 2s .  c  o  m*/
 * Copyright Ericsson AB 2011-2014. All Rights Reserved.
 *
 * The contents of this file are subject to the Lesser GNU Public License,
 *  (the "License"), either version 2.1 of the License, or
 * (at your option) any later version.; you may not use this file except in
 * compliance with the License. You should have received a copy of the
 * License along with this software. If not, it can be
 * retrieved online at https://www.gnu.org/licenses/lgpl.html. Moreover
 * it could also be requested from Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
 * WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
 * EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
 * OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY KIND,
    
 * EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
 * LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE,
 * YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
 *
 * IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
 * WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
 * REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU FOR
 * DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL
 * DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE LIBRARY
 * (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED
 * INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE
 * OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH
 * HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 *
 */

public class Main {
    /**
     * Get a string presentation of the 64 bits in a long
     *
     * @param integer
     * @return
     */
    public static String longToString(long longV) {
        StringBuilder result = new StringBuilder("{");
        int counter = 0;
        for (int ind = 63; ind >= 0; ind--) {
            if (getBitInLong(longV, ind) == 1) {
                result.append("1");
                counter++;
            } else {
                result.append("0");
                counter++;
            }
            if (counter % 8 == 0 && counter != 64) {
                result.append("|");
            }
        }
        result.append("}");
        return result.toString();

    }

    /**
     * Get value of bit n in a long (64 bits)
     *
     * @param l is a 64-bit value
     * @param n is the bit whose values is to be returned
     */
    public static byte getBitInLong(long l, int n) {
        return (byte) ((l >> (long) n) & 1L);
    }
}

Related

  1. longToString(final long value)
  2. longToString(long l)
  3. longToString(long l)
  4. LongToString(Long l, int length)
  5. longToString(long li)
  6. longToString(long longValue)
  7. longToString(long longValue, boolean noCase)
  8. longToString(long n)
  9. LongToString(long number, boolean bcdFlag, int bufferSize)