in reply to Re^2: How to create a DNS message in perl?
in thread How to create a DNS message in perl?

But how can can i convert $QTYPE=23 to the hexa value of 2 octets which is '0023'

Wrong question.

You need to learn how to store 2316 into $QTYPE. Right now, you are storing 2310, a completely different number.

$QTYPE = 0x23; # 35

If you're starting with a string, you can use hex or oct.

$QTYPE = hex('23'); # 35 $QTYPE = oct('0x23'); # 35

So is there any function in perl which directly convert the domain name into the required hexa format

There's no builtin function. I've specified the format (from which a solution is easily derived using split, length, and pack 'C') in an earlier post. Furthermore, someone gave you the name of a module that supposedly does the work for you.

Replies are listed 'Best First'.
Re^4: How to create a DNS message in perl?
by sanjay nayak (Sexton) on Jul 30, 2008 at 12:28 UTC

    Hi
    Thanks for the reply.Acording to your suggestion i have written the code as follows.
    my $QNAME= "www.google.com"; my ($QNAME1, $QNAME2,$QNAME3)= split(/\./,$QNAME); my ($len1,$len2, $len3)=(length($QNAME1),length($QNAME2),length($QNAME +3));

    Then how to use the pack(('C',0)or "\x00") for in this case.So that it deocded the value as '0377777706676F6F676C6503636F6D00'.

    Plz suggest.

    Regd's
    Sanjay

      That won't work for www.some.domain.com. Use a loop.

      my $QNAME= "www.google.com"; my $QNAME_packed = ''; for my $part ( split( /\./, $QNAME ) ) { $QNAME_packed .= pack( 'C', length($part) ) . $part; } $QNAME_packed .= pack('C', 0)

      Or

      my $QNAME= "www.google.com"; my $QNAME_packed = ''; for my $part ( split( /\./, $QNAME ) ) { $QNAME_packed .= pack( 'C/a*', $part ); } $QNAME_packed .= pack('C', 0)

      Or

      my $QNAME= "www.google.com"; my $QNAME_packed = ''; for my $part ( split( /\./, $QNAME ), '' ) { $QNAME_packed .= pack( 'C/a*', $part ); }

      Or if requiring Perl 5.8 is acceptable

      my $QNAME= "www.google.com"; my $QNAME_packed = pack( '(C/a*)*', split( /\./, $QNAME ), '' ) );

        Hi

        Thanks a lot for your help. Now my problem was solved.

        Regd's
        Sanjay