Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

public class Main {
    public static final long UINT32_MAX = 0xFFFFFFFFL;

    public static byte[] uint32ToByteArray(long src) throws IllegalArgumentException {
        if (src < 0 || src > UINT32_MAX) {
            throw new IllegalArgumentException("the input value " + src + " is out of the range of uint32");
        }

        byte[] dest = new byte[4];
        int mask = 0xff;

        for (int i = 0; i < 4; i++) {
            dest[i] = (byte) ((src >> (i * 8)) & mask);
        }

        return dest;
    }
}