Android UTF8 Encode bytesUtf8(int c)

Here you can find the source of bytesUtf8(int c)

Description

bytes Utf

License

Apache License

Parameter

Parameter Description
c one codePoint

Return

the number of bytes needed to encode this codePoint in UTF-8

Declaration

private static int bytesUtf8(int c) 

Method Source Code

//package com.java2s;
/**/*from  w w  w  . j  a v a2 s  .c om*/
 * Copyright (c) 2000, Google Inc.
 *
 * 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 {
    /**
     * @param c one codePoint
     * @return the number of bytes needed to encode this codePoint in UTF-8
     */
    private static int bytesUtf8(int c) {
        if (c < 0x80) {
            return 1;
        } else if (c < 0x00800) {
            return 2;
        } else if (c < 0x10000) {
            return 3;
        } else if (c < 0x200000) {
            return 4;

            // RFC 3629 forbids the use of UTF-8 for codePoint greater than 0x10FFFF,
            // so if the caller respects this RFC, this should not happen
        } else if (c < 0x4000000) {
            return 5;
        } else {
            return 6;
        }
    }
}

Related

  1. utf8Encode(String str)
  2. utf8Encode(String str, String defultReturn)
  3. toUtf8(String str)
  4. bytes2StringUTF8(byte[] buf)
  5. bytes2StringUTF8(byte[] buf, int bufOffset, int bufLength, boolean bigEndian)
  6. bytes2charsUTF8(byte[] buf, int bufOffset, int bufLength, char[] cbuf, boolean bigEndian)
  7. bytesUTF8len(byte[] buf, int bufOffset, int bufLength)