HASH算法


[self hashAlgorithmMethod:@"hhaabccdeef"];

//ASCII码映射表算是某种哈希规则函数的直观呈现,通过该哈希函数表存储/查找,有效地提高效率。
//符号字母a对应着ASCII映射表中数据编码值97,数组索引97下标
//HASH算法
//2^8=256种编码组合
- (void)hashAlgorithmMethod:(NSString *)testStr {
    char testCh[100];
    memcpy(testCh,[testStr cStringUsingEncoding:NSUTF8StringEncoding], [testStr length]);
    int list[256];//数组坑位中存储每个字符出现的次数
    for (int i = 0; i < 256; i++) {
        list[i] = 0;
    }
    //数组容器的底层采用的结构特征:链表结构
    char *p = testCh;//p指针型变量;p++地址右移1个字节长度后下一个字节对应的新地址//p本身的栈中存储着第0号数据内容元素所在内存区的入口地址&0
    char result = '\0';
    while (*p != result) {
        list[*(p++)]++;
    }
    p = testCh;
    while (*p != result) {
        if (list[*p] == 1) {
            result = *p;
            break;
        }
        p++;
    }
    printf("result:%c",result);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java 中常用的 Hash 算法有以下几: 1. MD5(Message Digest Algorithm 5):MD5 是一单向加密算法,不可逆,常用于验证数据的完整性和一致性。 ```java import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class HashAlgorithms { public static void main(String[] args) throws NoSuchAlgorithmException { String input = "hello world"; MessageDigest md = MessageDigest.getInstance("MD5"); byte[] mdBytes = md.digest(input.getBytes()); StringBuilder hexString = new StringBuilder(); for (byte b : mdBytes) { hexString.append(String.format("%02x", b)); } System.out.println(hexString.toString()); } } ``` 2. SHA(Secure Hash Algorithm):SHA 也是一单向加密算法,主要用于数字签名和验证数据的完整性。 ```java import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class HashAlgorithms { public static void main(String[] args) throws NoSuchAlgorithmException { String input = "hello world"; MessageDigest md = MessageDigest.getInstance("SHA-256"); byte[] mdBytes = md.digest(input.getBytes()); StringBuilder hexString = new StringBuilder(); for (byte b : mdBytes) { hexString.append(String.format("%02x", b)); } System.out.println(hexString.toString()); } } ``` 3. MurmurHash:MurmurHash 是一高性能 Hash 算法,适用于大规模数据集的 Hash 计算。 ```java import com.google.common.hash.HashCode; import com.google.common.hash.HashFunction; import com.google.common.hash.Hashing; public class HashAlgorithms { public static void main(String[] args) { String input = "hello world"; HashFunction hf = Hashing.murmur3_128(); HashCode hc = hf.hashBytes(input.getBytes()); System.out.println(hc.toString()); } } ``` 4. CRC32(Cyclic Redundancy Check):CRC32 是一循环冗余校验算法,常用于数据传输或存储时的错误检测。 ```java import java.util.zip.CRC32; public class HashAlgorithms { public static void main(String[] args) { String input = "hello world"; CRC32 crc32 = new CRC32(); crc32.update(input.getBytes()); System.out.println(crc32.getValue()); } } ``` 以上 Hash 算法都有其特定的应用场景,具体选择哪算法需要根据具体的需求来决定。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HaiJunYa

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值