在节点中将UUID转换为二进制/从二进制转换为UUID [英] Convert UUID to/from binary in Node

查看:468
本文介绍了在节点中将UUID转换为二进制/从二进制转换为UUID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从事的项目切换到了MySQL.我们使用的密钥是UUID字符串(例如43d597d7-2323-325a-90fc-21fa5947b9f3),但是数据库字段而不是字符串被定义为binary(16)-16字节无符号二进制.

The project I work on switched to MySQL. The keys we use are UUID strings (like 43d597d7-2323-325a-90fc-21fa5947b9f3), but the database field, rather than be a string, is defined as binary(16) - 16-byte unsigned binary.

我知道UUID基本上是16字节的二进制,但是我不知道如何将二进制数转换为二进制数.

I understand that a UUID is basically a 16-byte binary, but I have no idea how to convert from/to a binary number.

我正在使用 node-mysql 访问数据库,并且尝试使用 node-uuid 来解析UUID,但这会产生一个整数数组.我还尝试使用节点的缓冲区,但这只会产生一个缓冲区对象.

I'm using node-mysql to access the database, and I tried using node-uuid to parse the UUID, but that yields an array of integers. I also tried using Node's Buffer, but that just yields a buffer object.

如何转换UUID字符串以适合该字段?以及如何将从该字段读取的值转换为UUID?

How do I convert a UUID string to fit into that field? And how do I turn a value I read from that field into a UUID?

推荐答案

由于时间紧迫,我将粘贴提供有效结果的注释,并在以后修改答案,以便更清楚.

Due to lack of time, I'll paste the comment that provided valid result(s) and modify the answer later so it's clearer.

对,如果您的JS应用程序中已经有该字符串格式的UUID 43d597d7-2323-325a-90fc-21fa5947b9f3,则可以将以下查询发送到MySQL:

Right, if you have a UUID 43d597d7-2323-325a-90fc-21fa5947b9f3 in that string format already in your JS app, you'd send the following query to MySQL:

SELECT col FROM table WHERE uuid_col = UNHEX(REPLACE('43d597d7-2323-325a-90fc-21fa5947b9f3', '-', ''));

SELECT col FROM table WHERE uuid_col = UNHEX(REPLACE('43d597d7-2323-325a-90fc-21fa5947b9f3', '-', ''));

如果要提取数据并使用可读格式的UUID,则必须将其转换为十六进制表示法.

If you want to pull data out and have UUID in readable format, you have to convert it to hexadecimal notation.

SELECT HEX(uuid_col) FROM table;

SELECT HEX(uuid_col) FROM table;

那将给您UUID而不是破折号.如果您给node-uuid.parse方法提供不带破折号的十六进制字符串,则该方法似乎可以使用.

That one will give you the UUID without dashes. It appears that the node-uuid.parse method works if you give it hex string without dashes.

这篇关于在节点中将UUID转换为二进制/从二进制转换为UUID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆