【问题标题】:How to reformat a string that represents a big-endian into a MAC-Address format?如何将表示大端序的字符串重新格式化为 MAC 地址格式?
【发布时间】:2014-12-05 05:22:24
【问题描述】:

我有以下字符串解析问题。给定一个类似 endian 的字符串,输出一个 MAC 地址格式的字符串。示例:

输入:'\x01\xfa'

输出:'01:fa'

我需要的东西要复杂得多,但我在执行这项任务时遇到了困难。我感谢任何可能帮助我进行此类转换的函数的参考。

【问题讨论】:

    标签: python string format endianness


    【解决方案1】:

    generator expressionord% operator 一起使用:

    >>> ':'.join('%02x' % ord(ch) for ch in '\x01\xfa')
    '01:fa'
    

    str.format:

    >>> ':'.join('{:02x}'.format(ord(ch)) for ch in '\x0a\xfa')
    '0a:fa'
    

    【讨论】:

    • 感谢您的参考和解决方案。
    猜你喜欢
    • 2016-06-27
    • 1970-01-01
    • 2019-08-18
    • 1970-01-01
    • 2014-11-30
    • 1970-01-01
    • 1970-01-01
    • 2021-04-01
    • 2020-06-10
    相关资源
    最近更新 更多