I'm trying to write the below C# code in perl, what is the C# equivalent for Convert.ToByte(key.Text.Substring(i * 2, 2), 16); in perl.
Any insight is much appreciated.
Thanks.

C# code
// convert key to a byte array int len = key.Text.Length / 2; if (len == 0) throw new ApplicationException("key must be provid +ed"); byte[] k = new byte[len]; for (int i = 0; i < len; i++) { k[i] = Convert.ToByte(key.Text.Substring(i * 2, 2) +, 16); } summaryText += "key=" + key.Text.Trim() + "\r\n\r\n";
Perl code I got so far # convert key to a byte array
$key="AJDAHSDJ213718327DSFSDJFHSDKFJ87348171837"; #example $len=length($key) / 2; if($len == 0) { print"Key must be provided";} else { for($i=0;$i<$len;$i++) { $k[$i]= ? }

Also how do you pass the byte array as a key value to sha256 hash, Join the array values ? $key_value=@k; $digest=hmac_sha256_base64($data,$key_value);


In reply to String to Byte array by kpaxian25

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.