双向String散列函数 [英] A two way String hash function

查看:113
本文介绍了双向String散列函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得String的唯一数字表示。我知道有很多方法可以做到这一点,我的问题是你认为哪种方式最好?我不想有负数 - 所以java中的hashcode()函数不太好,虽然我可以覆盖它...但我宁愿不要因为我不那么自信而且不想意外打破一些东西。

I want to get a unique numeric representation of a String. I know there are lots of ways of doing this, my question is which do you think is the best? I don't want to have negative numbers - so the hashcode() function in java is not so good, although I could override it ... but I'd rather not since I am not so confident and don't want to accidentally break something.

我的字符串都是语义网络的URIS。数字表示的原因是当我在页面上显示URI的数据时,我需要将某些内容传递给查询字符串或放入我的javascript中的各个字段。当URI作为URI中的值时,URI本身太笨重并且看起来很糟糕。

My Strings are all semantic-web URIS. The reason for the numeric representation is that when I display the data for a URI on a page I need something to pass into the query String or put into various fields in my javascript. The URI itself is too unwieldy and looks bad when you have a URI as a value in a URI.

基本上我想要一个名为的类资源这将是这样的

Basically I want to have a class called Resource which will look like this

Resource{
  int id;
  String uri;
  String value; // this is the label or human readable name

  // .... other code/getters/setters here

  public int getId(){
    return id = stringToIntFunction();
  }

  private int stringToIntFunction(String uri){
  // do magic here
  }
}

如果出现以下情况,您能否建议执行此操作:

Can you suggestion a function that would do this if:


  1. 它必须是两种方式,即你也可以从数值中恢复原始字符串

  2. 它不必是双向的

还有其他重要的问题我不考虑吗?

Also are there other issues that are important that I am not considering?

推荐答案

如果你想要它是可逆的,那你就麻烦了。哈希设计是单向的。

If you want it to be reversible, you're in trouble. Hashes are designed to be one-way.

特别是,假设 int 有32位信息, char 有16位信息,需要可逆性意味着你只能有零个,一个或两个字符的字符串(甚至假设那个你很乐意将编码为\0\0或类似的东西)。当然,这是假设你没有任何存储空间。如果你可以使用存储,那么只需按顺序存储数字......例如:

In particular, given that an int has 32 bits of information, and a char has 16 bits of information, requiring reversibility means you can only have strings of zero, one or two characters (and even that's assuming that you're happy to encode "" as "\0\0" or something similar). That's assuming you don't have any storage, of course. If you can use storage, then just store numbers sequentially... something like:

private int stringToIntFunction(String uri) {
    Integer existingId = storage.get(uri);
    if (existingId != null) {
        return existingId.intValue();
    }
    return storage.put(uri);
}

这里 storage.put()会在内部增加一个计数器,将URI存储为与该计数器值相关联,然后返回它。我的猜测是,这不是你所追求的。

Here storage.put() would increase a counter internally, store the URI as being associated with that counter value, and return it. My guess is that that's not what you're after though.

基本上,为了执行可逆加密,我使用标准加密库将字符串转换为a首先是二进制格式(例如使用UTF-8)。我希望结果是 byte []

Basically, to perform a reversible encryption, I'd use a standard encryption library having converted the string to a binary format first (e.g. using UTF-8). I would expect the result to be a byte[].

如果它必须是可逆的,我考虑只取正常 hashCode()结果的绝对值(但映射 Integer.MIN_VALUE 特定的东西,因为它的绝对值不能表示为 int )。

If it doesn't have to be reversible, I'd consider just taking the absolute value of the normal hashCode() result (but mapping Integer.MIN_VALUE to something specific, as its absolute value can't be represented as an int).

这篇关于双向String散列函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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