LinuxQuestions.org
Visit the LQ Articles and Editorials section
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
 
Thread Tools
Old 09-03-2007, 04:06 AM   #1
trscookie
Member
 
Registered: Apr 2004
Location: oxford
Distribution: gentoo
Posts: 353
Thanked: 0
Bash + How to add Hexadecimal numbers


[Log in to get rid of this advertisement]
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.
trscookie is offline     Reply With Quote
Old 09-03-2007, 04:13 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 1,814
Blog Entries: 5
Thanked: 115
Code:
# echo $((0xA + 1))
11
# echo $((0xA + 2))
12
# echo $((0xA + 3))
13
# echo $((0xF + 3))
18
# echo $((0xF + 0xF))
30
ghostdog74 is offline     Reply With Quote
Old 09-03-2007, 04:17 AM   #3
trscookie
Member
 
Registered: Apr 2004
Location: oxford
Distribution: gentoo
Posts: 353
Thanked: 0

Original Poster
hi, thanks for a super speedy response but the answer is in decimal, how do you get the answer in hex?
trscookie is offline     Reply With Quote
Old 09-03-2007, 04:25 AM   #4
scoban
Member
 
Registered: Nov 2004
Location: Turkey
Distribution: Slackware
Posts: 124
Thanked: 0
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
scoban is offline     Reply With Quote
Old 09-03-2007, 04:49 AM   #5
trscookie
Member
 
Registered: Apr 2004
Location: oxford
Distribution: gentoo
Posts: 353
Thanked: 0

Original Poster
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 04:51 AM..
trscookie is offline     Reply With Quote
Old 09-03-2007, 04:50 AM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 1,814
Blog Entries: 5
Thanked: 115
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
ghostdog74 is offline     Reply With Quote
Old 09-03-2007, 04:52 AM   #7
trscookie
Member
 
Registered: Apr 2004
Location: oxford
Distribution: gentoo
Posts: 353
Thanked: 0

Original Poster
Sorry, just realised that it is case sensitive, thanks for all of your help. trscookie
trscookie is offline     Reply With Quote
Old 09-03-2007, 05:22 PM   #8
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mandriva, Ubuntu, LFS, gNewSense
Posts: 221
Thanked: 0
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))
cfaj is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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


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


All times are GMT -5. The time now is 01:30 AM.

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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration