Path: utzoo!censor!geac!torsqnt!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!elroy.jpl.nasa.gov!sdd.hp.com!hplabs!pyramid!mfrost From: mfrost@sword (Mark Frost) Newsgroups: comp.lang.perl Subject: Why are my digits disappearing? Message-ID: <136559@pyramid.pyramid.com> Date: 4 Dec 90 22:29:03 GMT Sender: daemon@pyramid.pyramid.com Reply-To: mfrost@pyramid.com (Mark Frost) Distribution: na Organization: Pyramid Technology Corp., Mountain View, CA Lines: 106 Thanks to all those who replied to my previous problem involving pattern matching (yes, I did pick up the fact that I should have been using =! instead of =~ -- oops! 8-) ). Anyway, I've got another problem that has really been puzzling me. As part of a larger script I am writing which deals extensively with IP addresses, I am trying to convert decimal IP addresses to hex format. So 129.214.009.096 would give me "81D60960". I clipped the code below from my other script to illustrate the problem. Each time I run the subroutine to convert decimal IP address to hex ("IPTOHEX") the routine seems to drop some digits semi-consistently. So sometimes I might get "81600" instead of "81D6A0A0" for 129.214.160.160. I can't see any reason for this. Being a perl novice, most of the constructs below are quite new to me, but I don't see why they don't work. I have included sample output below and the script itself. Thanks -m----------- Mark Frost (mfrost@pyramid.com) ---mmm--------- System Administrator - Hardware Engineering -----mmmmm------- Pyramid Technology Corporation -------mmmmmmm----- 1295 Charleston Rd, P.O. Box 7295 ---------mmmmmmmmm--- Mountain View, California 94039-7295 -----------mmmmmmmmmmm- (415) 335-8163 ----------------------------script-------------------------------- #!/usr/bin/perl # Convert decimal IP address to Hex form sub IPTOHEX { local(@ipaddr) = @_; local($hexaddr,$loop); local($digit1,$digit2) = 0; for ($loop = 0; $loop < 4; $loop++) { $digit1 = (0 .. 9, 'A' .. 'F')[( $ipaddr[$loop] / 16) & 15]; $digit2 = (0 .. 9, 'A' .. 'F')[( $ipaddr[$loop] % 16) & 15]; $hexaddr = join('',$hexaddr,$digit1,$digit2); print "ipaddr[$loop] = $ipaddr[$loop] "; print "... digit1 = $digit1 ... digit2 = $digit2 "; print "... hexaddr = $hexaddr\n"; } # for $hexaddr; } # iptohex for ( $i = 0; $i < 5; $i++) { print "\nPlease enter the hostname which you want to configure (XX.XX.XX.XX)\n"; print "-> "; $ipraw=; chop($ipraw); @ip=split(/\./,$ipraw); $hexstring = &IPTOHEX(@ip); print "\nHost ip entered was $ip[0].$ip[1].$ip[2].$ip[3] ($hexstring)\n"; } ------------------program output------------------------ Please enter the hostname which you want to configure (XX.XX.XX.XX) -> 129.214.160.160 ipaddr[0] = 129 ... digit1 = 8 ... digit2 = 1 ... hexaddr = 81 ipaddr[1] = 214 ... digit1 = D ... digit2 = 6 ... hexaddr = 81D6 ipaddr[2] = 160 ... digit1 = A ... digit2 = 0 ... hexaddr = 81D6A0 ipaddr[3] = 160 ... digit1 = ... digit2 = 0 ... hexaddr = 81D6A00 Host ip entered was 129.214.160.160 (81D6A00) Please enter the hostname which you want to configure (XX.XX.XX.XX) -> 129.214.160.160 ipaddr[0] = 129 ... digit1 = 8 ... digit2 = 1 ... hexaddr = 81 ipaddr[1] = 214 ... digit1 = ... digit2 = 6 ... hexaddr = 816 ipaddr[2] = 160 ... digit1 = ... digit2 = 0 ... hexaddr = 8160 ipaddr[3] = 160 ... digit1 = ... digit2 = 0 ... hexaddr = 81600 Host ip entered was 129.214.160.160 (81600) Please enter the hostname which you want to configure (XX.XX.XX.XX) -> 129.214.160.160 ipaddr[0] = 129 ... digit1 = 8 ... digit2 = 1 ... hexaddr = 81 ipaddr[1] = 214 ... digit1 = ... digit2 = 6 ... hexaddr = 816 ipaddr[2] = 160 ... digit1 = ... digit2 = 0 ... hexaddr = 8160 ipaddr[3] = 160 ... digit1 = ... digit2 = 0 ... hexaddr = 81600 Host ip entered was 129.214.160.160 (81600) Please enter the hostname which you want to configure (XX.XX.XX.XX) -> 129.214.009.096 ipaddr[0] = 129 ... digit1 = 8 ... digit2 = 1 ... hexaddr = 81 ipaddr[1] = 214 ... digit1 = ... digit2 = 6 ... hexaddr = 816 ipaddr[2] = 009 ... digit1 = 0 ... digit2 = 9 ... hexaddr = 81609 ipaddr[3] = 096 ... digit1 = 6 ... digit2 = 0 ... hexaddr = 8160960 Host ip entered was 129.214.009.096 (8160960) Please enter the hostname which you want to configure (XX.XX.XX.XX) -> 129.214.009.096 ipaddr[0] = 129 ... digit1 = 8 ... digit2 = 1 ... hexaddr = 81 ipaddr[1] = 214 ... digit1 = ... digit2 = 6 ... hexaddr = 816 ipaddr[2] = 009 ... digit1 = 0 ... digit2 = 9 ... hexaddr = 81609 ipaddr[3] = 096 ... digit1 = 6 ... digit2 = 0 ... hexaddr = 8160960 Host ip entered was 129.214.009.096 (8160960) Brought to you by Super Global Mega Corp .com