以十六进制形式显示文件内容

写的一个小工具,用来将文件的内容显示为十六进制格式的字符,就像UE中的Ctrl+H功能。

Hextext.java

import java.io.*;

/**
 * Application: Hextext
 * Author: Steven
 * Data: April 18, 2007
 * Purpose:
Show file's content in hexadecimal text.

 * Usage:
Java Hextext filepath

 * Description:
The result file will be created in the same directory.
Its name is composed of source file name and ".hextest" as postfix.
 */
public class Hextext
{
    public static void main( String[] args ) throws Exception
    {
        // argument check
        if ( args.length < 1 )
        {
            println( "not enough arguments!" );
            printUsage();
        }
        if (!( new File( args[0] ).exists() ) )
        {
            println("assigned file doesnot exist!");
        }
       
        // parameters
        File srcfile = new File( args[0] );
        File destfile = new File( srcfile.getAbsolutePath() + ".hextext" );
        final int NUM_OF_CHAR_PER_LINE = 8;
        FileInputStream fin = new FileInputStream( srcfile );
        PrintWriter fout = new PrintWriter( new FileWriter( destfile ) );
       
        // transfer
        byte[] chunk = new byte[ NUM_OF_CHAR_PER_LINE ];
        int rdcnt = 0;
       
       
        while ( ( rdcnt = fin.read( chunk ) ) != -1 )
        {
            // init line buf
            char[] line_buf = new char[ NUM_OF_CHAR_PER_LINE * 3 + 1 + NUM_OF_CHAR_PER_LINE * 2 ];
            for ( int i=0; i<line_buf.length; ++i )
            {
                line_buf[i] = ' ';
                line_buf[ NUM_OF_CHAR_PER_LINE * 3 ] = '|';
            }
            // write into line buf
            for ( int i=0; i<rdcnt; ++i )
            {
                char[] hexd = toHextextByte( (int)chunk[i] ).toCharArray();
                line_buf[ i*3 ] = hexd[0];
                line_buf[ i*3+1 ] = hexd[1];
                line_buf[ NUM_OF_CHAR_PER_LINE * 3 + 1 + i*2 ] = (char)chunk[i];
            }
            // write line buf
            fout.println( line_buf );
        }
       
        fin.close();
        fout.close();
       
        // report
        println( "Transfer Successfully to File:" );
        println( destfile.getAbsolutePath() );
    }
   
    public static String toHextextByte( int nch )
    {
        String ret = Integer.toHexString( nch );
        int len = ret.length();
        if ( len == 1 )
        {
            ret = "0" + ret;
        }
        else if ( len > 2 )
        {
            ret = ret.substring( len-2 );
        }
        return ret;
    }
   
    public static void println( Object o )
    {
        System.out.println( o );
    }
   
    public static void printUsage()
    {
        println("Usage:");
        println("Hextext file_path");
        println("");
    }   
}/*END OF CLASS Hextext*/

 [END]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值