Java String to Octal convertStringToOctal(String literalString)

Here you can find the source of convertStringToOctal(String literalString)

Description

Convert a utf-8 encoded string into into an octal enocded byte[] array.

License

Apache License

Parameter

Parameter Description
literalString string to convert.

Return

converted string value.

Declaration

public static String convertStringToOctal(String literalString) 

Method Source Code

//package com.java2s;
/*/*from   w ww  . j a  v a  2 s .  c o m*/
 * Copyright 2006-2017 ICEsoft Technologies Canada Corp.
 *
 * 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.
 */

public class Main {
    /**
     * Convert a utf-8 encoded string into into an octal enocded byte[] array.
     *
     * @param literalString string to convert.
     * @return converted string value.
     */
    public static String convertStringToOctal(String literalString) {
        // scan string ot see if we have any unicode.
        int length = literalString.length();
        boolean foundExtendedAscii = false;
        for (int i = 0; i < length; i++) {
            if (literalString.charAt(i) >= 255) {
                foundExtendedAscii = true;
                break;
            }
        }
        if (foundExtendedAscii) {
            char[] octalEncoded = new char[length * 2 + 2];
            octalEncoded[0] = 254;
            octalEncoded[1] = 255;
            for (int i = 0, j = 2; i < length; i++, j += 2) {
                octalEncoded[j] = (char) ((literalString.charAt(i) >> 8) & 0xFF);
                octalEncoded[j + 1] = (char) (literalString.charAt(i) & 0xFF);
            }
            return new String(octalEncoded);
        } else {
            return literalString;
        }
    }
}