简体   繁体   English

扩展ascii的base64编码

[英]base64 encoding of extended ascii

I'm receiving binaries from an IoT device. 我正在从物联网设备接收二进制文件。 I'm trying to convert some identifier consisting of a list of up to 13 bytes to the smallest readable string possible. 我正在尝试将一个由最多13个字节的列表组成的标识符转换为可能的最小可读字符串。

For that I've been decoding it to Base64 then convert the bytes to hex so that 为此,我一直在将其解码为Base64,然后将字节转换为十六进制

byte[] bytes = {0x24, 0x54, 0x4b, 0x00, 0x31, 0x00, 0x0e, 0x50, 0x33, 0x42, 0x58, 0x35};

becomes

4CAD4FDC15F9

正常ascii字节的Base64解码

However, when I receive bytes in extended ascii (in the debugger, the bytes appear as negative values), the conversion to base64 returns an empty array of bytes. 但是,当我在扩展的ascii中接收字节时(在调试器中,字节显示为负值),转换为base64将返回一个空的字节数组。

Base64解码扩展的ascii字节

I have been using org.apache.tomcat.util.codec.binary.Base64 which in its documentation do mention that it's not taking into account extended ASCII characters as mentioned in the documentation: 我一直在使用org.apache.tomcat.util.codec.binary.Base64,它在文档中提到它没有考虑文档中提到的扩展ASCII字符:

Since this class operates directly on byte streams, and not character streams, it is hard-coded to only * encode/decode character encodings which are compatible with the lower 127 ASCII chart (ISO-8859-1, Windows-1252, * UTF-8, etc). 由于该类直接在字节流上运行,而不是在字符流上运行,因此硬编码仅对*编码/解码与低127 ASCII图表兼容的字符编码(ISO-8859-1,Windows-1252,* UTF- 8等)。


I also tried java.util.Base64, it works with the first array of bytes, and throws an exception with the second array of bytes : 我也试过java.util.Base64,它使用第一个字节数组,并使用第二个字节数组抛出异常

public static String getBase64HexDeviceIdFromSerialBytes(byte[] serial) {
    byte[] base64Bytes = java.util.Base64.getMimeDecoder().decode(ArrayUtils.subarray(serial, 0, 12));
    String hex = BytesUtils.bytesToHex(base64Bytes);
    return hex;
}

19:39:31.946 [main] ERROR com.trackener.backend.api.device.service.DeviceService - message processing failed java.lang.IllegalArgumentException: Last unit does not have enough valid bits at java.util.Base64$Decoder.decode0(Base64.java:734) at java.util.Base64$Decoder.decode(Base64.java:526) 19:39:31.946 [main] ERROR com.trackener.backend.api.device.service.DeviceService - 消息处理失败java.lang.IllegalArgumentException:最后一个单元在java.util.Base64上没有足够的有效位$ Decoder.decode0 (Base64.java:734)at java.util.Base64 $ Decoder.decode(Base64.java:526)


How to manage to do this conversion from bytes to this small base64 string with those special characters in extended ascii ? 如何管理从字节到这个小的base64字符串转换与扩展ascii中的那些特殊字符? If I could do this with another method (to get a unique code with as few characters as possible from an array of bytes), I'd be happy too. 如果我可以使用另一种方法(从字节数组中获取尽可能少的字符的唯一代码),我也会很高兴。

You've confused encode and decode. 你混淆了编码和解码。 If you have binary data (8-bit bytes) that you want in a "safe" form, you want to encode them as base64. 如果您想要“安全”形式的二进制数据(8位字节),则需要将它们编码为base64。 And once they are encoded, you don't need to convert them to hex to display them; 一旦它们被编码,您就不需要将它们转换为十六进制来显示它们; the point of encoding is that the result is printable ASCII. 编码点是结果是可打印的ASCII。

public static String getBase64HexDeviceIdFromSerialBytes(byte[] serial) {
    return java.util.Base64.getEncoder().encodeToString(ArrayUtils.subarray(serial, 0, 12));
}

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

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