简体   繁体   English

如何在没有Bom的UTF-8中将字符串转换为byteArray

[英]how to convert string to byteArray in UTF-8 Without Bom

I converted text to Base64 byteArray without any problem. 我将文本转换为Base64 byteArray没有任何问题。 Unfortunately, the converted string needs to start with "PD". 不幸的是,转换后的字符串必须以“ PD”开头。 It means i should encode it to UTF-8 without BOM not with BOM. 这意味着我应该将其编码为UTF-8,而不使用BOM表,而不使用BOM表。 I started several codes and everything on the net. 我启动了几个代码,所有内容都在网络上。 But, I could not succeed. 但是,我无法成功。 Any help is appreciated. 任何帮助表示赞赏。

Thank you so much. 非常感谢。

Regards Alper 问候阿尔珀

public static byte[] convertToByteArray(String strToBeConverted) {
    return strToBeConverted.getBytes(StandardCharsets.UTF_8);
}
return strToBeConverted.replaceFirst("^\uFEFF", "").getBytes(StandardCharsets.UTF_8);

The BOM is Unicode code point U+FEFF. BOM是Unicode代码点U + FEFF。

Removing it would mean to check first whether it indeed is present. 删除它意味着首先检查它是否确实存在。 String.replaceFirst is costly, as it uses regular expression matching, but fine here. String.replaceFirst很昂贵,因为它使用正则表达式匹配,但是在这里很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM