Hi guys,
I have to check some data in the Labview programming for the RS 232 interface. >In that protocol I must build the checksum of the data to be sent.
For example, for the given data:
03, 58, 43 and 30:
I have to take from left to right two data (03 and 58) and build the xor
of them as shown below
0000 0011
0101 1000
========
0101 1011
Or
03 xor 58 5B
This result should be taken with the next data (43) to build the Xor and
the result of them should be xored with the last data (30) as shown
below:
0101 1011 5B
0100 0011 43
===============
0001 1000 18
0001 1000 18
0011 0000 30
===============
0010 1000 28
So, to check this with AWK, I've written this one liner, but this seems
not to work:
awk 'BEGIN {a = ARGV[1]; DELETE ARGV[1]; a = ARGV[2]; DELETE ARGV[2];
print (xor(a, b))}' 1011 1100
1100
Instead of giving me
0101 1011 = 5B
It gives only
1100 = C
Why? Where I'm doing something wrong?
Hi guys,
I have to check some data in the Labview programming for the RS 232 interface.
In that protocol I must build the checksum of the data to be sent.
For example, for the given data:
03, 58, 43 and 30:
I have to take from left to right two data (03 and 58) and build the xor of them as shown below
0000 0011
0101 1000
========
0101 1011
Or
03 xor 58 5B
This result should be taken with the next data (43) to build the Xor and the result of them should be xored with the last data (30) as shown below:
0101 1011 5B
0100 0011 43
===============
0001 1000 18
0001 1000 18
0011 0000 30
===============
0010 1000 28
So, to check this with AWK, I've written this one liner, but this seems not to work:
awk 'BEGIN {a = ARGV[1]; DELETE ARGV[1]; a = ARGV[2]; DELETE ARGV[2]; print (xor(a, b))}' 1011 1100
1100
Instead of giving me
0101 1011 = 5B
It gives only
1100 = C
Why? Where I'm doing something wrong?
Probably in many places. First, please tell us what awk version you are using that supports binary operations; is that GNU awk? Does this awk support binary string literals (like 1011)? (You seem to xor the binary representation of the *decimal* numbers 1011 and 1100 here.) Does thisHi Janis,
awk support DELETE with all caps? (Using 'delete' is anyway unnecessary here.) Then the question whether overwriting 'a' (a second time) is intentional? (b=... is probably meant.)
PS: In what environment are you working? Doing that (here [OT]) in a
shell, ksh, might be simpler that using awk for binary base numbers.
Janis
gawk --versionGNU Awk 5.1.1, API: 3.1 (GNU MPFR 4.1.0, GNU MP 6.2.1)
@ Mack the Knife
Thanks a lot for your correction of the code.
It works well.
I haven't used awk for a long tine and forgot to convert the read argument into a number.
Stupid mistake
Regards Mohsen
And it works with binary literals as you requested in your sample?
Can you show us the GNU awk program that works with such binary
arguments, please?
CheckSum = xor(CheckSum, strtonum(Data[N]))
But somehow, I cannot get it managed.
It prints something else than I described above.
I haven't written for long time AWK scripts more.
Do you have any idea?
Regards
Mohsen
So, to check this with AWK, I've written this one liner, but this seems not to work:[...]
awk 'BEGIN {a = ARGV[1]; DELETE ARGV[1]; a = ARGV[2]; DELETE ARGV[2]; print (xor(a, b))}' 1011 1100
1100
Mohsen Owzar <mohsen.owzar@gmail.com> writes:
[...]
So, to check this with AWK, I've written this one liner, but this seems not to work:[...]
awk 'BEGIN {a = ARGV[1]; DELETE ARGV[1]; a = ARGV[2]; DELETE ARGV[2]; print (xor(a, b))}' 1011 1100
1100
Awk is case-sensitive. There's nothing called "DELETE". Apparently
"DELETE ARGV[1]" does nothing (I haven't taken the time to figure out
why it doesn't produce an error message). The behavior is the same
when I replace "DELETE" by a random identifier.
Now to calculate all the checksums of the sent data, I have put two set of data
as an example in a file "data.txt" as below: >--------------------------------------------
Note: I didn't bother to actually verify the numerical accuracy of the results, because I don't really understand the underlying goal of the exercise.Hi all,
if you wanna avoid have to call strtonum( ) all the time, use the -n flag at startup :Thanks a lot
gawk -n -b -e '$++NF = sprintf("0x %.8X", xor( +$1 , +$2 ))' OFS='\n' <<< ' 0x88776655 0x311DD993 '
0x 88776655
0x 311DD993
0x B96ABFC6
jason....@gmail.com schrieb am Mittwoch, 27. Juli 2022 um 13:14:57 UTC+2:
if you wanna avoid have to call strtonum( ) all the time, use the -n flag at startup :Thanks a lot
gawk -n -b -e '$++NF = sprintf("0x %.8X", xor( +$1 , +$2 ))' OFS='\n' <<< ' >0x88776655 0x311DD993 '
0x 88776655
0x 311DD993
0x B96ABFC6
Best regards
Mohsen
implemented, and then got abandoned/deprecated by the developers.0xABCDEF1234567
The fact that it only works on input data, not on strings contained in the program, is kind of a deal-breaker for me.
Note that this depends on the bash-ism of <<<
(I don't know [or care] if any other shells implement it)
Note that this depends on the bash-ism of <<<
(I don't know [or care] if any other shells implement it)
I use that only cuz i'm too lazy to be thumping the POSIX-Bible. Feel
free to change that to command line variable assignment or echo or
printf or whatever you think would make Richard Stallman happy.
Note that this depends on the bash-ism of <<< (I don't know [or
care] if any other shells implement it)
I use that only cuz i'm too lazy to be thumping the POSIX-Bible. Feel
free to change that to command line variable assignment or echo or
printf or whatever you think would make Richard Stallman happy.
Sysop: | DaiTengu |
---|---|
Location: | Appleton, WI |
Users: | 793 |
Nodes: | 10 (1 / 9) |
Uptime: | 39:56:39 |
Calls: | 11,106 |
Calls today: | 3 |
Files: | 186,086 |
Messages: | 1,751,478 |