在Python中将“little endian”十六进制字符串转换为IP地址

时间:2022-10-08 18:11:56

What's the best way to turn a string in this form into an IP address: "0200A8C0". The "octets" present in the string are in reverse order, i.e. the given example string should generate 192.168.0.2.

将这种形式的字符串转换为IP地址的最佳方式是什么:“0200A8C0”。字符串中的“octets”是反向顺序,即给定的示例字符串应该生成192.168.0.2。

4 个解决方案

#1


29  

Network address manipulation is provided by the socket module.

套接字模块提供网络地址操作。

socket.inet_ntoa(packed_ip)

socket.inet_ntoa(packed_ip)

Convert a 32-bit packed IPv4 address (a string four characters in length) to its standard dotted-quad string representation (for example, ‘123.45.67.89’). This is useful when conversing with a program that uses the standard C library and needs objects of type struct in_addr, which is the C type for the 32-bit packed binary data this function takes as an argument.

将32位打包的IPv4地址(一个长度为4个字符的字符串)转换为其标准的点四边形字符串表示形式(例如,' 123.45.67.89 ')。当与使用标准C库的程序进行对话时,这是有用的,它需要类型struct in_addr的对象,这是32位填充二进制数据的C类型,这个函数作为一个参数。

You can translate your hex string to packed ip using struct.pack() and the little endian, unsigned long format.

您可以使用struct.pack()和little endian来将您的十六进制字符串转换成填充的ip。

>>> import socket
>>> import struct
>>> addr_long = int("0200A8C0",16)
>>> hex(addr_long)
'0x200a8c0'
>>> struct.pack("<L", addr_long)
'\xc0\xa8\x00\x02'

>>> socket.inet_ntoa(struct.pack("<L", addr_long))
'192.168.0.2'
>>> 

#2


4  

>>> s = "0200A8C0"
>>> bytes = ["".join(x) for x in zip(*[iter(s)]*2)]
>>> bytes
['02', '00', 'A8', 'C0']
>>> bytes = [int(x, 16) for x in bytes]
>>> bytes
[2, 0, 168, 192]
>>> print ".".join(str(x) for x in reversed(bytes))
192.168.0.2

It is short and clear; wrap it up in a function with error checking to suit your needs.

它简短而清晰;将它封装在一个带有错误检查的函数中,以满足您的需要。


Handy grouping functions:

方便的分组功能:

def group(iterable, n=2, missing=None, longest=True):
  """Group from a single iterable into groups of n.

  Derived from http://bugs.python.org/issue1643
  """
  if n < 1:
    raise ValueError("invalid n")
  args = (iter(iterable),) * n
  if longest:
    return itertools.izip_longest(*args, fillvalue=missing)
  else:
    return itertools.izip(*args)

def group_some(iterable, n=2):
  """Group from a single iterable into groups of at most n."""
  if n < 1:
    raise ValueError("invalid n")
  iterable = iter(iterable)
  while True:
    L = list(itertools.islice(iterable, n))
    if L:
      yield L
    else:
      break

#3


2  

You could do something like this:

你可以这样做:

>>> s = '0200A8C0'
>>> octets = [s[i:i+2] for i in range(0, len(s), 2)]
>>> ip = [int(i, 16) for i in reversed(octets)]
>>> ip_formatted = '.'.join(str(i) for i in ip)
>>> print ip_formatted
192.168.0.2

The octet splitting could probably be done more elegantly, but I can't think of a simpler way off the top of my head.

八位元分裂可能可以做得更优雅,但我想不出一个更简单的方法离开我的头顶。

EDIT: Or on one line:

编辑:或在一行:

>>> s = '0200A8C0'
>>> print '.'.join(str(int(i, 16)) for i in reversed([s[i:i+2] for i in range(0, len(s), 2)]))
192.168.0.2

#4


0  

My try:

我的尝试:

a = '0200A8C0'
indices = range(0, 8, 2)
data = [str(int(a[x:x+2], 16)) for x in indices]
'.'.join(reversed(data))

#1


29  

Network address manipulation is provided by the socket module.

套接字模块提供网络地址操作。

socket.inet_ntoa(packed_ip)

socket.inet_ntoa(packed_ip)

Convert a 32-bit packed IPv4 address (a string four characters in length) to its standard dotted-quad string representation (for example, ‘123.45.67.89’). This is useful when conversing with a program that uses the standard C library and needs objects of type struct in_addr, which is the C type for the 32-bit packed binary data this function takes as an argument.

将32位打包的IPv4地址(一个长度为4个字符的字符串)转换为其标准的点四边形字符串表示形式(例如,' 123.45.67.89 ')。当与使用标准C库的程序进行对话时,这是有用的,它需要类型struct in_addr的对象,这是32位填充二进制数据的C类型,这个函数作为一个参数。

You can translate your hex string to packed ip using struct.pack() and the little endian, unsigned long format.

您可以使用struct.pack()和little endian来将您的十六进制字符串转换成填充的ip。

>>> import socket
>>> import struct
>>> addr_long = int("0200A8C0",16)
>>> hex(addr_long)
'0x200a8c0'
>>> struct.pack("<L", addr_long)
'\xc0\xa8\x00\x02'

>>> socket.inet_ntoa(struct.pack("<L", addr_long))
'192.168.0.2'
>>> 

#2


4  

>>> s = "0200A8C0"
>>> bytes = ["".join(x) for x in zip(*[iter(s)]*2)]
>>> bytes
['02', '00', 'A8', 'C0']
>>> bytes = [int(x, 16) for x in bytes]
>>> bytes
[2, 0, 168, 192]
>>> print ".".join(str(x) for x in reversed(bytes))
192.168.0.2

It is short and clear; wrap it up in a function with error checking to suit your needs.

它简短而清晰;将它封装在一个带有错误检查的函数中,以满足您的需要。


Handy grouping functions:

方便的分组功能:

def group(iterable, n=2, missing=None, longest=True):
  """Group from a single iterable into groups of n.

  Derived from http://bugs.python.org/issue1643
  """
  if n < 1:
    raise ValueError("invalid n")
  args = (iter(iterable),) * n
  if longest:
    return itertools.izip_longest(*args, fillvalue=missing)
  else:
    return itertools.izip(*args)

def group_some(iterable, n=2):
  """Group from a single iterable into groups of at most n."""
  if n < 1:
    raise ValueError("invalid n")
  iterable = iter(iterable)
  while True:
    L = list(itertools.islice(iterable, n))
    if L:
      yield L
    else:
      break

#3


2  

You could do something like this:

你可以这样做:

>>> s = '0200A8C0'
>>> octets = [s[i:i+2] for i in range(0, len(s), 2)]
>>> ip = [int(i, 16) for i in reversed(octets)]
>>> ip_formatted = '.'.join(str(i) for i in ip)
>>> print ip_formatted
192.168.0.2

The octet splitting could probably be done more elegantly, but I can't think of a simpler way off the top of my head.

八位元分裂可能可以做得更优雅,但我想不出一个更简单的方法离开我的头顶。

EDIT: Or on one line:

编辑:或在一行:

>>> s = '0200A8C0'
>>> print '.'.join(str(int(i, 16)) for i in reversed([s[i:i+2] for i in range(0, len(s), 2)]))
192.168.0.2

#4


0  

My try:

我的尝试:

a = '0200A8C0'
indices = range(0, 8, 2)
data = [str(int(a[x:x+2], 16)) for x in indices]
'.'.join(reversed(data))