Here you can find the source of toBuffer(String spacedHex)
Parameter | Description |
---|---|
spacedHex | a parameter |
public static ByteBuffer toBuffer(String spacedHex)
//package com.java2s; /*//from ww w. ja va 2 s. co m * The contents of this file are subject to the terms of the Common Development and * Distribution License (the License). You may not use this file except in compliance with the * License. * * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the * specific language governing permission and limitations under the License. * * When distributing Covered Software, include this CDDL Header Notice in each file and include * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL * Header, with the fields enclosed by brackets [] replaced by your own identifying * information: "Portions copyright [year] [name of copyright owner]". * * Copyright 2015 LDS */ import java.io.ByteArrayOutputStream; import java.nio.ByteBuffer; public class Main { /** * Convert the spaced hex form of a String into a ByteBuffer. * * @param spacedHex * @return */ public static ByteBuffer toBuffer(String spacedHex) { return ByteBuffer.wrap(toByteArray(spacedHex)); } /** * Convert the spaced hex form of a String into a byte array. * * @param spacedHex * @return */ public static byte[] toByteArray(String spacedHex) { int idx = 0; int len = spacedHex.length(); StringBuilder sb = new StringBuilder(); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); while (idx < len) { char chr = spacedHex.charAt(idx++); if (chr != ' ') { sb.setLength(0); sb.append(chr); sb.append(spacedHex.charAt(idx++)); int i = Integer.parseInt(sb.toString(), 16); byte b = ((byte) i); bytes.write(b); } } return bytes.toByteArray(); } }