Java Byte Array from getBytesFromHexaText(String text)

Here you can find the source of getBytesFromHexaText(String text)

Description

get Bytes From Hexa Text

License

Open Source License

Declaration

public static byte[] getBytesFromHexaText(String text) 

Method Source Code

//package com.java2s;
/*//from w w  w .j av a  2 s . c o  m
 *  Copyright (C) 2010-2015 JPEXS, All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3.0 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.
 */

import java.io.ByteArrayOutputStream;

import java.util.Scanner;

public class Main {
    public static byte[] getBytesFromHexaText(String text) {
        Scanner scanner = new Scanner(text);
        scanner.nextLine(); // ignore first line
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine().trim();
            if (line.startsWith(";")) {
                continue;
            }
            line = line.replace(" ", "");
            for (int i = 0; i < line.length() / 2; i++) {
                String hexStr = line.substring(i * 2, (i + 1) * 2);
                byte b = (byte) Integer.parseInt(hexStr, 16);
                baos.write(b);
            }
        }
        byte[] data = baos.toByteArray();
        return data;
    }
}

Related

  1. getBytesArrayFromFile(String fileName)
  2. getBytesASCII(String s)
  3. getBytesAsTempFile(byte[] bytes)
  4. getBytesClassic(final int count, final InputStream is)
  5. getBytesFromArrayList(final ArrayList fileList)
  6. getBytesFromImage(File file)
  7. getBytesFromInt(int value)
  8. getBytesFromList(List values)
  9. getBytesFromObject(Serializable data)

  10. HOME | Copyright © www.java2s.com 2016