![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
AWK Help
HI All,
Need a help Suppose I have a file having the data 1,FF,233 where the second field is Hex now I want to convert this hex in dec and see the output as 1,255,233 Request you all to help Regards, Gaurav Goel |
|
||||
|
If you have Python, here's an alternative
Code:
for line in open("file"):
line = line.strip().split(",")
print "%s,%s,%s" % (line[0],str(int(line[1],16)), line[2])
Code:
function hex2dec(x, h, i, n, l) {
h = "0123456789ABCDEF..........abcdef"
for (i = l = length(x); i > 0; i--)
n += (index(h, substr(x, i, 1)) - 1) % 16 * 16 ^ (l - i)
return n
}
function dec2hex(d) {
return sprintf("%lx", d)
}
|
|
||||
|
Using printf
Using printf
HTML Code:
#/bin/ksh for v in $(cut -d, -f2 /tmp/hex) ; do printf '%d\n' "0x$v" done HTML Code:
#/bin/ksh for v in $(cut -d, -f2 /tmp/hex) ; do print "ibase=16;$v" |bc done 1,FF,233 1,EF,233 Output: 255 239 Hi Gurus, Please let us know if the following awk can be used for the above conversion, HTML Code:
awk -F, ' { printf("The Decimal value of %s is [%x]\n", $2,$2) }' /tmp/hex
Thanks Nagarajan Ganesan |
|
||||
|
Quote:
|
|
||||
|
Quote:
Code:
awk -v VAR=A 'END{ printf"%s %s\"%s\n", "echo \"ibase=16;", VAR, " | bc" }' /dev/null | sh
![]() |
|
||||
|
Quote:
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|