Here you can find the source of charsToUtf(char[] src, int srcIndex, byte[] dst, int dstIndex, int len)
Parameter | Description |
---|---|
src | The array holding the characters to convert. |
srcIndex | The start index from which characters are converted. |
dst | The array holding the converted characters.. |
dstIndex | The start index from which converted bytes are written. |
len | The maximum number of characters to convert. |
public static int charsToUtf(char[] src, int srcIndex, byte[] dst, int dstIndex, int len)
//package com.java2s; /*//from w w w .j av a 2 s .c o m * Copyright 2013 Alexander Shabanov - http://alexshabanov.com. * 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 * * 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 { /** * Copy characters in source array to bytes in target array, converting them to Utf8 representation. * The target array must be large enough to hold the result. * @param src The array holding the characters to convert. * @param srcIndex The start index from which characters are converted. * @param dst The array holding the converted characters.. * @param dstIndex The start index from which converted bytes are written. * @param len The maximum number of characters to convert. * @return First index in `dst' past the last copied byte. */ public static int charsToUtf(char[] src, int srcIndex, byte[] dst, int dstIndex, int len) { int j = dstIndex; int limit = srcIndex + len; for (int i = srcIndex; i < limit; i++) { char ch = src[i]; if (1 <= ch && ch <= 0x7F) { dst[j++] = (byte) ch; } else if (ch <= 0x7FF) { dst[j++] = (byte) (0xC0 | (ch >> 6)); dst[j++] = (byte) (0x80 | (ch & 0x3F)); } else { dst[j++] = (byte) (0xE0 | (ch >> 12)); dst[j++] = (byte) (0x80 | ((ch >> 6) & 0x3F)); dst[j++] = (byte) (0x80 | (ch & 0x3F)); } } return j; } }