Java String to Byte Array convertStringToByte(String strValue)

Here you can find the source of convertStringToByte(String strValue)

Description

Converts a given string into a byte of one integer

License

Open Source License

Parameter

Parameter Description
strValue a parameter

Return

int as byte

Declaration

protected static byte convertStringToByte(String strValue) 

Method Source Code

//package com.java2s;
/* $Revision: 159 $ $Date: 2009-08-17 12:52:56 +0000 (ma, 17 aug 2009) $ $Author: blohman $ 
 * /*from ww  w  .  ja  v  a2 s  .com*/
 * Copyright (C) 2007-2009  National Library of the Netherlands, 
 *                          Nationaal Archief of the Netherlands, 
 *                          Planets
 *                          KEEP
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
 * MA  02110-1301, USA.
 *
 * For more information about this project, visit
 * http://dioscuri.sourceforge.net/
 * or contact us via email:
 *   jrvanderhoeven at users.sourceforge.net
 *   blohman at users.sourceforge.net
 *   bkiers at users.sourceforge.net
 * 
 * Developed by:
 *   Nationaal Archief               <www.nationaalarchief.nl>
 *   Koninklijke Bibliotheek         <www.kb.nl>
 *   Tessella Support Services plc   <www.tessella.com>
 *   Planets                         <www.planets-project.eu>
 *   KEEP                            <www.keep-project.eu>
 * 
 * Project Title: DIOSCURI
 */

public class Main {
    /**
     * Converts a given string into a byte of one integer
     *
     * @param strValue
     * @return int as byte
     */
    protected static byte convertStringToByte(String strValue) {
        // FIXME: Check if correct byte implementation
        // Parse from string to int (hex)
        try {
            byte intRegVal = 0;
            for (int i = strValue.length(); i > 0; i--) {
                intRegVal = (byte) (intRegVal + ((int) Math.pow(16, strValue.length() - i))
                        * Integer.parseInt(strValue.substring(i - 1, i), 16));
            }

            return intRegVal;
        } catch (NumberFormatException e) {
            return -1;
        }
    }
}

Related

  1. asBytes(String basicString)
  2. asBytes(String hex)
  3. asBytes(String hexStr)
  4. convertStringToByte(String input)
  5. convertStringToByteArray(String input)
  6. convertStringToByteArray(String string)
  7. convertStringToBytes(String string)
  8. getBytes(String k)