Java Convert via ByteBuffer toUUIDBinary(UUID uuid)

Here you can find the source of toUUIDBinary(UUID uuid)

Description

to UUID Binary

License

Apache License

Declaration

public static byte[] toUUIDBinary(UUID uuid) 

Method Source Code

//package com.java2s;
/*/*  ww w  .  j  a  v a 2  s. co  m*/
 * Copyright 2013 Rui Sun (SteveSunCanada@gmail.com)
 *
 * The Netty Project licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

import java.nio.ByteBuffer;

import java.util.UUID;

public class Main {
    public static byte[] toUUIDBinary(UUID uuid) {
        ByteBuffer bb = ByteBuffer.allocate(16);
        bb.putLong(uuid.getMostSignificantBits()).putLong(uuid.getLeastSignificantBits());
        return bb.array();
    }

    public static byte[] toUUIDBinary(String uuid) {
        byte[] binary = new byte[uuid.length() / 2];

        for (int i = 0; i < binary.length; i++) {
            int idx = i << 1;
            final String code = uuid.substring(idx, idx + 2);
            binary[i] = (byte) Integer.parseInt(code, 16);
        }

        return binary;
    }
}

Related

  1. toURI(String value)
  2. toUTF(byte[] bytes)
  3. toUTF8(String str)
  4. toUuid(byte[] bytes)
  5. toUUID(byte[] uuid)