1

I am troubleing in coverting hex to decimal from txt file using awk.

I want to do like this

awk -F' '   '{   system("echo '$((16#"$1"))'") '} $file_name

but not work... then I try other code

awk -F' ' -v var="\"echo \x27\$((16#" '{var=var$1"))\x27\"" system(var) }' $file_name

also not work. but print var then enter image description here

what should i do?

10
  • 1
    Please include content as text, not behind image links. See Why not upload images of code on SO when asking a question? -- the reasoning regarding searchability, accessibility, access to folks behind corporate firewalls, bandwidth expenditures, &c. apply for errors as well. Commented Jan 11, 2018 at 4:44
  • 1
    Also, frankly, using the system() command in awk is a code smell -- you're much better off not using awk at all if you'd be pushing all the work from awk back to a shell. system() is not just inefficient, but its use runs major security risks. Commented Jan 11, 2018 at 4:45
  • @Charless Duffy sorry to upload image links, next time I must upload code, then not using awk, not using system() , As result, What can I use it? Commented Jan 11, 2018 at 5:21
  • either awk only, or bash only; John's answer covers both. system() is dangerous because everything it passes is parsed as code, so malicious data can run arbitrary commands; you don't have that problem when handling data in bash unless eval or equivalents are used, nor do you have it in native awk. Commented Jan 11, 2018 at 5:25
  • @Charless Duffy ok, I understand, then I want script code in either awk only or bash only, I don't know using only one way either awk only or bash only Commented Jan 11, 2018 at 5:34

1 Answer 1

3

Using awk

To use GNU awk to convert hex to decimal:

$ echo '0xFFFFFFFE' | awk -n '{printf "%i\n",$1}'
4294967294

Or:

$ x='0xFFFFFFFE'
$ awk -n -v x="$x"  'BEGIN{printf "%i\n",x}'
4294967294

Or:

$ x='0xFFFFFFFE'; awk -v x="$x"  'BEGIN{print strtonum(x)}'
4294967294

To convert hex to decimal using bash:

$ echo $((0xFFFFFFFE))
4294967294

Limitations:

1. GNU awk is limited to 52-bit integers.

2. The above could be extended to perform two's-complement arithmetic but it hasn't.

To avoid both these limitations, see the python solution below:

Using python

Awk does not handle long integers. For long integers, consider this python script:

$ cat n.py
#!/usr/bin/python3
import sys
def h(x):
        x = int(x, 16)
        return x if x < 2**63 else x - 2**64

for line in sys.stdin:
        print(*[h(x) for x in line.split()])

Let's use this input file:

$ cat file
FFFFFFFFFFFFFFFF EFEFEFEFEFEFEFEF

When we run our script, we find:

$ python3 n.py <file
-1 -1157442765409226769
Sign up to request clarification or add additional context in comments.

16 Comments

my file's contens are FFFFFFFFFFFFFFFF EFEFEFEFEFEFEFEF and I want to read and convert hex to long type(int64)!
@bestn Are those two numbers on one line or on two lines?
2 colums per 1 row and I think I must use echo like "echo $((16#FF))", don't it?
Then do the same thing using "$2" to handle the second field.
@John1024 AMAZING~! Thank you! god python!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.