Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 541747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:22:29+00:00 2026-05-13T10:22:29+00:00

I want to build a bash program that can read a file, like a

  • 0

I want to build a bash program that can read a file, like a *.bin and print all its hexadecimal numbers, as ‘hex’ editors do. Where I can start?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T10:22:30+00:00Added an answer on May 13, 2026 at 10:22 am

    Edit: Added “bytestream” functionality. If the script name contains the word “stream” (e.g. it’s a symlink such as ln -s bash-hexdump bash-hexdump-stream and run as ./bash-hexdump-stream), it will output a continuous stream of hex characters representing the contents of the file. Otherwise its output will look like hexdump -C.

    It takes a bunch of trickery since Bash isn’t really good at binary:

    #!/bin/bash
    # bash-hexdump
    # by Dennis Williamson - 2010-01-04
    # in response to http://stackoverflow.com/questions/2003803/show-hexadecimal-numbers-of-a-file
    # usage: bash-hexdump file
    
    if [[ -z "$1" ]]
    then
        exec 3<&0                           # read stdin
        [[ -p /dev/stdin ]] || tty="yes"    # no pipe
    else
        exec 3<"$1"            # read file
    fi
    
    # if the script name contains "stream" then output will be continuous hex digits
    # like hexdump -ve '1/1 "%.2x"'
    [[ $0 =~ stream ]] && nostream=false || nostream=true
    
    saveIFS="$IFS"
    IFS=""                     # disables interpretation of \t, \n and space
    saveLANG="$LANG"
    LANG=C                     # allows characters > 0x7F
    bytecount=0
    valcount=0
    $nostream && printf "%08x  " $bytecount
    while read -s -u 3 -d '' -r -n 1 char    # -d '' allows newlines, -r allows \
    do
        ((bytecount++))
        printf -v val "%02x" "'$char"    # see below for the ' trick
        [[ "$tty" == "yes" && "$val" == "04" ]] && break    # exit on ^D
        echo -n "$val"
        $nostream && echo -n " "
        ((valcount++))
        if [[ "$val" < 20 || "$val" > 7e ]]
        then
            string+="."                  # show unprintable characters as a dot
        else
            string+=$char
        fi
        if $nostream && (( bytecount % 8 == 0 ))      # add a space down the middle
        then
            echo -n " "
        fi
        if (( bytecount % 16 == 0 ))   # print 16 values per line
        then
            $nostream && echo "|$string|"
            string=''
            valcount=0
            $nostream && printf "%08x  " $bytecount
        fi
    done
    
    if [[ "$string" != "" ]]            # if the last line wasn't full, pad it out
    then
        length=${#string}
        if (( length > 7 ))
        then
            ((length--))
        fi
        (( length += (16 - valcount) * 3 + 4))
        $nostream && printf "%${length}s\n" "|$string|"
        $nostream && printf "%08x  " $bytecount
    fi
    $nostream && echo
    
    LANG="$saveLANG";
    IFS="$saveIFS"
    

    The apostrophe trick is documented here. The relevant part says:

    If the leading character is a
    single-quote or double-quote, the
    value shall be the numeric value in
    the underlying codeset of the
    character following the single-quote
    or double-quote.

    Here is some output from the script showing the first few lines of my /bin/bash plus a few more:

    00000000  7f 45 4c 46 01 01 01 00  00 00 00 00 00 00 00 00  |.ELF............|
    00000010  02 00 03 00 01 00 00 00  e0 1e 06 08 34 00 00 00  |............4...|
    00000020  c4 57 0d 00 00 00 00 00  34 00 20 00 09 00 28 00  |.W......4. ...(.|
    00000030  1d 00 1c 00 06 00 00 00  34 00 00 00 34 80 04 08  |........4...4...|
    . . .
    00000150  01 00 00 00 2f 6c 69 62  2f 6c 64 2d 6c 69 6e 75  |..../lib/ld-linu|
    00000160  78 2e 73 6f 2e 32 00 00  04 00 00 00 10 00 00 00  |x.so.2..........|
    00000170  01 00 00 00 47 4e 55 00  00 00 00 00 02 00 00 00  |....GNU.........|
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 281k
  • Answers 282k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Hm, we ended up making two entry points in the… May 13, 2026 at 3:57 pm
  • Editorial Team
    Editorial Team added an answer if it's a public or protected variable you can do… May 13, 2026 at 3:57 pm
  • Editorial Team
    Editorial Team added an answer We've had a lot of success with Caucho Resin's LongPoll… May 13, 2026 at 3:57 pm

Related Questions

I've found a great little program that will allow me to add user friendly
I'm using a bash script to automate a set of tests for some other
What is the best/easiest way to build a minimal task queue system for Linux
I am trying to set up an automated build system on Windows using Cygwin.
I'm struggling with a Mac OS X 10.5.8 that I've started using recently for

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.