Golang提供了htonl/htons函数吗?

huangapple go评论80阅读模式
英文:

Does golang provide htonl/htons?

问题

在C编程中,当我想要通过网络发送一个整数时,我们需要使用htonl()或htons()将整数从主机字节顺序转换为网络字节顺序,然后再发送。

但是在Go语言中,我已经查看了net包,并没有找到类似htons/htonl的函数。那么在使用Go语言时,我应该如何发送一个整数呢?我需要自己实现htons/htonl吗?

英文:

In C programming, when I want to send an integer across network, we need to use htonl() or htons() to convert the integer from host byte order
to network byte order before sending it.

But in golang, I have checked the net package, and can't find the similar functions like htons/htonl. So how should I send an integer when using golang? Do I need to implement htons/htonl
myself?

答案1

得分: 28

网络字节顺序就是大端序,所以你可以使用encoding/binary包来进行编码。

例如:

data := make([]byte, 6)
binary.BigEndian.PutUint16(data, 0x1011)
binary.BigEndian.PutUint32(data[2:6], 0x12131415)

另外,如果你要写入一个io.Writer,可以使用同一个包中的binary.Write()函数,这样可能更方便(同样使用binary.BigEndian作为order参数)。

英文:

Network byte order is just big endian, so you can use the encoding/binary package to perform the encoding.

For example:

data := make([]byte, 6)
binary.BigEndian.PutUint16(data, 0x1011)
binary.BigEndian.PutUint32(data[2:6], 0x12131415)

Alternatively, if you are writing to an io.Writer, the binary.Write() function from the same package may be more convenient (again, using the binary.BigEndian value as the order argument).

答案2

得分: 4

我认为你想要的是encoding/binary中的ByteOrder

ByteOrder用于指定如何将字节序列转换为16位、32位或64位的无符号整数。

英文:

I think what you're after is ByteOrder in encoding/binary.

>>A ByteOrder specifies how to convert byte sequences into 16-, 32-, or 64-bit unsigned integers.

答案3

得分: 0

只需将大端序和小端序之间的整数进行转换。

// NetToHostShort将一个16位整数从网络字节顺序转换为主机字节顺序,也称为“ntohs”
func NetToHostShort(i uint16) uint16 {
	data := make([]byte, 2)
	binary.BigEndian.PutUint16(data, i)
	return binary.LittleEndian.Uint16(data)
}

// NetToHostLong将一个32位整数从网络字节顺序转换为主机字节顺序,也称为“ntohl”
func NetToHostLong(i uint32) uint32 {
	data := make([]byte, 4)
	binary.BigEndian.PutUint32(data, i)
	return binary.LittleEndian.Uint32(data)
}

// HostToNetShort将一个16位整数从主机字节顺序转换为网络字节顺序,也称为“htons”
func HostToNetShort(i uint16) uint16 {
	b := make([]byte, 2)
	binary.LittleEndian.PutUint16(b, i)
	return binary.BigEndian.Uint16(b)
}

// HostToNetLong将一个32位整数从主机字节顺序转换为网络字节顺序,也称为“htonl”
func HostToNetLong(i uint32) uint32 {
	b := make([]byte, 4)
	binary.LittleEndian.PutUint32(b, i)
	return binary.BigEndian.Uint32(b)
}
英文:

Just convert integers between big-endian and little-endian.

// NetToHostShort converts a 16-bit integer from network to host byte order, aka "ntohs"
func NetToHostShort(i uint16) uint16 {
	data := make([]byte, 2)
	binary.BigEndian.PutUint16(data, i)
	return binary.LittleEndian.Uint16(data)
}

// NetToHostLong converts a 32-bit integer from network to host byte order, aka "ntohl"
func NetToHostLong(i uint32) uint32 {
	data := make([]byte, 4)
	binary.BigEndian.PutUint32(data, i)
	return binary.LittleEndian.Uint32(data)
}

// HostToNetShort converts a 16-bit integer from host to network byte order, aka "htons"
func HostToNetShort(i uint16) uint16 {
	b := make([]byte, 2)
	binary.LittleEndian.PutUint16(b, i)
	return binary.BigEndian.Uint16(b)
}

// HostToNetLong converts a 32-bit integer from host to network byte order, aka "htonl"
func HostToNetLong(i uint32) uint32 {
	b := make([]byte, 4)
	binary.LittleEndian.PutUint32(b, i)
	return binary.BigEndian.Uint32(b)
}

huangapple
  • 本文由 发表于 2013年9月25日 11:00:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/18995477.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定