64位进程池HashCode兼容处理

本文介绍了如何在64位环境下为32位程序生成兼容的HashCode,通过自定义方法实现了从字符串到兼容的整型哈希值的转换,确保了在不同位数环境中数据的一致性和兼容性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

背景

net旧项目使用32位生成的HashCode,存储到数据库中。迁移到64位上,就需要对HashCode做兼容处理。

解决方案

1:进程池配置支持32位程序。

2:对Hashcode做兼容处理,【推荐】。

兼容实现

 static void Main(string[] args)
        {

            string test = "hello";

            //-327419862  64位下
            //-695839  32位下
            int bit = test.GetHashCode();

            int hashCode = CompatibleHash("hello");
        }
        /// <summary>
        /// 64位环境下,生成兼容32位的hashCode。
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static unsafe int CompatibleHash(string str)
        {
            fixed (char* charPtr = new String(str.ToCharArray()))
            {
                int hashCode = (5381 << 16) + 5381;
                int numeric = hashCode;
                int* intPtr = (int*)charPtr;
                for (int i = str.Length; i > 0; i -= 4)
                {
                    hashCode = ((hashCode << 5) + hashCode + (hashCode >> 27)) ^ intPtr[0];

                    if (i <= 2) break;
                    numeric = ((numeric << 5) + numeric + (numeric >> 27)) ^ intPtr[1];
                    intPtr += 2;
                }
                return hashCode + numeric * 1566083941;
            }
        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值