Converting PHP function to C# -
i've finished converting php script c# 1 use in asp.net.
i've converted match the correct result far:-
function sign_url($url, $key, $secret) { if (strpos($url,'?') !== false) { $url .= "&"; } else { $url .= "?"; } $url .= "applicationkey=" . $key; $signature = hash_hmac("sha1", urldecode($url), $secret); $url .= "&signature=" . hex_to_base64($signature); return $url; }
here part i'm struggling with:-
function hex_to_base64($hex){ $return = ''; foreach(str_split($hex, 2) $pair){ $return .= chr(hexdec($pair)); } return base64_encode($return); }
from can gather splits input string ($hex) sets of 2 characters.
converts these hexadecimal strings decimal numbers before getting chr() value of number.
storing these conversions in $return string.
base 64 conversion.
i'm struggling find best way in c#, particularly splitting 2 characters while doing other conversions.
old, good, c-style code not use linq , megabytes of other libraries:
static string hextobase64(string hex) { byte[] buf = new byte[hex.length / 2]; (int = 0; < hex.length / 2; i++) { buf[i] = convert.tobyte(hex.substring(i*2,2), 16); } return convert.tobase64string(buf); }
Comments
Post a Comment