<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Серый форум &mdash; JavaScript: Перевод текста в HEX и обратно]]></title>
	<link rel="self" href="http://forum.script-coding.com/extern.php?action=feed&amp;tid=10976&amp;type=atom" />
	<updated>2016-02-19T17:41:40Z</updated>
	<generator>PunBB</generator>
	<id>http://forum.script-coding.com/viewtopic.php?id=10976</id>
		<entry>
			<title type="html"><![CDATA[Re: JavaScript: Перевод текста в HEX и обратно]]></title>
			<link rel="alternate" href="http://forum.script-coding.com/viewtopic.php?pid=101247#p101247" />
			<content type="html"><![CDATA[<p>Там всё сложно и запутано: JavaScript работает со строками в кодировке Unicode-32, а charCodeAt() возвращает по модулю 2^16.</p><p><a href="https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt">charCodeAt()</a></p>]]></content>
			<author>
				<name><![CDATA[BeginnerJS]]></name>
				<uri>http://forum.script-coding.com/profile.php?id=33702</uri>
			</author>
			<updated>2016-02-19T17:41:40Z</updated>
			<id>http://forum.script-coding.com/viewtopic.php?pid=101247#p101247</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[JavaScript: Перевод текста в HEX и обратно]]></title>
			<link rel="alternate" href="http://forum.script-coding.com/viewtopic.php?pid=97687#p97687" />
			<content type="html"><![CDATA[<p>Как можно перевести текст в HEX и обратно?</p><p>Необходимо что бы переводилась строка также и в кириллице.</p><p>Можно в ANSI или UTF-8</p><p>Например:</p><p>strToHex(&#039;Привет Мир!&#039;); // CFF0E8E2E5F220CCE8F021<br />hexToStr(&#039;CFF0E8E2E5F220CCE8F021&#039;); // Привет Мир!</p><p>Вот пример, но почему то назад HEX в строку переводит некорректно:<br /></p><div class="codebox"><pre><code>&lt;script&gt;
function strTohex(string)
{
    var str = string;
     var hex = &#039;&#039;;
    for(var i=0;i&lt;str.length;i++) {
        hex += &#039;&#039;+str.charCodeAt(i).toString(16);
    }
    return hex;
}
function hexTostr(value)
{
    var hex  = value;
     var str = &#039;&#039;;
    for (var i = 0; i &lt; hex.length; i += 2){
        str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
        }
    return str;
}
&lt;/script&gt;
&lt;input type=&quot;submit&quot; name=&quot;strTohex&quot; value=&quot;strTohex&quot; onclick=&quot;window.alert(strTohex(&#039;Привет Мир!&#039;)); return false;&quot;&gt;
&lt;br&gt;
&lt;br&gt;
&lt;input type=&quot;submit&quot; name=&quot;hexTostr&quot; value=&quot;hexTostr&quot; onclick=&quot;window.alert(hexTostr(&#039;41f4404384324354422041c43844021&#039;)); return false;&quot;&gt;</code></pre></div>]]></content>
			<author>
				<name><![CDATA[avens]]></name>
				<uri>http://forum.script-coding.com/profile.php?id=24487</uri>
			</author>
			<updated>2015-09-25T17:31:36Z</updated>
			<id>http://forum.script-coding.com/viewtopic.php?pid=97687#p97687</id>
		</entry>
</feed>
