Convert hexadecimal value in decimal value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert hexadecimal value in decimal value
# 1  
Old 11-29-2012
Convert hexadecimal value in decimal value

hi all,
this is my script:
Code:
#! /bin/sh

minutes=$( { i2cget -f -y 0 0x51 3; } 2>&1 )
minutes=${minutes:2}
 
hour=$( { i2cget -f -y 0 0x51 4; } 2>&1 )
hour=${hour:2}
 
day=$( { i2cget -f -y 0 0x51 5; } 2>&1 )
day=${day:2}
 
month=$( { i2cget -f -y 0 0x51 7; } 2>&1 )
month=${month:2}
 
year=$( { i2cget -f -y 0 0x51 8; } 2>&1 )
year=${year:2}
 
date -s 20$year$month$day-$hour:$minutes

i want get data and time from a RealTimeClock plugged on i2c of my embedded board and after setdate with this value.

but with i2cget command i receive an hexdecimal value like 0x54 and with year=${year:2} i have year =54 but i need convert this number to decimal, but how?

I cant use bc because in my embedded board there isnt.
thanks
# 2  
Old 11-29-2012
What is on your embedded board? Do you have dc? What shells do you have?
# 3  
Old 12-01-2012
Try this awk code:
Code:
awk 'BEGIN {
  h["0"]="0"; h["1"]="1"; h["2"]="2"; h["3"]="3"
  h["4"]="4"; h["5"]="5"; h["6"]="6"; h["7"]="7"
  h["8"]="8"; h["9"]="9"; h["A"]="10"; h["B"]="11"
  h["C"]="12"; h["D"]="13"; h["E"]="14"; h["F"]="15"
}
{
  l=length($0)
  s=h[substr($0,l--,1)]
  for(i=l;i>=1;i--) {
    s=s+ h[substr($0,i,1)]*(16^++j)
  }
  print s
}'


This is the out I get with with an example:
Code:
$ year=54
$ year=$(echo $year| awk 'BEGIN {
  h["0"]="0"; h["1"]="1"; h["2"]="2"; h["3"]="3"
  h["4"]="4"; h["5"]="5"; h["6"]="6"; h["7"]="7"
  h["8"]="8"; h["9"]="9"; h["A"]="10"; h["B"]="11"
  h["C"]="12"; h["D"]="13"; h["E"]="14"; h["F"]="15"
}
{
  l=length($0)
  s=h[substr($0,l--,1)]
  for(i=l;i>=1;i--) {
    s=s+ h[substr($0,i,1)]*(16^++j)
  }
  print s
}')
$
$ echo $year
84
$

# 4  
Old 12-01-2012
Code:
year=54
printf "%d\n" "0x"$( echo $year )
84

# 5  
Old 12-01-2012
Code:
$ year=54
$ echo "obase=10;ibase=16;$year" | bc
84

# 6  
Old 12-01-2012
For the (bash) shell enthusiast; works for quite lengthy hex numbers; might work on other shells too after slight modifications:
Code:
$ year=ABCD
$ Nr=0; base=1;for i in $(echo -n ${year^^}|od -An -td1|tac -s" "); do ((Nr+=($i>54?$i-55:$i-48)*base )); ((base*=16)); done; echo $Nr
43981


Last edited by RudiC; 12-01-2012 at 06:31 PM..
# 7  
Old 12-01-2012
Code:
#!/bin/bash
typeset -i hx=0xabcd
echo $hx

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Convert hexadecimal value to decimal value

Hi All, cat filename | awk '{print $1, $2, $4, $5, $6, $7, $8, $9, $10;}' | awk 'NF > 0' OUTPUT: 2015-01-19 00:12:32 00000000fbfa0000 000000009ae5cf80 014d 015d 0017 003c 0362de20 2015-01-19 00:13:52 00000000fc820000 00000000994c6758 014c 015d 000b 003c 08670250 2015-01-19 00:14:25... (12 Replies)
Discussion started by: sam@sam
12 Replies

2. Programming

Urgent help needed.. C++ program to convert decimal to hexa decimal

Hi , seq can be 0...128 int windex = seq / 8; int bindex = seq % 8; unsigned char bitvalue = '\x01' << (7-bindex) ; bpv.bitmapvalue = bitvalue; This is the part of a program to convert decimal to bitmap value of hexadecimal. I want this to change to convert only to... (1 Reply)
Discussion started by: greenworld123
1 Replies

3. UNIX for Dummies Questions & Answers

Convert hexa decimal to decimal

Hi, I want to convert two hexadecimal numbers to decimal using unix command line. 1cce446295197a9d6352f9f223a9b698 fc8f99ac06e88c4faf669cf366f60d I tried using `echo "ibase=16; $no |bc` printf '%x\n' "1cce446295197a9d6352f9f223a9b698" but it doesn't work for such big number it... (4 Replies)
Discussion started by: sudhakar T
4 Replies

4. Shell Programming and Scripting

NAWK conversion of hexadecimal input to decimal output via printf, I am close I can feel it

I have searched and the answers I have found thus far have led me to this point, so I feel I am just about there. I am trying to convert a column of hexadecimal to decimal values so that I can filter out via grep just the data I want. I was able to pull my original 3 character hex value and... (10 Replies)
Discussion started by: PCGameGuy
10 Replies

5. Shell Programming and Scripting

To convert file with decimal to another file with Hexadecimal

I have a text file of alphanumeric values listed one by one. I have to convert them to hexadecimal equivalents for each character seperated by ":" in Unix bash shell script. For example, 12345678 has to be converted to 31:32:33:34:35:36:37:38 (10 Replies)
Discussion started by: mathie
10 Replies

6. Shell Programming and Scripting

Convert exponential value to decimal

Hi, Here is my script to read a file into array: awk -F '+' ' # load first file into array indexed by fields 1 and 2 NR == FNR { file1nr = FNR for (i=3; i<NF; i++) { file1 = $i } I have this... (5 Replies)
Discussion started by: Sangtha
5 Replies

7. UNIX for Dummies Questions & Answers

Hexadecimal to Decimal

Hi all, I have a small script to convert my HexaDecimal Input to Decimal as output. #!/bin/ksh hd=00208060 dec=`printf %d $hd` echo $dec Output of the above program: printf: 00208060 not completely converted 16 But my expected output is "2130016". How can i acheive this. I... (2 Replies)
Discussion started by: Arunprasad
2 Replies

8. Shell Programming and Scripting

need script to convert number in hexadecimal

hi , i need a script to convert number into hexadecimal base for example: 237=>ED it s very important for me thank you in advance for you help (5 Replies)
Discussion started by: mips
5 Replies

9. Shell Programming and Scripting

Decimal to Hexadecimal conversion

Hi frnds :) I need a small help... I have a very long file containing 20 digits decimal number which i want to convert into the corresponding 16 digit hexadecimal values. File looks like.... 11908486672755551741 05446378739602232559 04862605079740156652 . . . I tried the script for i... (7 Replies)
Discussion started by: vanand420
7 Replies

10. UNIX for Dummies Questions & Answers

Convert hexadecimal to decimal base

Hello ! Does anyone knows how can I convert hexadecimal to decimal base in the ksh or csh script ?? Thanks ! Witt (1 Reply)
Discussion started by: witt
1 Replies
Login or Register to Ask a Question