// title: Decode %hex encoded characters // author: jamshark70 // description: // A quick utility function for decoding UTF-8 characters in %hex codes (such as for web URLs). Save the code into a .sc file in your Extensions/ directory, recompile, and then: // // "%E5%A4%A7%E5%AE%B6%E5%A5%BD".decodeHex; // -> 大家好 // // This probably works only for UTF-8. SC can save the individual bytes of Unicode characters into String objects, and the IDE will print UTF-8 code points correctly, but there is no way to switch the IDE to other Unicode variants. // code: + String { decodeHex { var new = String.new, stream = CollStream(this), ch, int; while { ch = stream.next; ch.notNil } { if(ch == $%) { ch = stream.next; if(ch.notNil) { int = ch.digit << 4; } { // If the string ends with %, then 'ch' after that is nil // so we can just append the % ^new.add($%); }; ch = stream.next; if(ch.notNil) { ch = (int + ch.digit).asAscii; } { // If we got this far, we know there was one (only one) char after % // so add them both ^new.add($%).add(this.last); }; }; new = new.add(ch); }; ^new } }