Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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 十六进制字符串转换为EBCDIC返回数字格式异常_Java_String_Utf 8_Hex - Fatal编程技术网

Java 十六进制字符串转换为EBCDIC返回数字格式异常

Java 十六进制字符串转换为EBCDIC返回数字格式异常,java,string,utf-8,hex,Java,String,Utf 8,Hex,我试图转换由十六进制数据组成的字符串,如C120,它分别表示a和(空格)。我正在获取字符串并尝试将其拆分为字符串数组。然后我尝试迭代这个数组,并获得相应的UTF-8值。下面是我的代码: public static String toEbcdic(String strToConvert){ String[] test = strToConvert.split("(?<=\\G..)"); ByteBuffer sb = ByteBuffer.allocate(test.len

我试图转换由十六进制数据组成的字符串,如
C120
,它分别表示
a
(空格)。我正在获取字符串并尝试将其拆分为字符串数组。然后我尝试迭代这个数组,并获得相应的
UTF-8
值。下面是我的代码:

public static String toEbcdic(String strToConvert){
    String[] test = strToConvert.split("(?<=\\G..)");
    ByteBuffer sb = ByteBuffer.allocate(test.length);
    for (String s : test) {
        Byte valueOf = Byte.valueOf(s, 10);
        sb.put(valueOf);
    }
    return new String(sb.array(), "CP1047");
}

我做错了什么?如何获得相应的EBCDIC值?

十六进制是基数16,而不是基数10。改变

Byte valueOf = Byte.valueOf(s, 10); 

用于获取基本类型(而不是包装器)

简单一行:

public static String toEbcdic( String hexStr ) throws IOException {
   return new String( DatatypeConverter.parseHexBinary( hexStr ), "CP1047" );
}

通过转换为字节数组并在CP1047中创建新字符串,代码的方向是正确的。 您只需解决两个问题即可使其正常工作:

  • 字节字符串是十六进制(以16为基数)而不是十进制(以10为基数)
  • Java字节是有符号的,因此将其输入C1将触发“值超出范围”异常
    解决方案很简单:解析为short并回溯到byte
  • 下面是一个完整的固定示例:

    import java.io.IOException;
    import java.nio.ByteBuffer;
    
    public class Q45235042 {
    
       public static String toEbcdic( String strToConvert ) throws IOException {
          String[] test = strToConvert.split( "(?<=\\G..)" );
          ByteBuffer sb = ByteBuffer.allocate( test.length );
          for ( String s : test )
             sb.put( (byte) Short.parseShort( s, 16 ) );
          return new String( sb.array(), "CP1047");
       }
    
       public static void main( String[] args ) throws IOException {
          System.out.println( toEbcdic( "C120C2" ) );
       }
    }
    
    import java.io.IOException;
    导入java.nio.ByteBuffer;
    公开课Q45235042{
    公共静态字符串toEbcdic(字符串strToConvert)引发IOException{
    
    String[]test=strotconvert.split(“(?我没有尝试将其转换为十六进制,而是ebcdic值。否。您尝试将十六进制值转换为二进制。和”c"不是十进制中的有效数字。好吧,那么我如何将十六进制字符串转换为ebcdic表示形式呢?经过一周的辛苦工作,你救了我。真希望这以前发生过。谢谢!我从未想过先短后字节。但一行代码太棒了。再次感谢!不幸的是,赏金可以在20小时后颁发,所以我将放弃o一旦时间过去,它就会消失。:)
    byte valueOf = Byte.parseByte(s, 16);
    
    public static String toEbcdic( String hexStr ) throws IOException {
       return new String( DatatypeConverter.parseHexBinary( hexStr ), "CP1047" );
    }
    
    import java.io.IOException;
    import java.nio.ByteBuffer;
    
    public class Q45235042 {
    
       public static String toEbcdic( String strToConvert ) throws IOException {
          String[] test = strToConvert.split( "(?<=\\G..)" );
          ByteBuffer sb = ByteBuffer.allocate( test.length );
          for ( String s : test )
             sb.put( (byte) Short.parseShort( s, 16 ) );
          return new String( sb.array(), "CP1047");
       }
    
       public static void main( String[] args ) throws IOException {
          System.out.println( toEbcdic( "C120C2" ) );
       }
    }