qq_27446037 2018-03-22 08:06 采纳率: 0%
浏览 1514
已结题

java 代码将PDF 文件转十六进制字符串以0x开头输出

java 代码将PDF 文件转十六进制字符串以0x开头输出,诸位大神谢谢

  • 写回答

1条回答 默认 最新

  • threenewbee 2018-03-22 15:55
    关注
     package string;
    
    import java.util.Arrays;
    
    /**
     * byte[]与16进制字符串相互转换
     * 
     * @date:2017年4月10日 下午11:04:27    
     */
    public class BytesHexStrTranslate {
    
        private static final char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5', 
                '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    
        /**
         * 方法一:
         * byte[] to hex string
         * 
         * @param bytes
         * @return
         */
        public static String bytesToHexFun1(byte[] bytes) {
            // 一个byte为8位,可用两个十六进制位标识
            char[] buf = new char[bytes.length * 2];
            int a = 0;
            int index = 0;
            for(byte b : bytes) { // 使用除与取余进行转换
                if(b < 0) {
                    a = 256 + b;
                } else {
                    a = b;
                }
    
                buf[index++] = HEX_CHAR[a / 16];
                buf[index++] = HEX_CHAR[a % 16];
            }
    
            return new String(buf);
        }
    
        /**
         * 方法二:
         * byte[] to hex string
         * 
         * @param bytes
         * @return
         */
        public static String bytesToHexFun2(byte[] bytes) {
            char[] buf = new char[bytes.length * 2];
            int index = 0;
            for(byte b : bytes) { // 利用位运算进行转换,可以看作方法一的变种
                buf[index++] = HEX_CHAR[b >>> 4 & 0xf];
                buf[index++] = HEX_CHAR[b & 0xf];
            }
    
            return new String(buf);
        }
    
        /**
         * 方法三:
         * byte[] to hex string
         * 
         * @param bytes
         * @return
         */
        public static String bytesToHexFun3(byte[] bytes) {
            StringBuilder buf = new StringBuilder(bytes.length * 2);
            for(byte b : bytes) { // 使用String的format方法进行转换
                buf.append(String.format("%02x", new Integer(b & 0xff)));
            }
    
            return buf.toString();
        }
    
        /**
         * 将16进制字符串转换为byte[]
         * 
         * @param str
         * @return
         */
        public static byte[] toBytes(String str) {
            if(str == null || str.trim().equals("")) {
                return new byte[0];
            }
    
            byte[] bytes = new byte[str.length() / 2];
            for(int i = 0; i < str.length() / 2; i++) {
                String subStr = str.substring(i * 2, i * 2 + 2);
                bytes[i] = (byte) Integer.parseInt(subStr, 16);
            }
    
            return bytes;
        }
    
    可以参考这个代码,但是pdf比较大,效率会比较低。
    
    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题