| Ones Complement Script
Go To Script Pool |
|
| # Ones-complement add a list of 16 bit numbers $sum = 0; while($theline = <>) { if($theline =~ /^#/) { # Do nothing - comment } else { chop($theline); $sum = $sum + &ReadHex($theline); if($sum > 65535) { $sum = $sum - 65536; $sum = $sum + 1; } printf("%s\tsum=%s\n",$theline,&WriteHex($sum)); } } sub ReadHex { local($theline) = @_; # Chop off any leading 0x indicator $theline =~ s/0x//; # Convert to lowercase $theline =~ tr/A-Z/a-z/; @digits = split(//,$theline); $value = 0; for($i=0; $i<=$#digits; $i=$i+1) { $value = $value * 16 + HexDigitToInt($digits[$i]); } return $value; } sub WriteHex { local($val) = @_; $string = ''; while($val > 0) { $rem = $val % 16; $string = IntToHexDigit($rem) . $string; $val = ($val - $rem) / 16; } return $string; } sub HexDigitToInt { local($digit) = @_; if($digit =~ /[0-9]/) { return $digit; } elsif ($digit eq 'a') { return 10; } elsif ($digit eq 'b') { return 11; } elsif ($digit eq 'c') { return 12; } elsif ($digit eq 'd') { return 13; } elsif ($digit eq 'e') { return 14; } elsif ($digit eq 'f') { return 15; } else { printf("Error - non-hex digit in string: %s\n",$digit); } } sub IntToHexDigit { local($int) = @_; if($int < 10) { return $int; } elsif ($int == 10) { return 'a'; } elsif ($int == 11) { return 'b'; } elsif ($int == 12) { return 'c'; } elsif ($int == 13) { return 'd'; } elsif ($int == 14) { return 'e'; } elsif ($int == 15) { return 'f'; } else { printf("Error - non-hex digit in integer: %d\n",$int); } } |
Other Scripts: Batch
Simulation
Products: Undertow Suite
|
| Company
Products News
Assistance Download Contact Us Sales Veritools, Inc. |