Decoding URI query string in Java

Use URLDecoder.decode(proxyRequestParam.replace(“+”, “%2B”), “UTF-8”) .replace(“%2B”, “+”) to simulate decodeURIComponent. Java’s URLDecoder decodes the plus sign to a space, which is not what you want, therefore you need the replace statements. Warning: the .replace(“%2B”, “+”) at the end will corrupt your data if the original (pre-x-www-form-urlencoded) contained that string, as @xehpuk pointed out.

NodeJS: How to decode base64 encoded string back to binary? [duplicate]

As of Node.js v6.0.0 using the constructor method has been deprecated and the following method should instead be used to construct a new buffer from a base64 encoded string: var b64string = /* whatever */; var buf = Buffer.from(b64string, ‘base64’); // Ta-da For Node.js v5.11.1 and below Construct a new Buffer and pass ‘base64’ as … Read more

Why does base64 encoding require padding if the input length is not divisible by 3?

Your conclusion that padding is unnecessary is right. It’s always possible to determine the length of the input unambiguously from the length of the encoded sequence. However, padding is useful in situations where base64 encoded strings are concatenated in such a way that the lengths of the individual sequences are lost, as might happen, for … Read more

How can I send and receive WebSocket messages on the server side?

Note: This is some explanation and pseudocode as to how to implement a very trivial server that can handle incoming and outcoming WebSocket messages as per the definitive framing format. It does not include the handshaking process. Furthermore, this answer has been made for educational purposes; it is not a full-featured implementation. Specification (RFC 6455) … Read more

How to decode Unicode escape sequences like “\u00ed” to proper UTF-8 encoded characters?

Try this: $str = preg_replace_callback(‘/\\\\u([0-9a-fA-F]{4})/’, function ($match) { return mb_convert_encoding(pack(‘H*’, $match[1]), ‘UTF-8’, ‘UCS-2BE’); }, $str); In case it’s UTF-16 based C/C++/Java/Json-style: $str = preg_replace_callback(‘/\\\\u([0-9a-fA-F]{4})/’, function ($match) { return mb_convert_encoding(pack(‘H*’, $match[1]), ‘UTF-8’, ‘UTF-16BE’); }, $str);