Java Text Parse textToByte(char[] text)

Here you can find the source of textToByte(char[] text)

Description

text To Byte

License

Open Source License

Declaration

public static byte textToByte(char[] text) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2000, 2014 QNX Software Systems and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://from  ww  w.  j av  a 2s . c  om
 *     QNX Software Systems - Initial API and implementation
 *     Freescale Semiconductor - Address watchpoints, https://bugs.eclipse.org/bugs/show_bug.cgi?id=118299
 *     Patrick Chuong (Texas Instruments) -   Update CDT ToggleBreakpointTargetFactory enablement (340177)
 *     Marc Khouzam (Ericsson) - Support for dynamic printf (400628)
 *******************************************************************************/

public class Main {
    public static byte textToByte(char[] text) {
        byte result = 0;
        if (text.length == 2) {
            byte[] bytes = { charToByte(text[0]), charToByte(text[1]) };
            result = (byte) ((bytes[0] << 4) + bytes[1]);
        }
        return result;
    }

    public static byte charToByte(char ch) {
        if (Character.isDigit(ch)) {
            return (byte) (ch - '0');
        }
        if (ch >= 'a' && ch <= 'f') {
            return (byte) (0xa + ch - 'a');
        }
        if (ch >= 'A' && ch <= 'F') {
            return (byte) (0xa + ch - 'A');
        }
        return 0;
    }
}

Related

  1. textToBytes(String text)
  2. textToCDATA(String text)
  3. textToInteger(String filename, String text, String name)
  4. textToInteger(String text)