Java String Encode encodeString(String s, boolean useUnicode)

Here you can find the source of encodeString(String s, boolean useUnicode)

Description

Encodes the string to a byte array using UTF-16LE or the ASCII charset in function of the useUnicode argument.

License

Open Source License

Parameter

Parameter Description
s the string to encode
useUnicode if true then string is encoded to UTF-16LE otherwise to ASCII

Exception

Parameter Description
UnsupportedEncodingException if encoding fails

Return

the encoded string as a byte array

Declaration

public static byte[] encodeString(String s, boolean useUnicode)
        throws UnsupportedEncodingException 

Method Source Code

//package com.java2s;
/**/*from   w ww. j  av  a 2 s  . com*/
 * Copyright 2007-2016, Kaazing Corporation. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.UnsupportedEncodingException;

public class Main {
    /**
     * Encodes the string to a byte array using UTF-16LE or the ASCII charset
     * in function of the <code>useUnicode</code> argument.
     * 
     * @param s the string to encode
     * @param useUnicode if true then string is encoded to UTF-16LE 
     * otherwise to ASCII
     * @return the encoded string as a byte array
     * @throws UnsupportedEncodingException if encoding fails
     */
    public static byte[] encodeString(String s, boolean useUnicode)
            throws UnsupportedEncodingException {
        if (useUnicode) {
            return getUTFStringAsByteArray(s);
        }

        return getOEMStringAsByteArray(s);
    }

    /**
     * Converts an UTF-16LE string as defined in NTLM protocol to a byte array.
     * 
     * @param s the string to convert
     * @return the result byte array
     * @throws UnsupportedEncodingException if the string is not an UTF-16LE string
     */
    public static byte[] getUTFStringAsByteArray(String s)
            throws UnsupportedEncodingException {
        return s.getBytes("UTF-16LE");
    }

    /**
     * Converts an OEM string as defined in NTLM protocol (eg ASCII charset)
     * to a byte array.
     * 
     * @param s the string to convert
     * @return the result byte array
     * @throws UnsupportedEncodingException if the string is not an OEM string
     */
    public static byte[] getOEMStringAsByteArray(String s)
            throws UnsupportedEncodingException {
        return s.getBytes("ASCII");
    }
}

Related

  1. encodeString(String b)
  2. encodeString(String in)
  3. encodeString(String myString)
  4. encodeString(String s)
  5. encodeString(String s)
  6. encodeString(String s, Integer size)
  7. encodeString(String s, String charset)
  8. encodeString(String sourceString, String sysCharset, String charset)
  9. encodeString(String str)