将 id “1_0xr__abc@xyz.com__abc@uvw.com__054gg” 转换或编码为数字 srtri

Convert or encode id "1_0xr__abc@xyz.com__abc@uvw.com__054gg" into a number srtring

本文关键字:com 转换 编码 数字 srtri abc@uvw id 0xr abc@xyz 054gg      更新时间:2023-09-26

有什么可以转换或编码的吗 id ="1_0xr__abc@xyz.com__abc@uvw.com__054gg"到特定的数字字符串中

例如,假设id="abcd@er.com"转换为任何数字字符串"1233452556"

在javascript中可以做到这一点吗?

你可以对字符串进行哈希处理

String.prototype.hashCode = function() {
      var hash = 0, i, chr, len;
      if (this.length == 0) return hash;
      for (i = 0, len = this.length; i < len; i++) {
        chr   = this.charCodeAt(i);
        hash  = ((hash << 5) - hash) + chr;
        hash |= 0; // Convert to 32bit integer
      }
      return hash;
    };

来源: http://werxltd.com/wp/2010/05/13/javascript-implementation-of-javas-string-hashcode-method/

使用

id="abcd@er.com".hashCode();