将无符号字节数组(short [])解码为java

时间:2016-02-25 13:57:11

标签: java

我必须在java中读取无符号的8位字节数组(值范围为0-255),并且需要将其解码为String。

但是在java中,byte是8位有符号值,java将其视为short [](短值数组)而不是byte []。我只能将对象转换为short []。

我需要的是一种将此数组解码为String的方法。

提前致谢!

3 个答案:

答案 0 :(得分:2)

嗯,你有没有试过这个明显的?

public static void main(String[] args) {
    final short[] input = {72, 69, 76, 76, 79};

    for (final short character : input) {
        System.out.println((char) character);
    }
}

答案 1 :(得分:0)

2016-02-25T13:53:19.049733Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2016-02-25T13:53:19.049733Z 0 [Warning] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.
2016-02-25T13:53:19.049733Z 0 [Warning] 'NO_AUTO_CREATE_USER' sql mode was not set.
2016-02-25T13:53:19.049733Z 0 [Warning] Insecure configuration for --secure-file-priv: Current value does not restrict location of generated files. Consider setting it to a valid, non-empty path.
2016-02-25T13:53:19.050735Z 0 [Note] C:\UwAmp\bin\database\mysql-5.7.11\bin\mysqld.exe (mysqld 5.7.11) starting as process 6552 ...
2016-02-25T13:53:19.117843Z 0 [ERROR] Can't open shared library 'C:\UwAmp\bin\database\mysql-5.7.11\lib\plugin\keyring_file.dll' (errno: 126 Le module spécifié est introuvable.)
2016-02-25T13:53:19.122851Z 0 [ERROR] Couldn't load plugin named 'keyring_file' with soname 'keyring_file.dll'.
2016-02-25T13:53:19.127856Z 0 [Note] InnoDB: Mutexes and rw_locks use Windows interlocked functions
2016-02-25T13:53:19.130861Z 0 [Note] InnoDB: Uses event mutexes
2016-02-25T13:53:19.132864Z 0 [Note] InnoDB: Memory barrier is not used
2016-02-25T13:53:19.134867Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.3
2016-02-25T13:53:19.137871Z 0 [Note] InnoDB: Number of pools: 1
2016-02-25T13:53:19.139875Z 0 [Note] InnoDB: Not using CPU crc32 instructions
2016-02-25T13:53:21.056091Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
2016-02-25T13:53:21.087354Z 0 [Note] InnoDB: Completed initialization of buffer pool
2016-02-25T13:53:21.234144Z 0 [Note] InnoDB: Setting log file .\ib_logfile101 size to 48 MB
2016-02-25T13:53:22.659215Z 0 [Note] InnoDB: Setting log file .\ib_logfile1 size to 48 MB
2016-02-25T13:53:24.176685Z 0 [Note] InnoDB: Renaming log file .\ib_logfile101 to .\ib_logfile0
2016-02-25T13:53:24.176685Z 0 [Warning] InnoDB: New log files created, LSN=2602779
2016-02-25T13:53:24.176685Z 0 [Note] InnoDB: Highest supported file format is Barracuda.
2016-02-25T13:53:24.192315Z 0 [ERROR] InnoDB: Operating system error number 87 in a file operation.
2016-02-25T13:53:24.192315Z 0 [Note] InnoDB: Some operating system error numbers are described at http://dev.mysql.com/doc/refman/5.7/en/operating-system-error-codes.html
2016-02-25T13:53:24.192315Z 0 [ERROR] InnoDB: File .\ib_logfile0: 'aio read' returned OS error 187. Cannot continue operation
2016-02-25T13:53:24.192315Z 0 [ERROR] InnoDB: Cannot continue operation.

输出

    short[] x = {0x45,0x46,0x47};
    byte[] y = {0x45,0x46,0x47};
    System.out.println(new String(y));
    StringBuffer str = new StringBuffer();
    for(short s : x)
        str.append((char)s);
    System.out.println(str.toString());

答案 2 :(得分:0)

您可以将无符号字节转换为如下字符:

   byte unsignedByte = 65;
   char character = (char) unsignedByte;

现在你要做的就是在所有值上创建一个循环

相关问题