MathSwap

ushort型値のバイト順を変更します。

ushort  MathSwap(
  ushort  value      // 値
  );

パラメータ

value

[in]  バイト順を変更する値

戻り値

バイト順が反転されたushort値

MathSwap

uint型値のバイト順を変更します。

uint  MathSwap(
  uint  value      // 値
  );

パラメータ

value

[in]  バイト順を変更する値

戻り値

バイト順が反転されたuint値

MathSwap

ulong型値のバイト順を変更します。

ulong  MathSwap(
  ulong  value      // 値
  );

パラメータ

value

[in]  バイト順を変更する値

戻り値

バイト順が反転されたulong値

 

例:

#property script_show_inputs
 
input ulong InpLongValue  = 1;   // ここに任意のulong値を入力
input uint   InpIntValue   = 2;   // ここに任意のuint値を入力
input ushort InpShortValue = 3;   // ここに任意のushort値を入力
 
//+------------------------------------------------------------------+
//| スクリプトプログラム開始関数                                              |
//+------------------------------------------------------------------+
void OnStart()
 {
 
//--- 入力され、MathSwap()によって変換された値を10進数および2進数表現で操作ログに出力する
  Print(ValueDescription(InpLongValue));
  Print(ValueDescription(InpIntValue));
  Print(ValueDescription(InpShortValue));
  /*
  結果:
  ulong value: 1
  ulong value: 72057594037927936 using MathSwap()
  binary ulong value: 0000000000000000000000000000000000000000000000000000000000000001
  binary ulong value: 0000000100000000000000000000000000000000000000000000000000000000 using MathSwap()
 
  uint value: 2
  uint value: 33554432 using MathSwap()
  binary uint value: 00000000000000000000000000000010
  binary uint value: 00000010000000000000000000000000 using MathSwap()
 
  ushort value: 3
  ushort value: 768 using MathSwap()
  binary ushort value: 0000000000000011
  binary ushort value: 0000001100000000 using MathSwap()
  */
 }
//+------------------------------------------------------------------+
//| 変数値を説明するテキストを返す                                           |
//+------------------------------------------------------------------+
template <typename T>
string ValueDescription(T x)
 {
  int   num_bits   = sizeof(T)*8;
  string type_name  = typename(T);
  string bin_x      = NumberToBinaryString(x);
  string bin_swap_x = NumberToBinaryString(MathSwap(x));
  return(StringFormat("%s value: %lld\n%s value: %lld using MathSwap()\nbinary %s value: %0*s\nbinary %s value: %0*s using MathSwap()\n",
                      type_name, x, type_name, MathSwap(x), type_name, num_bits, bin_x, type_name, num_bits, bin_swap_x));
 }
//+------------------------------------------------------------------+
//| 数値の2進数表現を文字列として返す                                       |
//+------------------------------------------------------------------+
template <typename T>
string NumberToBinaryString(T x)
 {
  string res  = "";
  int   i    = -1;
  uchar size = sizeof(T)*8-1;
  ulong mask = (ulong)1<<size;
  while(!((x<<++i)& mask));
  for(; i <=size; i++)
    res += !((x<<i)& mask) ? "0" : "1";
  return res;
 }

参照

ネットワーク関数SocketReadSocketSendSocketTlsReadSocketTlsReadAvailableSocketTlsSend