Hexdump

Post questions regarding programming in C/C++ in Linux/Unix.

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

Hexdump

Postby kapilasr » Mon Jan 09, 2012 2:11 am

Hi,

Has anyone smiler c++ code to get hex dump of byte array ?

If not
I have Java method, please guide me to convert to c++

Java code

Code: Select all

public static String hexdump (byte[] b, int offset, int len) {
        StringBuffer sb    = new StringBuffer ();
        StringBuffer hex   = new StringBuffer ();
        StringBuffer ascii = new StringBuffer ();
        String sep         = "  ";
        String lineSep     = System.getProperty ("line.separator");

        for (int i=offset; i<len; i++) {
            char hi = Character.forDigit ((b[i] >> 4) & 0x0F, 16);
            char lo = Character.forDigit (b[i] & 0x0F, 16);
            hex.append (Character.toUpperCase(hi));
            hex.append (Character.toUpperCase(lo));
            hex.append (' ');
            char c = (char) b[i];
            ascii.append ((c >= 32 && c < 127) ? c : '.');

            int j = i % 16;
            switch (j) {
                case 7 :
                    hex.append (' ');
                    break;
                case 15 :
                    sb.append (hexOffset (i));
                    sb.append (sep);
                    sb.append (hex.toString());
                    sb.append (' ');
                    sb.append (ascii.toString());
                    sb.append (lineSep);
                    hex   = new StringBuffer ();
                    ascii = new StringBuffer ();
                    break;
            }
        }
        if (hex.length() > 0) {
            while (hex.length () < 49)
                hex.append (' ');

            sb.append (hexOffset (b.length));
            sb.append (sep);
            sb.append (hex.toString());
            sb.append (' ');
            sb.append (ascii.toString());
            sb.append (lineSep);
        }
        return sb.toString();
    }
kapilasr
 
Posts: 2
Joined: Sun Jan 08, 2012 11:40 pm

Return to Unix/Linux

Who is online

Users browsing this forum: No registered users and 1 guest