LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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
 
Search this Thread
Old 04-24-2010, 11:59 AM   #16
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,749

Rep: Reputation: 461Reputation: 461Reputation: 461Reputation: 461Reputation: 461

The portable way to do the math is like this:
var=$(($var-1))

These are all less portable:
var=((var-1))
var=$((var-1))
((var--))
let...
 
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
EricAiken
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
7

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

$ 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
11
 
Old 08-31-2010, 08:38 PM   #18
jay73
Guru
 
Registered: Nov 2006
Location: Belgium
Distribution: Ubuntu 11.04, Debian testing
Posts: 5,019

Rep: Reputation: 130Reputation: 130
Quote:
$ 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.
  


Reply

Tags
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


All times are GMT -5. The time now is 03:41 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