Req on how to convert hex numbers to decimals


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Req on how to convert hex numbers to decimals
# 1  
Old 03-21-2008
Req on how to convert hex numbers to decimals

Hi,
If i have an input as
c1:41 c2:0x0000.00046b3e
I want to make output display as
c1:41 c2:224062
.
Basically convert first part 0x0000 (as hex) to decimal which is 0 and
convert second part 0x00046b3e (as hex) to decimal which is 289598
and as such add both parts namely 0+289598=289598
.
Is there a way to do this via awk or shell script?
Thanks
Hare.
# 2  
Old 03-21-2008
in ksh:
Code:
$ input=0x00046b3e
$ newinput=16#${input##+([0x])}
$ echo $newinput
16#46b3e
$ typeset -i10 output
$ output=$newinput
$ echo $output
289598
$

# 3  
Old 03-21-2008
Code:
#!/bin/ksh
# this does the math
echo "c1:41 c2:0x0000.00046b3e" | awk -F[:\.] '{ print $3, $4}' | read one two
result=$( printf "%d + %d" $one  0x"$two")
echo $result
result=$( printf "%d + %d" 0x0x000 0x00046b3e )    # check 
echo $result

# 4  
Old 03-21-2008
Or with printf:

Code:
printf "%d\n" "0x00046b3"

Regards

Edit: Jim was faster!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Assign numbers with decimals from a line to a variable

I am trying to assign a string of numbers with their decimals to a variable. The code reads a file called 'log'. I have not included the entire file since it's huge. The line is: Tagging release with the label program-2.8.114...My code: BUILDNUMFORSIT=$(egrep 'Tagging release with the... (3 Replies)
Discussion started by: sgffgs
3 Replies

2. Shell Programming and Scripting

Convert hex to decimal

can someone help me in converting hex streams to decimal values using perl script Hex value: $my_hex_stream="0c07ac14001676"; Every hex value in the above stream should be converted in to decimal and separated by comma. The output should be: 12,07,172,20,00,22,118 (2 Replies)
Discussion started by: Arun_Linux
2 Replies

3. Shell Programming and Scripting

Convert to Hex in perl

Hi, i want to convert number 5860533159 to hexadecimal. i need to use perl. i used $foo = 5860533159; $hexval3 = sprintf("%#x", $foo); i am getting value as 0xffffffff. i need to get value as 0x15D50A3A7. when i converted using google calculator, i got the correct value, expected... (9 Replies)
Discussion started by: asak
9 Replies

4. Shell Programming and Scripting

Convert Hex - KSH

Hello, I woild like to convert hex on KSH not BASH: I tried to use: tmp=31 printf "\x"${tmp}"" it works on bash - Output is '1' but not on ksh. please advice on the right syntax. Thanks. (4 Replies)
Discussion started by: LiorAmitai
4 Replies

5. Shell Programming and Scripting

How to convert hex numbers to decimal ?

Hi, please tell me how to convert hex number to decimal 000000E7 000000000002640D 0000000000025B16 and seconds to minutes, hours, days, months, years bytes to kbytes, mbytes , gbytes read the following examples while read a b do printf "%5d %5d\n" "0x$a" "0x$b" done < "$FILE"... (15 Replies)
Discussion started by: jack2
15 Replies

6. Programming

Convert HEX to EBCDIC IN C

i want to convert Hex value To EBCDIC value. i tried to convert hex to ascii and then to ebcdic but it doesn't give desired results . it doesn't give corresponding ebcdic value instead it gives some junk values. e.g; Hex EBCDIC ----------------- 81 a 82 b 83 c 84 d 85 e 86 f 87... (6 Replies)
Discussion started by: junaid.nehvi
6 Replies

7. Shell Programming and Scripting

convert Regular decimals to Packed decimals

Hi, I am trying to find if there is a way to convert regular decimal values to Paced decimal values. I tried to find a c program but I could get a Packed converted to regular decimal not the other way round. If not unix please let me know if any other progrimming language I can use to do... (2 Replies)
Discussion started by: mgirinath
2 Replies

8. Shell Programming and Scripting

How to Convert Hex value to Dec ?

Hi All, I want to convert below Hex value to Dec value in each column .How to do it ? This data is in a 1 file. 4e20 0475 2710 010f 7530 69a2 7530 7e2f 4e20 02dd 7530 6299 4e20 0c0a 7530 69a2 4e20 0a0b 2710 0048 7530 7955 4e20 0d23 7530 622d 7530 9121 2710 001f 7530 7d3f (6 Replies)
Discussion started by: Nayanajith
6 Replies

9. Shell Programming and Scripting

ksh and hex numbers

typeset -i A=16#0 typeset -u A=$a y=${A#16#} This converted $a to hex and stored it in y. Can someone walk me through how this was done? thanks (2 Replies)
Discussion started by: JamesByars
2 Replies

10. Shell Programming and Scripting

comparing two numbers with the decimals

Can someone tell me how do I comapre two numbers with the decimals in UNIX shell scripting I understand "-gt" can be used only for integers Regards, Giri (4 Replies)
Discussion started by: chittari
4 Replies
Login or Register to Ask a Question