Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Ascii到十六进制转换错误_Java_Hex_Ascii_Type Conversion - Fatal编程技术网

Java Ascii到十六进制转换错误

Java Ascii到十六进制转换错误,java,hex,ascii,type-conversion,Java,Hex,Ascii,Type Conversion,我有一个问题,当我尝试将'é'转换为十六进制时,它会给我一个错误“类型不匹配:无法从int转换为byte” 代码如下: public byte[] convertUnicode(String msg){ byte[] data = new byte[msg.length()]; for (int i = 0; i < msg.length(); i++) { switch (msg.charAt(i)) { case 'E': data[

我有一个问题,当我尝试将
'é'
转换为十六进制时,它会给我一个错误
“类型不匹配:无法从int转换为byte”
代码如下:

public  byte[] convertUnicode(String msg){
      byte[] data = new byte[msg.length()];
    for (int i = 0; i < msg.length(); i++) {
      switch (msg.charAt(i)) {
        case 'E':  data[i] = 0x45; break;
        case 'è':  data[i] = 0xE9; break;
        default:   data[i] = 0x3F; break; // '?'
      }
    }
    return data;
  }  
public byte[]convertUnicode(字符串msg){
字节[]数据=新字节[msg.length()];
对于(int i=0;i
致以最诚挚的问候。

对于超过0x7F(127,Java有符号字节的最大值)的值,您需要强制转换为(字节),否则文本将被视为int

data[i]=(字节)0xE9

然而,看起来您正试图以特定的编码方式获取字符串的字节,这要容易得多

byte[] bytes = msg.getBytes("ISO-8859-1");  // Or any other encoding of your choice
对于超过0x7F(127,Java有符号字节的最大值)的值,您需要强制转换为(字节),否则该文本将被视为int

data[i]=(字节)0xE9

然而,看起来您正试图以特定的编码方式获取字符串的字节,这要容易得多

byte[] bytes = msg.getBytes("ISO-8859-1");  // Or any other encoding of your choice

如果打印“0xE9”的值,则其233将超过字节-127到+127的值。因此,您需要将其类型转换为byte。

如果您打印'0xE9'的值,它的233将超过byte的值,即-127到+127。因此,您需要将其类型转换为字节