python - HEX() function in python sqlalchemy
问题描述
I stored uuid4 without dashes using native UNHEX() as binary in MySQL and use native HEX() function to retrieve the uuid4.
Example:
UUID4: UNHEX("7D96F13AC8394EF5A60E8252B70FC179")
BINARY IN MySQL: }éŽ:·9NŠÙ ýRÈ ¾y
UUID4: HEX(UUID4Column)
This works fine to store and retrieve using its functions HEX() and UNHEX().
However, by using ORM sqlalchemy and declare UUID4Column as LargeBinary(16), the return value become b'}\x96\xf1:\xc89N\xf5\xa6\x0e\x82R\xb7\x0f\xc1y'
How can convert this bytes to 7D96F13AC8394EF5A60E8252B70FC179
in python code?
解决方案
Without any imports:
You can use int.from_bytes and string formatting with 'X'
to get the hexadecimal representation of the integer:
s = b'}\x96\xf1:\xc89N\xf5\xa6\x0e\x82R\xb7\x0f\xc1y'
nicer = f"{(int.from_bytes(s,byteorder='big')):X}"
print(nicer)
prints:
7D96F13AC8394EF5A60E8252B70FC179
推荐阅读
- concurrency - 从多个内核更新 PTE 位 - 它们如何避免相互踩踏?
- node.js - 版本不兼容节点和npm?
- python - 如何像椭圆一样平滑 ConvexHull?
- javascript - 适合父容器的图像定向
- json - 重用 JSON 子模式
- angular - 使用 Angular Material 的 cdkDrag 指令时,ngx quill 编辑器无法正常工作
- c# - c# BindingList with Dapper
- python - 关于CURD更新测试后的问题,这个邮箱已经存在于Store
- android - 如何更改从第一个活动单击的按钮上的第二个活动的文本?[科特林]
- spring - HTTP 状态 500 - 内部服务器错误 org.springframework.beans.factory.BeanCreationException: