Win32 API 日本語リファレンス
ホームNetworking.WinSock › ntohs

ntohs

関数
16ビット値をネットワーク順からホストバイト順へ変換する。
DLLWS2_32.dll呼出規約winapiSetLastErrorあり対応OSWindows 8.1 以降

シグネチャ

// WS2_32.dll
#include <windows.h>

WORD ntohs(
    WORD netshort
);

パラメーター

名前方向
netshortWORDin

戻り値の型: WORD

各言語での呼び出し定義

// WS2_32.dll
#include <windows.h>

WORD ntohs(
    WORD netshort
);
[DllImport("WS2_32.dll", SetLastError = true, ExactSpelling = true)]
static extern ushort ntohs(
    ushort netshort   // WORD
);
<DllImport("WS2_32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function ntohs(
    netshort As UShort   ' WORD
) As UShort
End Function
' netshort : WORD
Declare PtrSafe Function ntohs Lib "ws2_32" ( _
    ByVal netshort As Integer) As Integer
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

ntohs = ctypes.windll.ws2_32.ntohs
ntohs.restype = ctypes.c_ushort
ntohs.argtypes = [
    ctypes.c_ushort,  # netshort : WORD
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('WS2_32.dll')
ntohs = Fiddle::Function.new(
  lib['ntohs'],
  [
    -Fiddle::TYPE_SHORT,  # netshort : WORD
  ],
  -Fiddle::TYPE_SHORT)
#[link(name = "ws2_32")]
extern "system" {
    fn ntohs(
        netshort: u16  // WORD
    ) -> u16;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("WS2_32.dll", SetLastError = true)]
public static extern ushort ntohs(ushort netshort);
"@
$api = Add-Type -MemberDefinition $sig -Name 'WS2_32_ntohs' -Namespace Win32 -PassThru
# $api::ntohs(netshort)
#uselib "WS2_32.dll"
#func global ntohs "ntohs" sptr
; ntohs netshort   ; 戻り値は stat
; netshort : WORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "WS2_32.dll"
#cfunc global ntohs "ntohs" int
; res = ntohs(netshort)
; netshort : WORD -> "int"
; WORD ntohs(WORD netshort)
#uselib "WS2_32.dll"
#cfunc global ntohs "ntohs" int
; res = ntohs(netshort)
; netshort : WORD -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	ws2_32 = windows.NewLazySystemDLL("WS2_32.dll")
	procntohs = ws2_32.NewProc("ntohs")
)

// netshort (WORD)
r1, _, err := procntohs.Call(
	uintptr(netshort),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // WORD
function ntohs(
  netshort: Word   // WORD
): Word; stdcall;
  external 'WS2_32.dll' name 'ntohs';
result := DllCall("WS2_32\ntohs"
    , "UShort", netshort   ; WORD
    , "UShort")   ; return: WORD
●ntohs(netshort) = DLL("WS2_32.dll", "int ntohs(int)")
# 呼び出し: ntohs(netshort)
# netshort : WORD -> "int"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。