Skip to content

Instantly share code, notes, and snippets.

@subzey
Forked from bytespider/LICENSE.txt
Created June 5, 2011 08:07
Show Gist options
  • Save subzey/1008764 to your computer and use it in GitHub Desktop.
Save subzey/1008764 to your computer and use it in GitHub Desktop.
140byt.es -- encodeURIComponent based convert string to array of UTF-8 bytes
function(
s, // string to be onverted into bytes
a // placeholder
){
a=[]; // a is array, we'll return it
// %-escape the byte sequence. All chars that are no escaped are basic ASCII
encodeURIComponent(s)
// traversing the string
.replace(
// capture %{char}{char} ({char} can be presumably 0..F) storing these two chars
// or just one any char
/%(..)|./g,
// and passing to function
function(
c, // full captured string
p // two chars in parens if those were captured
){
// push into array
a.push(
// if p is not "00" or is undefined (not captured)
p != 0?
// try to treat p as a hex value
// if it succeded it will return anyting-above-zero (zero bytes are bypassed by p!=0)
parseInt(p, 16) ||
// if not it will return NaN and it means that captured string was not escaped
// getting charCode
c.charCodeAt()
:
// special case: parens were captured but it parseInt would return zero
0
)
}
);
// after doing that with all bot escaped and not escaped chars
// the a array is filled with byte codes. Return it
return a
}
function(s,a){a=[];encodeURIComponent(s).replace(/%(..)|./g,function(c,p){a.push(p!=0?parseInt(p,16)||c.charCodeAt():0)});return a}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "stringToByteArray",
"description": "Convert a string of characters to an array of UTF-8 bytes using encodeURIComponent",
"keywords": [
"alternative",
"cryptography",
"utf8"
]
}
<!DOCTYPE html>
<title>stringToByteArray</title>
<div>Expected value: <b>49|52|48|227|131|144|227|130|164|227|131|136|0</b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
var stringToByteArray = function(s,a){a=[];encodeURIComponent(s).replace(/%(..)|./g,function(c,p){a.push(p!=0?parseInt(p,16)||c.charCodeAt():0)});return a}
document.getElementById( "ret" ).innerHTML = stringToByteArray("140バイト\x00").join("|")
</script>
@mikesherov
Copy link

Bravo

@bytespider
Copy link

Perfect!

@jed
Copy link

jed commented Jun 5, 2011

this is fantastic work! @subzey, would you mind forking off of the master gist so we can pick it up?

@mathiasbynens
Copy link

c.charCodeAt(0) could be just c.charCodeAt(), as 0 is the default.

@subzey
Copy link
Author

subzey commented Feb 20, 2012

@mathiasbynens, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment