Reverses the order of the bytes in the given byte array - Java java.lang

Java examples for java.lang:byte Array

Description

Reverses the order of the bytes in the given byte array

Demo Code

/*******************************************************************************
 * Copyright (c) 2008 JCrypTool Team and Contributors
 * //from  ww w.jav  a  2  s  .  c  o m
 * All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse
 * Public License v1.0 which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        byte[] input = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        System.out.println(java.util.Arrays.toString(reverseOrder(input)));
    }

    /**
     * Reverses the order of the bytes in the given byte array
     * 
     * @param input - byte array
     * @return new byte array with the property result[a.length-i] = a[i]
     * 
     */
    public static byte[] reverseOrder(byte[] input) {
        byte[] result = new byte[input.length];
        for (int i = 0; i < input.length; i++) {
            result[i] = input[input.length - i - 1];
        }
        return result;
    }
}

Related Tutorials