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


  Search this Thread
Old 04-24-2010, 11:59 AM   #16
Amigo developer
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,855

Rep: Reputation: 547Reputation: 547Reputation: 547Reputation: 547Reputation: 547Reputation: 547

The portable way to do the math is like this:

These are all less portable:
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 08-31-2010, 07:23 PM   #17
LQ Newbie
Registered: Oct 2003
Posts: 1

Rep: Reputation: 0
Ok, can someone explain this one?

$ a="06"
$ b="1"
$ c=$((a+b))
$ echo $c

$ a="07"
$ b="1"
$ c=$((a+b))
$ echo $c

$ a="08"
$ b="1"
$ c=$((a+b))
-sh: 08: value too great for base (error token is "08")

$ a="09"
$ b="1"
$ c=$((a+b))
-sh: 09: value too great for base (error token is "09")

$ a="10"
$ b="1"
$ c=$((a+b))
$ echo $c
Old 08-31-2010, 08:38 PM   #18
LQ Guru
Registered: Nov 2006
Location: Belgium
Distribution: Ubuntu 11.04, Debian testing
Posts: 5,019

Rep: Reputation: 130Reputation: 130
$ a="08"
$ b="1"
$ c=$((a+b))
-sh: 08: value too great for base (error token is "08")

$ a="09"
$ b="1"
$ c=$((a+b))
-sh: 09: value too great for base (error token is "09")
Prefacing a number with 0 makes it octal. Your problem is that octal uses units of 8, expressed as the range 0-7, so 08 is plain nonsense. Decimal 8 would be octal (0)10 and decimal 9 would be octal (0)11.

echo $((8#10)) <--- base 8 (octal) value 10 is decimal 8; this will print 8
echo $((2#10)) <--- base 2 (binary) 10 is decimal 2; it will print 2
echo $((16#10)) <--- base 16 (hexadecimal) 10 is decimal 16; it will print 16

Last edited by jay73; 08-31-2010 at 08:46 PM.
2 members found this post helpful.


bash, conversion, convert, integer, string, type

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
converting string in integer in bash script dziadgba Linux - Newbie 5 08-31-2009 05:59 PM
C++ converting from char to integer MicahCarrick Programming 4 12-19-2005 02:16 PM
Integer To a String in C Bean101 Programming 2 05-27-2004 04:46 AM
converting integer value to hexadecimal string in C - any suggestions?? woodywellhung Programming 3 04-24-2004 05:27 PM
Converting integer types... JStew Programming 3 12-03-2002 08:53 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:36 PM.

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