Java String to convertStringToWord(String strValue)

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

Description

Converts a given string into a word of bytes

License

Open Source License

Parameter

Parameter Description
strValue a parameter

Return

byte[] as word

Declaration

protected static byte[] convertStringToWord(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 w  w w. ja  v  a 2s  . c  o m
 * 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 word of bytes
     *
     * @param strValue
     * @return byte[] as word
     */
    protected static byte[] convertStringToWord(String strValue) {
        // Create empty word
        byte[] word = new byte[2];

        // Parse from string to int (hex)
        try {
            byte intRegVal = 0;
            for (int i = strValue.length(); i > 0; i--) {
                intRegVal = (byte) (intRegVal
                        + Math.pow(16, strValue.length() - i) * Byte.parseByte(strValue.substring(i - 1, i), 16));
                // intRegVal = Byte.parseByte(strValue.substring(i-1,i),16);
            }

            // Enter values in word
            word[0] = (byte) (intRegVal >>> 8 & 0xFF);
            word[1] = (byte) (intRegVal & 0xFF);
        } catch (NumberFormatException e) {
            return null;
        }
        return word;
    }
}

Related

  1. convertStringToPrimitive(Object object, Class toType)
  2. convertStringToPrimitive(String value, Class destinationClass)
  3. convertStringToSafeFilename(String s)
  4. convertStringToValue(String txt, Class klass)
  5. convertStringToVarName(final String str)
  6. convertStringWithReplacementPattern(final String originalString, final String pattern, final String replace)