# Converting hex to ascii/decimal

**URL:** <https://community.unix.com/t/converting-hex-to-ascii-decimal/285559>\
**Category:** Shell Programming and Scripting\
**Created:** [March 31, 2011, 9:45am UTC](https://community.unix.com/t/converting-hex-to-ascii-decimal/285559 "2011-03-31T09:45:16Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shiftkey](https://community.unix.com/letter_avatar/shiftkey/32/5_5575768a8748004e209b776fc1b2916d.png) [@Shiftkey](https://community.unix.com/u/Shiftkey)\
**Post date:** [March 31, 2011, 9:45am UTC](https://community.unix.com/t/converting-hex-to-ascii-decimal/285559/1 "2011-03-31T09:45:16Z")

</div>

I am writing a bash script to do some parsing on a log and I am running into a problem when it comes to converting only certain sections of the file from hex to ascii or hex to decimal.

Data Example:

> [@](#):
>
> * * *
> 
> ip = 10.2.3.4  
> ------------  
> Hardware= [38 30 30 30]  
> Errors= [00 a1]  
> Full= [63]  
> SW Version= [31 2e 38 2e 31 2e 31 30 39]  
> Test= [76]  
> ------------  
> ip = 10.2.3.8  
> ------------  
> Hardware= [34 30 30 30]  
> Errors= [00 a1]  
> Full= [45]  
> SW Version= [31 2e 38 2e 31 2e 31 30 39]  
> Test= [76]

The hex values after Hardware and SW Version I need to convert from Hex to ASCII and the Errors, Full, Test values I need to convert from hex to decimal.

The output should look like the following:

> [@](#):
>
> * * *
> 
> ip = 10.2.3.4  
> ------------  
> Hardware= [8000]  
> Errors= [161]  
> Full= [99]  
> SW Version= [1.8.1.109]  
> Test= [118]  
> ------------  
> ip = 10.2.3.8  
> ------------  
> Hardware= [4000]  
> Errors= [161]  
> Full= [69]  
> SW Version= [1.8.1.109]  
> Test= [118]

Thanks for any help!

---

<div class="post-metadata">

**Author:** ![Perderabo](https://community.unix.com/user_avatar/community.unix.com/perderabo/32/27_2.png) [@Perderabo](https://community.unix.com/u/Perderabo)\
**Post date:** [March 31, 2011, 10:10am UTC](https://community.unix.com/t/converting-hex-to-ascii-decimal/285559/2 "2011-03-31T10:10:12Z")

</div>

```nohighlight
$
$ cat xverter
#! /usr/bin/bash

# convert from hex to ascii
sample=" 38 30 30 30"
sample=${sample// /\\x}
sample="$sample\\n"
printf "$sample"

#
# convert from base 16 to base 10

hex=76
((dec=0X$hex))
echo $dec

exit 0
$
$
$ ./xverter
8000
118
$

```

---

<div class="post-metadata">

**Author:** ![Shiftkey](https://community.unix.com/letter_avatar/shiftkey/32/5_5575768a8748004e209b776fc1b2916d.png) [@Shiftkey](https://community.unix.com/u/Shiftkey)\
**Post date:** [March 31, 2011, 10:20am UTC](https://community.unix.com/t/converting-hex-to-ascii-decimal/285559/3 "2011-03-31T10:20:25Z")

</div>

Thanks , but how would I go about searching for the line and then converting whatever is in between the brackets because its not going to be the same values every time.

Perhaps id have to search for Hardware and then take whatever is in between the brackets set as a variable and then use the above code for the conversion?

---

<div class="post-metadata">

**Author:** ![Perderabo](https://community.unix.com/user_avatar/community.unix.com/perderabo/32/27_2.png) [@Perderabo](https://community.unix.com/u/Perderabo)\
**Post date:** [March 31, 2011, 10:26am UTC](https://community.unix.com/t/converting-hex-to-ascii-decimal/285559/4 "2011-03-31T10:26:27Z")

</div>

> [@shiftkey](#):
>
> Perhaps id have to search for Hardware and then take whatever is in between the brackets set as a variable and then use the above code for the conversion?

Exactly. And you will need to ensure that you have exactly one space before each hex value for the hex to ascii conversion. And no spaces at all for the hex to decimal conversion.

---

<div class="post-metadata">

**Author:** ![kurumi](https://community.unix.com/letter_avatar/kurumi/32/5_5575768a8748004e209b776fc1b2916d.png) [@kurumi](https://community.unix.com/u/kurumi)\
**Post date:** [March 31, 2011, 10:35am UTC](https://community.unix.com/t/converting-hex-to-ascii-decimal/285559/5 "2011-03-31T10:35:45Z")

</div>

Ruby(1.9+)

```nohighlight
#!/usr/bin/env ruby 
File.open("file").each do|line|
    if line =~ /^(SW|Hardware)|Error|Test|Full/i
        toconv = line.scan(/\[(.*?)\]/)[0][0]
        if line =~ /SW|Hardware/
            toconv = toconv.split.map{|x|x.hex.chr}.join
        else
            toconv = toconv.split.map{|x|x.hex}.join
        end
        line.gsub!( /(.*\[)(.*)(\].*)/ , "\\1#{toconv}\\3" )
    end
    puts line
end

```

Test run:

```nohighlight
$ ruby myconvert.rb
------------
ip = 10.2.3.4
------------
Hardware= [8000]
Errors= [0161]
Full= [99]
SW Version= [1.8.1.109]
Test= [118]
------------
ip = 10.2.3.8
------------
Hardware= [4000]
Errors= [0161]
Full= [69]
SW Version= [1.8.1.109]
Test= [118]

```

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [March 31, 2011, 10:53am UTC](https://community.unix.com/t/converting-hex-to-ascii-decimal/285559/6 "2011-03-31T10:53:57Z")

</div>

A bit verbose, but......  
`nawk -f shift.awk myFile`

shift.awk:

```nohighlight
BEGIN {
  FS="([[]|[]])"
}
function hex2dec(h, i,x,v){
  h=tolower(h);sub(/^0x/,"",h)
  for(i=1;i<=length(h);++i){
    x=index("0123456789abcdef",substr(h,i,1))
    v=(16*v)+x-1
  }
  return v
}

function hex2str(h) {
  printf("%c", hex2dec(h))
}
/^(--|ip)/ {print; next}

/^(Hardware|SW Version)=/ {
  printf("%s",$1 "[")
  n=split($2, vA,OFS)
  for(i=1;i<=n;i++)
     printf("%c%s", hex2str(vA), (i==n)? "]"ORS:"")
  next
}
{
  printf("%s",$1 "[")
  n=split($2, vA,OFS)
  for(i=1;i<=n;i++)
     printf("%d%s", hex2dec(vA), (i==n)? "]"ORS:"")
}

```

---

<div class="post-metadata">

**Author:** ![Shiftkey](https://community.unix.com/letter_avatar/shiftkey/32/5_5575768a8748004e209b776fc1b2916d.png) [@Shiftkey](https://community.unix.com/u/Shiftkey)\
**Post date:** [March 31, 2011, 11:38am UTC](https://community.unix.com/t/converting-hex-to-ascii-decimal/285559/7 "2011-03-31T11:38:38Z")

</div>

Thanks but I dont have ruby installed so I cant test that code out.

trying to finish it up with awk and sed atm, will post if I can get it working.

---------- Post updated at 10:38 AM ---------- Previous update was at 09:57 AM ----------

Awesome vgersh99! I tweaked a few things to match my actual log file and it works perfectly.

Im not to familiar with using awk with bash scripts though, how do I go about combining that piece of awk code with the rest of my bash script? I keep getting errors when I put awk inside the script.

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [March 31, 2011, 11:46am UTC](https://community.unix.com/t/converting-hex-to-ascii-decimal/285559/8 "2011-03-31T11:46:54Z")

</div>

```nohighlight
#!/bin/bash

......
......

nawk '
 <here comes the copy/paste of shift.awk>
' myFile

....
....

```

---

<div class="post-metadata">

**Author:** ![fpmurphy](https://community.unix.com/user_avatar/community.unix.com/fpmurphy/32/110_2.png) [@fpmurphy](https://community.unix.com/u/fpmurphy)\
**Post date:** [March 31, 2011, 11:50am UTC](https://community.unix.com/t/converting-hex-to-ascii-decimal/285559/9 "2011-03-31T11:50:22Z")

</div>

Bash (and a number of other shells) has builtin support for different numeral radices

For example:

```nohighlight
$ hex=76
$ echo "Decimal equivalent is: $(( 16#76 ))"
Decimal equivalent is: 118
$ echo "Octal equivalent is: $(( 8#76 ))"
Octal equivalent is: 62
$

```

---

<div class="post-metadata">

**Author:** ![Shiftkey](https://community.unix.com/letter_avatar/shiftkey/32/5_5575768a8748004e209b776fc1b2916d.png) [@Shiftkey](https://community.unix.com/u/Shiftkey)\
**Post date:** [March 31, 2011, 2:22pm UTC](https://community.unix.com/t/converting-hex-to-ascii-decimal/285559/10 "2011-03-31T14:22:25Z")

</div>

Ok so new problem. Everything was looking ok when I output the data on the screen, But once I actual open or view the output file I get ^@ after every ASCII character. Any ideas? I tried removing with sed and tr but no luck. tr just reverts it back to its hex form

> [@](#):
>
> * * *
> 
> ip = 10.2.3.4  
> ------------  
> Hardware= [8^@0^@0^@0^@]  
> Errors= [161]  
> Full= [99]  
> SW Version= [1^@.^@8^@.^@1^@.^@1^@0^@9^@]  
> Test= [118]

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [March 31, 2011, 2:26pm UTC](https://community.unix.com/t/converting-hex-to-ascii-decimal/285559/11 "2011-03-31T14:26:59Z")

</div>

do `cat -vet myFile` and post the results (using code tags).  
Posting your modified script would help as well...

---

<div class="post-metadata">

**Author:** ![Shiftkey](https://community.unix.com/letter_avatar/shiftkey/32/5_5575768a8748004e209b776fc1b2916d.png) [@Shiftkey](https://community.unix.com/u/Shiftkey)\
**Post date:** [March 31, 2011, 2:33pm UTC](https://community.unix.com/t/converting-hex-to-ascii-decimal/285559/12 "2011-03-31T14:33:58Z")

</div>

> [@](#):
>
> ip = 10.2.3.4$  
> ------------$  
> Hardware= [8^@0^@0^@0^@]$  
> Errors= [0161]$  
> Full= [99]$  
> SW Version= [1^@.^@8^@.^@1^@.^@1^@0^@9^@]$  
> Test= [118]$  
> ------------$  
> ip = 10.2.3.8$  
> ------------$  
> Hardware= [4^@0^@0^@0^@]$  
> Errors= [0161]$  
> Full= [69]$  
> SW Version= [1^@.^@8^@.^@1^@.^@1^@0^@9^@]$  
> Test= [118]$

---------- Post updated at 02:33 PM ---------- Previous update was at 02:33 PM ----------

This was output after running

> [@](#):
>
> cat -vet myFile

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [March 31, 2011, 2:37pm UTC](https://community.unix.com/t/converting-hex-to-ascii-decimal/285559/13 "2011-03-31T14:37:22Z")

</div>

no, I meant `cat -vet ORIGINALfile` .  
Also please post your modified script.

---

<div class="post-metadata">

**Author:** ![Shiftkey](https://community.unix.com/letter_avatar/shiftkey/32/5_5575768a8748004e209b776fc1b2916d.png) [@Shiftkey](https://community.unix.com/u/Shiftkey)\
**Post date:** [March 31, 2011, 2:53pm UTC](https://community.unix.com/t/converting-hex-to-ascii-decimal/285559/14 "2011-03-31T14:53:03Z")

</div>

Modified script:

> [@](#):
>
> #!/bin/bash
> 
> awk '  
> BEGIN {  
> FS="([|])"  
> }
> 
> function hex2dec(h, i,x,v){  
> h=tolower(h);sub(/^0x/,"",h)  
> for(i=1;i\<=length(h);++i){  
> x=index("0123456789abcdef",substr(h,i,1))  
> v=(16\*v)+x-1  
> }  
> return v  
> }
> 
> function hex2str(h) {  
> printf("%c", hex2dec(h))  
> }  
> /^(--|ip|Last)/ {print; next}
> 
> /^(Hardware|SW Version)=/ {  
> printf("%s",$1 "[")  
> n=split($2, vA,OFS)  
> for(i=1;i\<=n;i++)  
> printf("%c%s", hex2str(vA[i]), (i==n)? "]"ORS:"")  
> next  
> }  
> /^(Errors|Full|Test)=/{  
> printf("%s",$1 "[")  
> n=split($2, vA,OFS)  
> for(i=1;i\<=n;i++)  
> printf("%d%s", hex2dec(vA[i]), (i==n)? "]"ORS:"")  
> }' sample.log \>temp.log

Output after running

> [@](#):
>
> cat -vet

on original data file Sample.log

> [@](#):
>
> ip = 10.2.3.4$  
> ------------$  
> Hardware= [38 30 30 30]$  
> Errors= [00 a1]$  
> Full= [63]$  
> SW Version= [31 2e 38 2e 31 2e 31 30 39]$  
> Test= [76]$  
> ------------$  
> ip = 10.2.3.8$  
> ------------$  
> Hardware= [34 30 30 30]$  
> Errors= [00 a1]$  
> Full= [45]$  
> SW Version= [31 2e 38 2e 31 2e 31 30 39]$  
> Test= [76]$

---------- Post updated at 02:53 PM ---------- Previous update was at 02:48 PM ----------

Sorry here is with code tags:

> [@](#):
>
> ```plaintext
> #!/bin/bash
> 
> awk '
> BEGIN {
> FS="([[]|[]])"
> }
> 
> function hex2dec(h, i,x,v){
> h=tolower(h);sub(/^0x/,"",h)
> for(i=1;i<=length(h);++i){
> x=index("0123456789abcdef",substr(h,i,1))
> v=(16*v)+x-1
> }
> return v
> }
> 
> function hex2str(h) {
> printf("%c", hex2dec(h))
> }
> /^(--|ip|Last)/ {print; next}
> 
> /^(Hardware|SW Version)=/ {
> printf("%s",$1 "[")
> n=split($2, vA,OFS)
> for(i=1;i<=n;i++)
> printf("%c%s", hex2str(vA), (i==n)? "]"ORS:"")
> next
> }
> /^(Errors|Full|Test)=/{
> printf("%s",$1 "[")
> n=split($2, vA,OFS)
> for(i=1;i<=n;i++)
> printf("%d%s", hex2dec(vA), (i==n)? "]"ORS:"")
> }' sample.log >temp.log
> 
> ```

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [March 31, 2011, 3:03pm UTC](https://community.unix.com/t/converting-hex-to-ascii-decimal/285559/15 "2011-03-31T15:03:26Z")

</div>

Hm....... Strange works fine here (Solaris).  
Try changing

```nohighlight
n=split($2, vA,OFS)

```

to

```nohighlight
n=split($2, vA," ")

```

BTW, what OS are you on?

---

<div class="post-metadata">

**Author:** ![Shiftkey](https://community.unix.com/letter_avatar/shiftkey/32/5_5575768a8748004e209b776fc1b2916d.png) [@Shiftkey](https://community.unix.com/u/Shiftkey)\
**Post date:** [March 31, 2011, 3:11pm UTC](https://community.unix.com/t/converting-hex-to-ascii-decimal/285559/16 "2011-03-31T15:11:35Z")

</div>

tried some tr stuff again and got it to work this time

used

```nohighlight
tr "\000" " " < inputFile > outputFile

```

Thank you so much for the help and patience!

---------- Post updated at 03:11 PM ---------- Previous update was at 03:06 PM ----------

Actually should of done this first time, just deleted the null character rather than replace it

```nohighlight
tr -d "\000" <input > output

```

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [March 31, 2011, 3:15pm UTC](https://community.unix.com/t/converting-hex-to-ascii-decimal/285559/17 "2011-03-31T15:15:30Z")

</div>

Hmmm... strange printf implementation of your awk...

Change

```nohighlight
/^(Hardware|SW Version)=/ {
  printf("%s",$1 "[")
  n=split($2, vA,OFS)
  for(i=1;i<=n;i++)
     printf("%c%s", hex2str(vA), (i==n)? "]"ORS:"")
  next
}

```

to this:

```nohighlight
/^(Hardware|SW Version)=/ {
  printf("%s",$1 "[")
  n=split($2, vA,OFS)
  for(i=1;i<=n;i++)
     printf("%c", hex2str(vA))
  print "]" ORS
  next
}

```
