LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
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

Reply
 
LinkBack Search this Thread
Old 09-03-2007, 03:06 AM   #1
trscookie
Member
 
Registered: Apr 2004
Location: oxford
Distribution: gentoo
Posts: 438

Rep: Reputation: 30
Bash + How to add Hexadecimal numbers


Hi all,

How do you add two hexadecimal numbers together in bash? I have looked all over google and could not find an answer.

Thanks, trscookie.
 
Old 09-03-2007, 03:13 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 239Reputation: 239Reputation: 239
Code:
# echo $((0xA + 1))
11
# echo $((0xA + 2))
12
# echo $((0xA + 3))
13
# echo $((0xF + 3))
18
# echo $((0xF + 0xF))
30
 
Old 09-03-2007, 03:17 AM   #3
trscookie
Member
 
Registered: Apr 2004
Location: oxford
Distribution: gentoo
Posts: 438

Original Poster
Rep: Reputation: 30
hi, thanks for a super speedy response but the answer is in decimal, how do you get the answer in hex?
 
Old 09-03-2007, 03:25 AM   #4
scoban
Member
 
Registered: Nov 2004
Location: Turkey
Distribution: Slackware
Posts: 145

Rep: Reputation: 16
You can use "bc".

Code:
$ bc -l
obase=16 # Order is important (see note below)
ibase=16 # Once you set input base all numbers are in that base

9+3
C

ibase=A  # set input base to decimal
obase=10 # set output base to decimal

9+3
12
 
Old 09-03-2007, 03:49 AM   #5
trscookie
Member
 
Registered: Apr 2004
Location: oxford
Distribution: gentoo
Posts: 438

Original Poster
Rep: Reputation: 30
Getting the wrong result????:


Code:
adrian@adrians-laptop:~$ bc -l
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 
ibase=16
obase=16
a1+1
 01


adrian@adrians-laptop:~$ bc -l
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 
obase=16
ibase=16
a1+1
1

shouldn't the answer be a2? Am I doing something wrong here?

Thanks.

Last edited by trscookie; 09-03-2007 at 03:51 AM.
 
Old 09-03-2007, 03:50 AM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 239Reputation: 239Reputation: 239
Quote:
Originally Posted by trscookie View Post
hi, thanks for a super speedy response but the answer is in decimal, how do you get the answer in hex?
Code:
# var=$((0xA + 1))
# echo $var
11
# echo 16o${var}p |dc
B
 
Old 09-03-2007, 03:52 AM   #7
trscookie
Member
 
Registered: Apr 2004
Location: oxford
Distribution: gentoo
Posts: 438

Original Poster
Rep: Reputation: 30
Sorry, just realised that it is case sensitive, thanks for all of your help. trscookie
 
Old 09-03-2007, 04:22 PM   #8
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by trscookie View Post
hi, thanks for a super speedy response but the answer is in decimal, how do you get the answer in hex?
Code:
printf "%X\n" $((0xA + 2))
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to add numbers in a formatted file via a bash script? zero79 Linux - General 8 12-24-2010 05:48 PM
Perl add numbers in 2nd field twantrd Programming 6 10-19-2006 08:26 PM
Add consecutive numbers to a Chinese file tcma Linux - General 0 10-19-2004 03:25 PM
Help.. how do I add two numbers? Tengil Linux - Newbie 3 03-04-2004 12:58 PM
Bash script: add all numbers from command output wi-Z-art Programming 2 08-06-2003 09:16 AM


All times are GMT -5. The time now is 05:13 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration