Java UTF8 Encode Utf8Encode(String string)

Here you can find the source of Utf8Encode(String string)

Description

Utf Encode

License

Apache License

Declaration

static private String Utf8Encode(String string) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2012 Gregory McFall//from  w  w w  .j av  a  2s .  c  o m
 * 
 * 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 {
    static private String Utf8Encode(String string) {
        string = string.replace("\\r\\n", "\n");
        String utftext = "";

        for (int n = 0; n < string.length(); n++) {

            char c = string.charAt(n);

            if (c < 128) {
                utftext += c;
            } else if ((c > 127) && (c < 2048)) {
                utftext += ((c >> 6) | 192);
                utftext += ((c & 63) | 128);
            } else {
                utftext += ((c >> 12) | 224);
                utftext += (((c >> 6) & 63) | 128);
                utftext += ((c & 63) | 128);
            }

        }

        return utftext;
    }
}

Related

  1. utf8Encode(final String s)
  2. utf8Encode(final String value)
  3. utf8Encode(String s)
  4. utf8Encode(String str)
  5. utf8Encode(String str)
  6. utf8Encode(String url)
  7. utf8FromString(String sIn)
  8. utf8urlencode(String text)