4

I have a file name x

$ cat x
1A34532112345

I wants to print in hex using awk I written a script (with help from web)

fold -1 /home/cscape/Desktop/x | gawk '{ printf("%s , %X\n",$0, int($0) )}' 

But output for each char is 0

$ fold -1 /home/cscape/Desktop/x | gawk '{ printf("%s , %X\n",$0, int($0) )}'
1 , 1
A , 0
3 , 3
4 , 4
5 , 5
3 , 3
2 , 2
1 , 1
1 , 1
2 , 2
3 , 3
4 , 4
5 , 5

Why 0 for A. even with %d. I wants to print ASCII value of A as HEX.

1
  • Not sure what you are trying to accomplish, but it'll probably work if you change the file contents to contain a single character per line. If that gives the output you are looking for, then we can help you change the awk script. Also what's wrong with hexdump -C?
    – jippie
    Dec 15, 2012 at 9:29

3 Answers 3

3

If you don't have to use awk, you might look at od ("octal dump"):

$ echo 1A34532112345 | od -t x1
0000000 31 41 33 34 35 33 32 31 31 32 33 34 35 0a
0000016
0
2

awk will silently convert strings to numbers. Which number? The number you get by taking the initial decimal digits of the string (this may be just "", if the string is empty or doesn't begin with any decimal digits, as is the case with "A") and converting them to a number. "" gets converted to 0. So all of these would be converted to 0:

  • "0"
  • "0text"
  • "text"
  • ""

awk only converts strings to numbers in this way when a number is needed. In "0text" + 1, a number is needed, so the result will be 1. In just plain "0text", a number isn't needed, so no conversion takes place. In printf("%d", "0text"), a number again is needed, so the string will be converted to a number.

What you are looking for is an ord function, which isn't a native function in awk. The gawk documentation describes how to write such a function in awk.

1
  • Thanks ..helpful to get my work done and my concept is now clear. Dec 15, 2012 at 10:24
1

Know this is super old but popped up top of Google results, so in case you've found your way here as well...

The value being passed to printf() is "A" and not a hex value nor a form of string that can be converted using int($0). Type casting isn't quite the same in awk or other languages of its era, as others already pointed out.

    $ awk 'BEGIN{printf("%s, %X, %d\n", "A", "A", "A")}'
    A, 0, 0

For the record, something modern like Python is no different if you aren't using the syntax it expects:

    >>> int("0xA")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: invalid literal for int() with base 10: '0xA'
    
    >>> int("0xA", 16)
    10

There is a simple solution for awk though. Just add a leading "0x" to "cast" the string to a hex value prior to printing with printf with the appropriate "%X". The latter is no different than passing base=16 to the int() function within python [a trick that awk's int() does not support].

    $ awk 'BEGIN{printf("%s, %X, %d\n", "A", "0xA", "0xA")}'
    A, A, 10

Yes, it strips the 0x from the output. If in doubt, double-check expectations by converting an int to a hex string using sprintf and then see how that value prints without using printf. The x+1 produces a "B" proves that awk is treating x as a number and that sprintf is converting to hex but simply not including the leading "0x". I mean how annoying would that be if building up say a 4 byte value two bytes at a time.

    $ awk 'BEGIN{x=10; y=sprintf("%X", x); print(y)}'
    A
    
    $ awk 'BEGIN{x=10; y=sprintf("%X", x+1); print(y)}'
    B 

Besides you can always add them in when you need them.

    $ awk 'BEGIN{x=10; y=sprintf("%X", x+1); printf("x = %d, x+1 = 0x%02X, x^3 = 0x%04X\n", x, x+1, x^3)}'
    x = 10, x+1 = 0x0B, x^3 = 0x03E8

Which of course is equally true in python as well

    >>> print("%04X 0x%04X\n" % (1000, 1000))
    03E8 0x03E8

Simply adding "0x" in awk is way simpler than the python way at any rate

    >>> hex(int("A", 16))
    '0xa'

Having said all of that, the original question was a bit odd given the original file already contained a hex value that didn't need to be converted...unless there was some unspoken greater need for re-formatting that the op didn't mention.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .