How To Convert A String Into It's Real Binary Representation (UTF-8 Or Whatever Is Currently Used)?
I want to experiment with UTF-8 and Unicode, for that I want to build a small Website which helps me to understand the encoding better.
First I want the ability to enter some Text and then get the actual binary encoding of the string. For that I'm searching for a equivalent to ".GetBytes" from C# or Java. I do not want the resolved CharCodes!
Here a C# function I would like to reproduce in JavaScript
string ToBinary(string input)
{
//this is the part I am looking for in JavaScript
var utf8Bytes = Encoding.UTF8.GetBytes(input);
var bytesFormatedToBin = utf8Bytes.Select(b => Convert.ToString(b, 2).PadLeft(8, '0'));
return string.Join(' ', bytesFormatedToBin);
}
Here some sample results:
- "abc" => "01100001 01100010 01100011"
- "@©®" => "01000000 11000010 10101001 11000010 10101110"
- "😀😄" => "11110000 10011111 10011000 10000000 11110000 10011111 10011000 10000100"
Is there a way to achieve this in JavaScript?
Thanks. Marc
Edit: Fixed truncated sample result.
Answer
String.prototype.charCodeAt(...)
only works properly when the the string only contains ASCII characters. You'll have to use the standard TextEncoder
if you want to deal with other characters:
const te = new TextEncoder('utf-8')
function toBinaryRepr(str) {
return Array.from(te.encode(str))
.map(i => i
.toString(2)
.padStart(8, '0'))
.join(' ')
}
// '01100001 01100010 01100011'
toBinaryRepr('abc')
// '01000000 11000010 10101001 11000010 10101110'
toBinaryRepr('@©®')
// '11110000 10011111 10011000 10000000 11110000 10011111 10011000 10000100'
toBinaryRepr('😀😄')
Warning: TextEncoder
is not a global constructor in older versions of Node.js - if you get some errors saying TextEncoder
is not defined, try importing it by:
const { TextEncoder } = require('util')
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM