以十六進位形式顯示檔案內容

來源:互聯網
上載者:User

寫的一個小工具,用來將檔案的內容顯示為十六進位格式的字元,就像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]

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.