Java Text Parse textToBytes(String text)

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

Description

Convert text to bytes

License

Open Source License

Parameter

Parameter Description
text a parameter

Return

bytes for the input text

Declaration

private static byte[] textToBytes(String text) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from w  ww .  ja  va  2s .c o  m
     * Convert text to bytes
     *
     * @param text
     * @return bytes for the input text
     */
    private static byte[] textToBytes(String text) {
        byte[] baBytes = new byte[text.length() / 2];
        for (int j = 0; j < text.length() / 2; j++) {
            Integer tmpInteger = Integer.decode(new String("0x" + text.substring(j * 2, (j * 2) + 2)));
            int tmpValue = tmpInteger.intValue();
            if (tmpValue > 127) // fix negatives
            {
                tmpValue = (tmpValue - 127) * -1;
            }
            tmpInteger = new Integer(tmpValue);
            baBytes[j] = tmpInteger.byteValue();
        }

        return baBytes;
    }
}

Related

  1. textToByte(char[] text)
  2. textToCDATA(String text)
  3. textToInteger(String filename, String text, String name)
  4. textToInteger(String text)
  5. textToMMDDYYYY(String pdata)