简体   繁体   中英

how to convert string to byteArray in UTF-8 Without Bom

I converted text to Base64 byteArray without any problem. Unfortunately, the converted string needs to start with "PD". It means i should encode it to UTF-8 without BOM not with 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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