kpaxian25 has asked for the wisdom of the Perl Monks concerning the following question:

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);

Replies are listed 'Best First'.
Re: String to Byte array
by BrowserUk (Patriarch) on Sep 19, 2013 at 16:42 UTC

    That usage of Convert.ToByte( string, 16 ) converts a 2-digit hex string to an unsigned 8-bit integer.

    The direct equivalent in Perl would be unpack 'H2', substr $key, $i*2, 2;.

    But then you'll need to convert that integer into a character and pack it back into a string.

    But, you have a bigger problem if your example "AJDAHSDJ213718327DSFSDJFHSDKFJ87348171837" is obtained from somewhere rather than just made up? J, K, H & S are not hex digits.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      J, K, H & S are not hex digits.
      Maybe they're kpaxadecimal :) </lame>
        :)
      Yes that was just made up text. Didn't realize about the J,K,H&S.
Re: String to Byte array
by hdb (Monsignor) on Sep 19, 2013 at 19:03 UTC

    Assuming the key contains valid 2digit hex numbers:

    use strict; use warnings; my $key = "A21B12C212AD12FF"; my @key = map hex, $key =~ /../g; print "@key\n";

    HELP: How to do this with unpack? All my attempts are failing...

      What the OP probably needs, rather than an array of integers, is a binary string of unsigned chars derived from the hex string input.

      That is done using pack, not unpack:

      print pack 'H*', "A21B12C212AD12FF";; ó&#8592;&#8597;&#9516;&#8597;¡&#8597; 

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Thanks that helped me to get the same output as C# code. Perl Rules :)
Re: String to Byte array
by Tux (Canon) on Sep 19, 2013 at 16:34 UTC

    I'd suggest using unpack.


    Enjoy, Have FUN! H.Merijn
      Using UTF8 conversion byte[] bArray = Encoding.UTF8.GetBytes (inputString); Using ASCII conversion byte[] bArray = Encoding.ASCII.GetBytes (inputString); Full Source: http://net-informations.com/q/faq/stringtobyte.html Eric