Java ASCII from toAscii(int i, boolean reversed)

Here you can find the source of toAscii(int i, boolean reversed)

Description

Given an integer value, reinterpret it as a string stored in four bytes.

License

Creative Commons License

Parameter

Parameter Description
i The integer to interpret
reversed True for big-endian, false for little-endian

Return

A string representation of the integer

Declaration

public static String toAscii(int i, boolean reversed) 

Method Source Code

//package com.java2s;
/**/*from  w  ww.j  av  a  2  s  . c o  m*/
 * Copyright (C) 2011 Darien Hager
 *
 * This code is part of the "HL2Parse" project, and is licensed under
 * a Creative Commons Attribution-ShareAlike 3.0 Unported License. For
 * either a summary of conditions or the full legal text, please visit:
 *
 * http://creativecommons.org/licenses/by-sa/3.0/
 *
 * Permissions beyond the scope of this license may be available
 * at http://technofovea.com/ .
 */

public class Main {
    /**
     * Given an integer value, reinterpret it as a string stored in four bytes.
     *
     * This is useful for a few places where developers have chosen integer IDs
     * which map to a human-readable mnemonic value.
     *
     * @param i The integer to interpret
     * @param reversed True for big-endian, false for little-endian
     * @return A string representation of the integer
     */
    public static String toAscii(int i, boolean reversed) {
        byte[] bytes = new byte[4];
        if (!reversed) {
            bytes[0] = (byte) (i >> 24);
            bytes[1] = (byte) ((i << 8) >> 24);
            bytes[2] = (byte) ((i << 16) >> 24);
            bytes[3] = (byte) ((i << 24) >> 24);
        } else {
            bytes[3] = (byte) (i >> 24);
            bytes[2] = (byte) ((i << 8) >> 24);
            bytes[1] = (byte) ((i << 16) >> 24);
            bytes[0] = (byte) ((i << 24) >> 24);
        }
        return new String(bytes);
    }
}

Related

  1. toAscii(byte[] b)
  2. toAscii(byte[] ba)
  3. toAscii(char ch)
  4. toAscii(char source)
  5. toAscii(int number)
  6. toAscii(String hexStr)
  7. toAscii(String notAscii)
  8. toAscii(String s)