Visit Jeremy's Blog.
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org hexa,binary,octal additions in shell scripting
 User Name Remember Me? Password
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

Notices

 07-08-2008, 05:23 AM #1 gaynut LQ Newbie   Registered: Jan 2008 Posts: 27 Rep: hexa,binary,octal additions in shell scripting hi , can anybody tell me how to add,sub,mul,division functions for octal,hexadecimal,binary numbers? thanks ~ gaynut
 07-08-2008, 05:50 AM #2 nx5000 Senior Member   Registered: Sep 2005 Location: Out Posts: 3,307 Rep: One way is to use iBase and oBase with bc: Octal: echo "obase=8;8+8" | bc -l 20 Hex: echo "obase=16;8+8" | bc -l 10 Bin: echo "obase=2;1+1" | bc -l 10 From hex to binary: echo "ibase=16;obase=2;15*1A" | bc -l 1000100010 Going from decimal to octal back to decimal: echo "ibase=2;obase=2;10*11;obase=1010;ibase=1010;15*10" | bc -l 110 150 Last edited by nx5000; 07-08-2008 at 05:51 AM.
 07-08-2008, 08:54 AM #3 smoked kipper Member   Registered: May 2008 Location: UK Distribution: Slackware,Slamd64 Posts: 81 Rep: You can do it in the shell with arithmetic expansion, if integers are sufficient. Code: ```# echo \$(( 0x100 / 010 )) 32``` It can handle any base using "base#number" syntax, so for binary: Code: ```# echo \$(( 2#10000 / 4 )) 4```
 07-09-2008, 12:40 AM #4 gaynut LQ Newbie   Registered: Jan 2008 Posts: 27 Original Poster Rep: thank u ~ gaynut

 Tags handling, numbers, unix

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post LXer Syndicated Linux News 0 03-26-2008 11:50 PM topcat Programming 15 10-28-2007 02:14 AM fax8 Linux - General 1 04-20-2006 04:29 AM jcchenz Linux - Software 1 10-26-2005 03:32 PM bamalabs Programming 3 01-20-2004 09:59 AM

All times are GMT -5. The time now is 04:57 PM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -