Java对十六进制文件读取

注:当前文件中的数据顺序:低位在前、高位在后

Java对十六进制文件的读取,尤其是使用readInt()和readDouble()方法时必须要对数据进行转换,这样才可以避免读到的数据出错。

我们先提供一个数据转换的类,这样可以便于后面的数据转换:

  类名:ByteToOther

     方法:

    public int intFromByte(byte[] temp){
        int value = 0;
//      value = (((temp[0] & 0xff) << 24) | ((temp[1] & 0xff) << 16) | ((temp[2] & 0xff) << 8) | (temp[3] & 0xff));
        value = ((temp[0] & 0xff) | ((temp[1] & 0xff) << 8) | ((temp[2] & 0xff) << 16) | ((temp[3] & 0xff) << 24));
        return value;
    }


    public double doubleFromByte(byte[] temp){
        byte tem = 0;
        for(int i = 0; i < (temp.length + 1)/2; i ++){
            tem = temp[i];
            temp[i] = temp[temp.length - 1 - i];
            temp[temp.length - 1 - i] = tem;
        }
        long val = (((long)(temp[0] & 0xff) << 56) | ((long)(temp[1] & 0xff) << 48) | ((long)(temp[2] & 0xff) << 40) | ((long)(temp[3] & 0xff) << 32) | ((long)(temp[4] & 0xff) << 24) | ((long)(temp[5] & 0xff) << 16) | ((long)(temp[6] & 0xff) << 8) | (long)(temp[7] & 0xff));
        double value = Double.longBitsToDouble(val);
        return value;

    }

 

读取类:FileReader

    public List<Point2D.Double> readFileCor(String path){
        List<Point2D.Double> points = new ArrayList<Point2D.Double>();
        File file = new File(path);
        
        try {
            FileInputStream in = new FileInputStream(file);
            ByteToOther tTO = new ByteToOther();
            for(int i = 0; i < file.length()/16; i ++){
                Point2D.Double point = new Point2D.Double();
                byte[] px = new byte[8];
                byte[] py = new byte[8];
                in.read(px);
                in.read(py);
                point.x = tTO.doubleFromByte(px);
                point.y = tTO.doubleFromByte(py);
                points.add(point);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return points;
    }

注:如果文件中的数据顺序:高位在前、低位在后时,ByteToOther类应做如下处理

   public int intFromByte(byte[] temp){
        int value = 0;
        value = (((temp[0] & 0xff) << 24) | ((temp[1] & 0xff) << 16) | ((temp[2] & 0xff) << 8) | (temp[3] & 0xff));
        return value;
    }


    public double doubleFromByte(byte[] temp){
        byte tem = 0;
        long val = (((long)(temp[0] & 0xff) << 56) | ((long)(temp[1] & 0xff) << 48) | ((long)(temp[2] & 0xff) << 40) | ((long)(temp[3] & 0xff) << 32) | ((long)(temp[4] & 0xff) << 24) | ((long)(temp[5] & 0xff) << 16) | ((long)(temp[6] & 0xff) << 8) | (long)(temp[7] & 0xff));
        double value = Double.longBitsToDouble(val);
        return value;

   }

转载于:https://www.cnblogs.com/Jiphen/archive/2012/04/28/2475384.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将Java十六进制txt文件转换成bin文件,可以按照以下步骤进行操作: 第一步,读取txt文件。使用Java文件输入流(FileInputStream)来读取txt文件中的内容,并将其保存到一个字符串中。 第二步,将十六进制字符串转换成字节数组。可以使用Java的字符串方法substring和Integer类的parseInt方法,将字符串中的每两个字符解析为一个十六进制数,并将其转换成字节数组。 第三步,写入字节数组到bin文件中。使用Java文件输出流(FileOutputStream)来写入字节数组到bin文件中。 下面是一个示例代码实现: ```java import java.io.*; public class HexToBinConverter { public static void main(String[] args) { String filePath = "hex.txt"; // txt文件路径 String outputFilePath = "binary.bin"; // bin文件输出路径 try { // 读取txt文件 BufferedReader br = new BufferedReader(new FileReader(filePath)); String hexString = br.readLine(); // 将十六进制字符串转换成字节数组 byte[] byteArray = new byte[hexString.length() / 2]; for (int i = 0; i < hexString.length(); i += 2) { String hex = hexString.substring(i, i + 2); byteArray[i / 2] = (byte) Integer.parseInt(hex, 16); } // 写入字节数组到bin文件 FileOutputStream fos = new FileOutputStream(outputFilePath); fos.write(byteArray); // 关闭流 br.close(); fos.close(); System.out.println("转换成功!"); } catch (IOException e) { e.printStackTrace(); } } } ``` 以上是一个简单的示例代码,通过读取txt文件中的十六进制字符串,并将其转换成字节数组,并将字节数组写入到bin文件中。要使用该代码,请将`filePath`变量设置为输入txt文件的路径,将`outputFilePath`变量设置为输出bin文件的路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值