来自十六进制数组的Java初始化字符串 [英] Java initialiase string from hex array
本文介绍了来自十六进制数组的Java初始化字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
unsigned char [] charArray可以从一个十六进制数组中初始化字符串吗? = {0x41,0x42,0x43};
为什么不能这样做?
String charArray = new String((byte []){0x41,0x42,0x43});
解决方案
完全有可能,您可以在几个
-
使用unicode转义:
String string =\\\A\\\B\\\C;
-
创建和使用字节数组:
String string = new String(new byte [] {(byte)0x41,(byte)0x42,(byte)0x43});
您必须记住的主要事情是该字符串不是某个东西的数组,它是一个单独的对象。因此,它应该创建为字符串字面值,或者使用其中一个构造函数创建。
Is it possible to initialize a String from an array of hex values, like C style?
unsigned char[] charArray = {0x41, 0x42, 0x43};
Why is not possible to do something like this?
String charArray = new String((byte[]) {0x41, 0x42, 0x43});
解决方案
It is perfectly possible, you can do it in several ways.
Using unicode escapes:
String string = "\u0041\u0042\u0043";
Creating and using a byte array:
String string = new String(new byte[] {(byte) 0x41, (byte) 0x42, (byte) 0x43});
The main thing you have to remember is that the string is not an array of something, it is a separate object. Therefore it should either be created as a string literal, or with one of the constructors.
这篇关于来自十六进制数组的Java初始化字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文