<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Серый форум &mdash; AHK: Конвертация endians]]></title>
	<link rel="self" href="https://forum.script-coding.com/extern.php?action=feed&amp;tid=13965&amp;type=atom" />
	<updated>2018-07-23T11:27:48Z</updated>
	<generator>PunBB</generator>
	<id>https://forum.script-coding.com/viewtopic.php?id=13965</id>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Конвертация endians]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=127306#p127306" />
			<content type="html"><![CDATA[<p>Китаец какой-нибудь писал, не иначе.</p>]]></content>
			<author>
				<name><![CDATA[teadrinker]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=24515</uri>
			</author>
			<updated>2018-07-23T11:27:48Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=127306#p127306</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Конвертация endians]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=127305#p127305" />
			<content type="html"><![CDATA[<p>Нашёл в winsock2.h код, который выполняет htonll.<br /></p><div class="codebox"><pre><code>
#define _WS2_32_WINSOCK_SWAP_LONGLONG(l)            \
            ( ( ((l) &gt;&gt; 56) &amp; 0x00000000000000FFLL ) |       \
              ( ((l) &gt;&gt; 40) &amp; 0x000000000000FF00LL ) |       \
              ( ((l) &gt;&gt; 24) &amp; 0x0000000000FF0000LL ) |       \
              ( ((l) &gt;&gt;  8) &amp; 0x00000000FF000000LL ) |       \
              ( ((l) &lt;&lt;  8) &amp; 0x000000FF00000000LL ) |       \
              ( ((l) &lt;&lt; 24) &amp; 0x0000FF0000000000LL ) |       \
              ( ((l) &lt;&lt; 40) &amp; 0x00FF000000000000LL ) |       \
              ( ((l) &lt;&lt; 56) &amp; 0xFF00000000000000LL ) )


#ifndef htonll
__inline unsigned __int64 htonll ( unsigned __int64 Value ) 
{ 
    const unsigned __int64 Retval = _WS2_32_WINSOCK_SWAP_LONGLONG (Value);
    return Retval;
}
#endif /* htonll */
</code></pre></div><p>Выглядит довольно громоздко, учитывая, что у процессора есть инструкция bswap, которая делает это сразу для 32- или 64-битного регистра. А здесь получается для каждого байта сдвиг и AND, да потом ещё OR между результатами. Куча инструкций.</p>]]></content>
			<author>
				<name><![CDATA[YMP]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=81</uri>
			</author>
			<updated>2018-07-23T10:18:38Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=127305#p127305</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Конвертация endians]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=127303#p127303" />
			<content type="html"><![CDATA[<p>Там, во-первых, не 32 байта, а все 64, а во-вторых, нужно не только старший и младший местами поменять, а чтобы все байты были в обратном порядке, чтобы понять, запустите <a href="http://forum.script-coding.com/viewtopic.php?pid=127262#p127262">этот</a> код.</p>]]></content>
			<author>
				<name><![CDATA[teadrinker]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=24515</uri>
			</author>
			<updated>2018-07-23T07:50:43Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=127303#p127303</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Конвертация endians]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=127302#p127302" />
			<content type="html"><![CDATA[<p><strong>teadrinker</strong> Ну тогда может так?<br /></p><div class="codebox"><pre><code>data := (data &lt;&lt; 32) | (data &gt;&gt; 32) ;меняем старший и младший байт местами</code></pre></div>]]></content>
			<author>
				<name><![CDATA[Raven]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=30150</uri>
			</author>
			<updated>2018-07-23T07:44:21Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=127302#p127302</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Конвертация endians]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=127301#p127301" />
			<content type="html"><![CDATA[<p>Ну да, я и смотрю, что в описании dll не указана, то-есть для нас её всё равно, что нет. <img src="//forum.script-coding.com/img/smilies/smile.png" width="15" height="15" /><br /><strong>Raven</strong>, в принципе то, только надо то же самое для такого числа: 0xFFEEDDCCBBAA9988</p>]]></content>
			<author>
				<name><![CDATA[teadrinker]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=24515</uri>
			</author>
			<updated>2018-07-23T06:51:58Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=127301#p127301</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Конвертация endians]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=127300#p127300" />
			<content type="html"><![CDATA[<div class="quotebox"><cite>teadrinker пишет:</cite><blockquote><p>А пример использования можете показать?</p></blockquote></div><p>Судя по её определению,<br /></p><div class="codebox"><pre><code>
unsigned __int64 __inline htonll(
   unsigned __int64 value
);
</code></pre></div><p>это инлайн-функция, т.е., по сути, макрос. Когда компилятор видит в исходнике её вызов, он туда просто вставляет её код. Поэтому в ws2_32.dll её нет, там только htonl.</p>]]></content>
			<author>
				<name><![CDATA[YMP]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=81</uri>
			</author>
			<updated>2018-07-23T06:48:13Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=127300#p127300</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Конвертация endians]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=127298#p127298" />
			<content type="html"><![CDATA[<p>Это не то?<br /></p><div class="codebox"><pre><code>data := (data &lt;&lt; 8) | (data &gt;&gt; 8) ;меняем старший и младший байт местами</code></pre></div>]]></content>
			<author>
				<name><![CDATA[Raven]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=30150</uri>
			</author>
			<updated>2018-07-23T05:36:44Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=127298#p127298</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Конвертация endians]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=127293#p127293" />
			<content type="html"><![CDATA[<div class="quotebox"><cite>MandarinKa02 пишет:</cite><blockquote><p>.. возьму предложенную Malcev&#039;ым.</p></blockquote></div><p>Она у вас не совсем доделанная:<br /></p><div class="codebox"><pre><code>msgbox % htonl_64(0x0123456789abcdef) 
htonl_64(int) { ;x86&gt;net
	return DllCall(&quot;ntdll\RtlUlonglongByteSwap&quot;,&quot;UInt64&quot;,int,&quot;UInt64&quot;)
	;return DllCall(&quot;Ws2_32\htonll&quot;, UInt64, int, UInt64) :NOT_WORK
}</code></pre></div>]]></content>
			<author>
				<name><![CDATA[Malcev]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=26930</uri>
			</author>
			<updated>2018-07-22T21:48:23Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=127293#p127293</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Конвертация endians]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=127291#p127291" />
			<content type="html"><![CDATA[<p>Хах, &quot;монстр&quot; - громко сказано. <img src="//forum.script-coding.com/img/smilies/smile.png" width="15" height="15" /> Есть монстры побольше.<br /></p><div class="quotebox"><cite>teadrinker пишет:</cite><blockquote><p>А пример использования можете показать?</p></blockquote></div><p>Т.к. меня данная функция не работает:<br /></p><div class="codebox"><pre><code>htonl_64(int) {
	return DllCall(&quot;Ws2_32\htonll&quot;, UInt64, int, UInt64) ;NOT_WORK
}</code></pre></div><p>.. возьму предложенную <strong>Malcev</strong>&#039;ым.<br /></p><div class="codebox"><pre><code>htonl_64(int) { ;x86&gt;net
	return DllCall(&quot;ntdll\RtlUlonglongByteSwap&quot;,&quot;UInt64&quot;,int,&quot;UInt64&quot;)
	;return DllCall(&quot;Ws2_32\htonll&quot;, UInt64, int, UInt64) :NOT_WORK
}</code></pre></div><p>След. пример разбирает фрейм WebSocket&#039;а.</p><div class="codebox"><pre><code>	FIN:=(NumGet(&amp;in_data,0,&quot;UChar&quot;))&gt;&gt;7,offset:=1
	len:=NumGet(&amp;in_data,offset,&quot;UChar&quot;)-128,offset+=1 ;exclude the first bit
	OPCODE:=NumGet(&amp;in_data,0,&quot;UChar&quot;)-(FIN?128:0)

	if (len&gt;=0) {
		if(len&lt;=125)
			do_nothing()
		else if(len=126)
			len:=htons(NumGet(&amp;in_data,offset,&quot;UShort&quot;)),offset+=2 ;16bits
		else if(len=127)
			len:=htonl_64(NumGet(&amp;in_data,offset,&quot;UInt64&quot;)),offset+=8 ;64bits
	} VarSetCapacity(mask,4)
	CopyBinData(&amp;in_data+offset,&amp;mask,4),offset+=4
	XOR_crypt(&amp;in_data+offset,data,size:=len,mask)
	StrGet(&amp;data+offset,size,&quot;CP0&quot;)</code></pre></div><div class="fancy_spoiler_switcher"><div class="fancy_spoiler_switcher_header"><strong>+</strong>&nbsp;XOR_crypt</div><div class="fancy_spoiler"><div class="codebox"><pre><code>
XOR_crypt(data,ByRef out,size,ByRef key) {
    VarSetCapacity(out,size,0)
    loop % size {
        i:=A_Index-1
        num:=Abs(NumGet(data+0, i,&quot;UChar&quot;)-1+1)^NumGet(&amp;key+0, (i &amp; 0x3), &quot;UChar&quot;)
        NumPut(num, &amp;out,i,&quot;UChar&quot;)
    } return size
}
</code></pre></div></div></div><p>p.s. недостающие функции можно найти на форумах AHK, если загуглить.</p>]]></content>
			<author>
				<name><![CDATA[MandarinKa02]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=34409</uri>
			</author>
			<updated>2018-07-22T21:06:10Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=127291#p127291</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Конвертация endians]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=127287#p127287" />
			<content type="html"><![CDATA[<p><strong>MandarinKa02</strong>, ну вы просто монстр программирования, я смотрю. <img src="//forum.script-coding.com/img/smilies/smile.png" width="15" height="15" /><br /></p><div class="quotebox"><cite>MandarinKa02 пишет:</cite><blockquote><p>Существует - <a href="https://docs.microsoft.com/en-us/windows/desktop/api/winsock2/nf-winsock2-htonll">https://docs.microsoft.com/en-us/window … ck2-htonll</a>.</p></blockquote></div><p>А пример использования можете показать?</p>]]></content>
			<author>
				<name><![CDATA[teadrinker]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=24515</uri>
			</author>
			<updated>2018-07-22T17:41:43Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=127287#p127287</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Конвертация endians]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=127277#p127277" />
			<content type="html"><![CDATA[<p>Сколько много ответов наплыло..<br />Протестировал еще вчера функцию со второго поста. (спасибо, что напомнили о ней)<br />Работает как надо.<br /></p><div class="fancy_spoiler_switcher"><div class="fancy_spoiler_switcher_header" data-lang-open="открыть спойлер" data-lang-close="скрыть спойлер"><strong>+</strong>&nbsp;открыть спойлер</div><div class="fancy_spoiler"><div class="codebox"><pre><code>ntohl(int) { ;net&gt;x86
	return DllCall(&quot;Ws2_32\ntohl&quot;, UInt, int, UInt)
}
htonl(int) { ;x86&gt;net
	return DllCall(&quot;Ws2_32\htonl&quot;, UInt, int, UInt)
}



ntohs(int) { ;net&gt;x86
	return DllCall(&quot;Ws2_32\ntohs&quot;, UShort, int, UShort)
}
htons(int) { ;x86&gt;net
	return DllCall(&quot;Ws2_32\htons&quot;, UShort, int, UShort)
}



ntohl_64(int) { ;net&gt;x86
	return htonl_64(int)
}
htonl_64(int) { ;x86&gt;net
	return DllCall(&quot;ntdll\RtlUlonglongByteSwap&quot;,&quot;UInt64&quot;,int,&quot;UInt64&quot;)
	;return DllCall(&quot;Ws2_32\htonll&quot;, UInt64, int, UInt64) :NOT_WORK
}</code></pre></div></div></div><div class="quotebox"><cite>teadrinker пишет:</cite><blockquote><p>Скорее всего, не существует для 64 бит.</p></blockquote></div><p>Существует - <a href="https://docs.microsoft.com/en-us/windows/desktop/api/winsock2/nf-winsock2-htonll">https://docs.microsoft.com/en-us/window … ck2-htonll</a>.<br />Только, начиная с Windows 8. <img src="//forum.script-coding.com/img/smilies/smile.png" width="15" height="15" /></p><div class="quotebox"><cite>YMP пишет:</cite><blockquote><p>Не знаю, зачем вам нужны эти uint64, но на всякий случай — AutoHotkey с этим типом нормально не работает, т.к. знает только int64. Поэтому может при вычислениях число испортить.</p></blockquote></div><p>Сам AHK - да, переменной такое большое значение не присвоишь, а вот NumPut&#039;ы работают без проблем.<br />Зачем нужны? Для программы сделал Web-интерфейс и была поставлена задача - связать основной скрипт с браузером. Использовал WebSocket, здесь у каждого <a href="https://tools.ietf.org/html/rfc6455#section-5.2">фрейма</a> есть структура, и если длина фрейма больше 16-битного числа(UShort), то необходимо длину фрейма поместить в int64. И здесь предстал вопрос: почему число задом наперед?</p>]]></content>
			<author>
				<name><![CDATA[MandarinKa02]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=34409</uri>
			</author>
			<updated>2018-07-22T14:15:29Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=127277#p127277</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Конвертация endians]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=127274#p127274" />
			<content type="html"><![CDATA[<div class="quotebox"><cite>teadrinker пишет:</cite><blockquote><p>Скорее всего, не существует для 64 бит.</p></blockquote></div><p>Вот из-за подобных случаев я и пользуюсь AHK 32 bit.</p>]]></content>
			<author>
				<name><![CDATA[Malcev]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=26930</uri>
			</author>
			<updated>2018-07-22T13:23:55Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=127274#p127274</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Конвертация endians]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=127268#p127268" />
			<content type="html"><![CDATA[<p>Да, NumPut, похоже, знает. Но этот разнобой и путает. Тут играем, тут не играем, тут рыбу заворачивали. <img src="//forum.script-coding.com/img/smilies/smile.png" width="15" height="15" /> В чём, интересно, проблема с общей поддержкой uint64.</p>]]></content>
			<author>
				<name><![CDATA[YMP]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=81</uri>
			</author>
			<updated>2018-07-22T13:00:22Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=127268#p127268</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Конвертация endians]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=127266#p127266" />
			<content type="html"><![CDATA[<div class="quotebox"><cite>YMP пишет:</cite><blockquote><p>т.к. знает только int64</p></blockquote></div><p>Судя по всему, NumPut/NumGet знает UInt64, попробуй заменить в моём примере на Int64.</p>]]></content>
			<author>
				<name><![CDATA[teadrinker]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=24515</uri>
			</author>
			<updated>2018-07-22T12:04:10Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=127266#p127266</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Конвертация endians]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=127263#p127263" />
			<content type="html"><![CDATA[<p><strong>MandarinKa02</strong><br />Не знаю, зачем вам нужны эти uint64, но на всякий случай — AutoHotkey с этим типом нормально не работает, т.к. знает только int64. Поэтому может при вычислениях число испортить.<br /></p><div class="codebox"><pre><code>
SetFormat, Integer, Hex
num = 0x8070605040302010
MsgBox, % num + 0
</code></pre></div><p>Но если нужен чисто обратный порядок байтов, то проблем быть не должно, наверно.</p>]]></content>
			<author>
				<name><![CDATA[YMP]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=81</uri>
			</author>
			<updated>2018-07-22T11:30:43Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=127263#p127263</id>
		</entry>
</feed>
