gpt4 book ai didi

hibernate - hibernate 实体中IP地址的最佳类型?

转载 作者:行者123 更新时间:2023-12-04 23:09:48 27 4
gpt4 key购买 nike

使用 Hibernate 在数据库中存储 IP 地址的最佳类型是什么?

我虽然是 Byte[] 或 String,但有没有更好的方法,或者你用什么?

 @Column(name = "range_from",  nullable = false)
public Byte[] getRangeFrom() {
return rangeFrom;
}
public void setRangeFrom(Byte[] rangeFrom) {
this.rangeFrom = rangeFrom;
}

最佳答案

我把它存放在 ,查找速度非常快。您可以使用以下函数进行转换。

public static string GetStandardIP(long numericIP)
{
string w = Convert.ToString(Convert.ToInt64(numericIP / 16777216) % 256);
string x = Convert.ToString(Convert.ToInt64(numericIP / 65536) % 256);
string y = Convert.ToString(Convert.ToInt64(numericIP / 256) % 256);
string z = Convert.ToString(Convert.ToInt64(numericIP) % 256);

return w + "." + x + "." + y + "." + z;
}

和这个
public static long GetNumericIP(string standardIP)
{
if (standardIP != null && standardIP != string.Empty)
{
string[] ipParts = standardIP.Split('.');
long numericIP = 16777216 * Convert.ToInt64(ipParts[0]) + 65536 * Convert.ToInt64(ipParts[1]) + 256 * Convert.ToInt32(ipParts[2]) + Convert.ToInt32(ipParts[3]);

return numericIP;
}
return 0;
}

您可能希望通过检查参数来改进它们。

关于hibernate - hibernate 实体中IP地址的最佳类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3342378/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com