Any byte swapping in awk? 
Author Message
 Any byte swapping in awk?

I've got a file to read which has byte-swapped hex digits like 'D715'.

I need to evaluate this as '15D7' and convert to decimal 5591.

Any ideas?

----------------------------------------------
Jim Drummey
Drum  Engineering



Sun, 27 Feb 2000 03:00:00 GMT  
 Any byte swapping in awk?


Quote:

> I've got a file to read which has byte-swapped hex digits like 'D715'.

> I need to evaluate this as '15D7' and convert to decimal 5591.

> Any ideas?

For the byte-swapping:

sed 's/\([0-9a-f]\{2\}\)\([0-9a-f]\{2\}\)/\2\1/'

(or, if your sed doesn't support repetition counts:)

sed 's/\([0-9a-f][0-9a-f]\)\([0-9a-f][0-9a-f]\)/\2\1/'

you can combine this with bc for conversion.  (ibase=16)

(echo 'ibase=16';sed 's/\([0-9A-F]\{2\}\)\([0-9A-F]\{2\}\)/\2\1/')|bc

or (my apologies for a Perl solution in this newsgroup)

perl -pe 's/([0-9a-f]{2})([0-9a-f]{2})/$2$1/i'

For conversion, Perl has hex().  A snippet solving the stated problem is
then:

perl -ple 's/([0-9a-f]{2})([0-9a-f]{2})/$2$1/i;$_=hex'

This cannot be done with regex search-and-replace in awk, since awk
doesn't have backreferences.  You can probably do it with substr.

Quote:
> Jim Drummey

--



Mon, 28 Feb 2000 03:00:00 GMT  
 Any byte swapping in awk?

Quote:

> I've got a file to read which has byte-swapped hex digits like 'D715'.

> I need to evaluate this as '15D7' and convert to decimal 5591.

> Any ideas?

presuming you have "D715" in field 2,
hexed = substr(3, 2, $2) substr(1, 2, $2);
plain = hex2dec(hexed);

where hex2dec is a function I picked up on this newsgroup a while ago
from jim monty and have been using ever since:
# Compliments of Jim Monty, from a post to comp.lang.awk:
function hex2dec(x,    h, n, l, i) {
    h = "0123456789ABCDEF..........abcdef"
    for (i = l = length(x); i > 0; i--)
        n += (index(h, substr(x, i, 1)) - 1) % 16 * 16 ^ (l - i)
    return n

Quote:
}

going the other way is, of course, easy with sprintf.

andru



Mon, 28 Feb 2000 03:00:00 GMT  
 Any byte swapping in awk?

: I've got a file to read which has byte-swapped hex digits like 'D715'.

IIRC, the latest gawk has this undocumented, I have not completed
looking at it though.

: I need to evaluate this as '15D7' and convert to decimal 5591.

: Any ideas?

: ----------------------------------------------
: Jim Drummey
: Drum  Engineering

--
Ian Stirling.   Designing a linux PDA, see  http://www.mauve.demon.co.uk/
-----******* If replying by email, check notices in header *******-------
The fight between good and evil, an epic battle. Darth vader and Luke,
suddenly in the middle of the fight, Darth pulls Luke to him, and whispers
"I know what you'r getting for christmas!" Luke exclaims "But how ??!?"
"It's true Luke, I know what you'r getting for christmas" Luke tries to ignore
this, but wrenches himself free, yelling "How could you know this?",
Vader replies "I felt your presents"



Tue, 29 Feb 2000 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. swapping bytes on a two byte integer

2. Swapping bytes

3. Network Byte-Swap

4. Swapping byte order in blocks of words

5. byte swapping

6. byte swapping with NAG f95 compiler

7. ? Byte swapping

8. Byte swapping sequential access files on input

9. byte swapping

10. Swap byte (big->little endian) with ifc

11. byte-swap - IEEE-754 question/clarification

12. Byte swapping in F90

 

 
Powered by phpBB® Forum Software