Hi Monks,

I'm trying to send an octal string to a receiving host.  The code example below shows the original ASCII values (in hex) of the command.  My script then loops over each two byte pair and converts the ASCII (hex values) to octal ready for sending to the host (and I agree that part is probably a bit long-winded).

Anyway, the problem is that if I try to send a simple string of octal digits (see $goodoct) it works fine, however the string returned from the sub convert is a literal string of characters and not interpreted as actual octal values.  I can tell this to be the case even without looking at the host side as the printed output (in Windows) for $octcommand shows "\2\61\70" etc whereas the output for $goodoct shows actual ASCII chars.

Its obviously a simple mistake somewhere on my part, but I'm having trouble seeing what the difference between the variable contents of $goodoct and $octcommd are.  All help appreciated!
#!/usr/bin/perl # $hexcommand="02313832300030010000c000003030353130303038313032343034343 +83236383331343135353430303030303030303030343135353430303003"; convert($hexcommand); print "DERRIVED OCTAL\n$octcommand\n\n"; $goodoct="\2\61\70\62\60\0\60\1\0\0\300\0\0\60\60\65\61\60\60\60\70\61 +\60\62\64\60\64\64\70\62\66\70\63\61\65\65\64\60\60\60\60\60\60\60\60 +\60\60\64\61\65\65\64\60\60\60\3"; print "GOOD OCTAL\n$goodoct\n\n"; sub convert { $hexcommand=shift; $length = length($hexcommand); $tot = ($length/2)-1; print "length is: $length\n"; print "tot is: $tot\n"; for ($x=0; $x<=$tot; $x++){ $chunk = substr($hexcommand, $offset, 2); $dec = &hex2dec($chunk); $octbyte=sprintf "%lo", $dec; $octcommand = $octcommand . "\\" . $octbyte; $offset+=2; } return $octcommand; } sub hex2dec($) { return hex $_[0] }

In reply to Returning variable from sub by cstrong

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.