Decimal to Hexadecimal conversion


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Decimal to Hexadecimal conversion
# 1  
Old 09-19-2006
Decimal to Hexadecimal conversion

Hi frnds Smilie
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 in `cat myfile`
do
echo 'ibase=10;obase=16;$i' | bc
done
but it does not works and gives error "syntax error on line 1, teletype"
please help me out to solve the problem
thnx in advance.
# 2  
Old 09-19-2006
Replace the ' with ". The ' prevents values from getting substituted in place of the variables, which bc does not like.
# 3  
Old 09-19-2006
Quote:
Originally Posted by blowtorch
Replace the ' with ". The ' prevents values from getting substituted in place of the variables, which bc does not like.

thanx very much it works...
but one more problem arised the output hexdec which i gets varies in character length some are 15 char while some 16 char. Please help how to make them equi length 16 chars by padding 0 (zero) on left most of 15 char output.
thnx in advance. output looks like...
5566a24546934981
343647852757202
I want...
5566a24546934981
0343647852757202
# 4  
Old 09-19-2006
Use printf. Like this:
Code:
for i in `cat test`; do 
   printf "%16s\n" $(echo "ibase=10;obase=16;$i" | bc)
done

This will not add a zero, just a space. To add a zero, pipe the printf through tr ' ' '0'
# 5  
Old 09-19-2006
Quote:
Originally Posted by blowtorch
Use printf. Like this:
Code:
for i in `cat test`; do 
   printf "%16s\n" $(echo "ibase=10;obase=16;$i" | bc)
done

This will not add a zero, just a space. To add a zero, pipe the printf through tr ' ' '0'


thnx for reply
but it gives error
syntax error: '(' unexpected
another options available pl.....
# 6  
Old 09-19-2006
You might be using sh. In that case, replace
Code:
$(echo "ibase=10;obase=16;$i" | bc)

with
Code:
`echo "ibase=10;obase=16;$i" | bc`

It should work. I checked.
# 7  
Old 09-19-2006
Quote:
Originally Posted by blowtorch
You might be using sh. In that case, replace
Code:
$(echo "ibase=10;obase=16;$i" | bc)

with
Code:
`echo "ibase=10;obase=16;$i" | bc`

It should work. I checked.

Smilie thnx very much it is working fine....
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. Shell Programming and Scripting

Conversion from Hexadecimal to binary

How can I convert hexadecimal values to Binary from the second field to the end Input: WS-2 23 345 235 DT-3 45 4A3 000 pp-2 76 300 E4 Output: WS-2 100011 1101000101 1000110101 DT-3 1000101 10010100011 000 pp-2 1110110 1100000000 11100100 (16 Replies)
Discussion started by: aydj
16 Replies

3. Shell Programming and Scripting

Hexadecimal to Binary conversion

Hi Guys, Is it possible to convert the hexadecimal to Binary by unix command.....I could not figure out.... If I need to convert AF6D to binary...what could be the way to do? Thanks in advance!! ---------- Post updated at 02:57 AM ---------- Previous update was at 02:42 AM ---------- I... (6 Replies)
Discussion started by: Indra2011
6 Replies

4. Shell Programming and Scripting

Convert hexadecimal value in decimal value

hi all, this is my script: #! /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} ... (6 Replies)
Discussion started by: enaud
6 Replies

5. 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

6. 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

7. Shell Programming and Scripting

Decimal to hex conversion

Dear All PROs Thanks in advance need a shell for Decimal to hex conversion input file (decimal values) 65,5,48,66,133,131,118,47 65,5,48,66,133,131,83,63 . . desire output should be (Hex value)... (11 Replies)
Discussion started by: The_Archer
11 Replies

8. Shell Programming and Scripting

need help with ascii to decimal conversion

Hi, Can anyone please help me ascci to decimal conversion in bash I have a file which contains stream of numbers like this,these are ascci values 729711810132973278105991013268971213233 I want to covert it to its actual value like upper code's decimal is "Have a Nice Day!" ... (15 Replies)
Discussion started by: sunilmenhdiratt
15 Replies

9. 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

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