Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bugfix] CodeUtils::utf8ToUnicode对某些字符不能正确进行UTF8到Unicode转换 #310

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -511,9 +511,10 @@ private MachineListVo getZkData(List<ZkDisconfDataItem> datalist, Config config)
int errorNum = 0;
for (ZkDisconfDataItem zkDisconfDataItem : datalist) {

String value = CodeUtils.unicodeToUtf8(config.getValue());
if (config.getType().equals(DisConfigTypeEnum.FILE.getType())) {

List<String> errorKeyList = compareConfig(zkDisconfDataItem.getValue(), config.getValue());
List<String> errorKeyList = compareConfig(zkDisconfDataItem.getValue(), value);

if (errorKeyList.size() != 0) {
zkDisconfDataItem.setErrorList(errorKeyList);
Expand All @@ -525,11 +526,11 @@ private MachineListVo getZkData(List<ZkDisconfDataItem> datalist, Config config)
// 配置项
//

if (zkDisconfDataItem.getValue().trim().equals(config.getValue().trim())) {
if (zkDisconfDataItem.getValue().trim().equals(value.trim())) {

} else {
List<String> errorKeyList = new ArrayList<String>();
errorKeyList.add(config.getValue().trim());
errorKeyList.add(value.trim());
zkDisconfDataItem.setErrorList(errorKeyList);
errorNum++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public class CodeUtils {

protected static final Logger LOG = LoggerFactory.getLogger(CodeUtils.class);

private static final int SHIFT = 4;

/**
* utf-8 转换成 unicode
*/
Expand All @@ -26,10 +28,17 @@ public static String utf8ToUnicode(String inStr) {
int j = (int) myBuffer[i] - 65248;
sb.append((char) j);
} else {
int chr1 = (char) myBuffer[i];
String hexS = Integer.toHexString(chr1);
String unicode = "\\u" + hexS;
sb.append(unicode.toLowerCase());
int chr1 = myBuffer[i] & 0x0FFFF;

//@see Integer::toUnsignedString0
int mag = Integer.SIZE - Integer.numberOfLeadingZeros(chr1);
int chars = Math.max(((mag + (SHIFT - 1)) / SHIFT), 1);

sb.append("\\u");
for (int j = 0; j < SHIFT - chars; j++) {
sb.append('0');
}
sb.append(Integer.toHexString(chr1).toLowerCase());
}
}
return sb.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@
*/
public class CodeUtilsTestCase {

@Test
public void utf8ToUnicodeTest() {
String code = CodeUtils.utf8ToUnicode("syserror.paramtype=请求参数解析错误");
System.out.println(code);
Assert.assertEquals("syserror.paramtype=\\u8bf7\\u6c42\\u53c2\\u6570\\u89e3\\u6790\\u9519\\u8bef", code);
}

@Test
public void utf8ToUnicodeTest2() {
String code = CodeUtils.utf8ToUnicode("·");
System.out.println(code);
Assert.assertEquals("\\u00b7", code);
}

@Test
public void unicodeToUtf8Test() {

Expand Down