不同语言的 USB 标签

不同语言的 USB 标签

如果 USB 标签(名称)采用不同的语言(印地语或中文),我如何知道它被编码为哪种语言以传递给 iconv,或者我们有什么方法可以知道所使用的语言?尝试使用 udevadm

答案1

这个链接描述字符串描述符 0 如何保存其余字符串作为 16 位代码使用的所有语言的列表,例如 0x0409 表示美国英语。 (大多数情况下,该列表仅包含一种语言。)

当 USB 实用程序要求进行字符串查找时,它会使用此 16 位代码指定所需的语言,或者默认为 0。我想大多数实用程序只寻找第一语言。所有字符串均采用 unicode。

您可以编写一些简单的 python 来获取语言代码(安装包 pyusb):

#!/usr/bin/python
import usb.core
import usb.util
import usb.control

def getlangs(dev): # from /usr/lib/python2.7/site-packages/usb/util.py
        # Asking for the zero'th index is special - it returns a string
        # descriptor that contains all the language IDs supported by the device.
        # Typically there aren't many - often only one. The language IDs are 16
        # bit numbers, and they start at the third byte in the descriptor. See
        # USB 2.0 specification section 9.6.7 for more information.
        # Note from libusb 1.0 sources (descriptor.c)
        buf = usb.control.get_descriptor(dev, 254, usb.util.DESC_TYPE_STRING, 0)
        assert len(buf) >= 4
        langid = buf[2] | (buf[3] << 8)
        return langid

for dev in usb.core.find(find_all=True):
    try:
        print usb.util.get_string(dev, dev.iManufacturer) # ,langid=0x409
        print " first language code 0x%x" % getlangs(dev)
    except usb.core.USBError:
        pass

相关内容