to Big Endian - Java java.lang

Java examples for java.lang:String Endian

Description

to Big Endian

Demo Code

/**// w  ww . j  av a2  s  .  c  om
 * Project: ${puma-common.aid}
 * 
 * File Created at 2012-6-24
 * $Id$
 * 
 * Copyright 2010 dianping.com.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Dianping Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with dianping.com.
 */
//package com.java2s;

public class Main {
    public static int toBigEndian(final int v) {
        int r = v;
        for (int i = 0; i < 4; i++) {
            r = ((r & 0x000000FF) << 24) | (r >>> 8);
        }
        return r;
    }

    public static long toBigEndian(final long v) {
        long r = v;
        for (int i = 0; i < 8; i++) {
            r = ((r & 0x00000000000000FFL) << 56) | (r >>> 8);
        }
        return r;
    }

    public static byte[] toBigEndian(byte[] value) {
        for (int i = 0, length = value.length >> 2; i <= length; i++) {
            final int j = value.length - 1 - i;
            final byte t = value[i];
            value[i] = value[j];
            value[j] = t;
        }
        return value;
    }
}

Related Tutorials