简体   繁体   English

在python中将数据从sqlite转换为十六进制字符串表示法

[英]Converting data from sqlite into hex string notation in python

I have a 64 bit address that i want to store in a sqlite db column. 我有一个要存储在sqlite db列中的64位地址。 The column is of type VARCHAR. 该列的类型为VARCHAR。 This is how i store the data to the db. 这就是我将数据存储到数据库的方式。 The sql is done in python using the cursor object c. sql是使用光标对象c在python中完成的。

c.execute("INSERT INTO SENSORS(XBEE_ID) VALUES('\x00\x13\xA2\x00\x40\xe9\x21\x9d')")

I want to then read that address from the database and pass it to a function that will then send the data to some xbees. 然后,我想从数据库中读取该地址,并将其传递给一个函数,该函数随后将数据发送到某些xbee。 I am using a xbee library that accepts the address in the form: 我使用的xbee库接受以下形式的地址:

dest_addr = '\x00\x13\xa2\x00\x40\xe9\x21\x93'
xbee.tx(dest_addr_long = dest_addr ,data=newState)

when i use 当我使用

repr(dest_addr) 

I get 我懂了

 '\x00\x13\xa2\x00@\xe9!\x93'

also

type(dest_addr)

gives

<type 'str'>

But when I read the data from the database, and store to a variable,I get an error "the data provided for 'dest_addr_long' was not 8 bytes long. 但是,当我从数据库中读取数据并存储到变量中时,出现错误“为'dest_addr_long'提供的数据不是8个字节长。

 addr_long = c.fetchall()
 addr_long2 = addr_long([0][0])
 xbee.tx(dest_addr_long = addr_long2,data=newState)

when i read the data from the db it is of type unicode. 当我从数据库读取数据时,它是unicode类型的。

repr(send_addr[0][0])

which returns 哪个返回

u'\\x00\\x13\\xa2\\x00\\x40\\xe9\\x21\\x93'

and

<type 'unicode'>

, so i tried this - Convert a Unicode string to a string in Python (containing extra symbols) but it did not work. ,所以我尝试了此操作- 将Unicode字符串转换为Python中的字符串(包含额外的符号),但是没有用。

I am basically looking for the opposite of this . 我基本上正在寻找与此相反的东西。 Converting string into hex representation 将字符串转换为十六进制表示

**so my questions, is my method right for storing the data in the db? **所以我的问题是,我的方法是否适合在db中存储数据?

how can I manipulate the data so that I can get it in the string type that is required for the function?** 如何处理数据,以便能够以函数所需的字符串类型获取数据?**

因此,经过朋友的研究和帮助后,您可以简单地使用

send_addr[0][0].decode("string_escape")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM