从长字符串创建短字符串 [英] create short string from long string

查看:202
本文介绍了从长字符串创建短字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的字符串很长(大约50个字符),需要插入数据库中.
但是,数据库中的字段长度只有10个字符(我无法更改它!)
我知道应该有一种哈希或加密字符串的方法,以便可以缩短它,然后以后我想再次使用字符串时应该解密.

最好的方法是什么?
请注意,安全性并不重要,我需要将其转换为简短的另一组字符,并且可以将其转换回原始字符串.
我正在寻找一个C#解决方案.

I have a long string (around 50 characters)and need to insert into database.
however, the length of field in database is only 10 characters (i cannot change it !!)
i know there should be a way to hash or encrypt the string so it can be shorten, then later i should decrypt when i want to use string again.

what is the best way to do this?
please note security is not important and all i need to convert it to another set of characters which are short and i can convert it back to original string.
I am looking for a c# solution.

appreciate your response in advance.

推荐答案

0)您不能接受50个字符并将其设置为10.这是不可能的.

1)哈希无法反转,因此哈希将不起作用.

2)加密的字符串几乎总是至少16个字符.

3)您始终可以创建一个具有自动递增的id列和一个x字符长的字符串字段(我建议使用255个字符)的新表,并将数据存储在新表中.然后,您可以获取为其创建的ID,并将该ID存储在主表的10个字符的列中,从而在两个表之间建立代理关系.之后,您可以根据存储在主表中的ID来在新表中检索适当的数据.

编辑==================

由于您无法以任何方式修改数据库,因此,您唯一可行的解​​决方案是将50个字符的数据存储在磁盘上的文件中(我自己将XML与它一起使用),并执行相同的关系操作在上面谈论过.当然,除非另一个用户也必须访问数据,否则这将不起作用,除非XML文件位于共享存储资源上.

您还可以更改数据来源的格式,并将输入限制为10个字符.

如果数据来自外部来源,则只需使用提交的前10个字符,然后丢弃其余字符即可.
0) You can''t take 50 characters, and make it 10. That''s impossible.

1) A hash cannot be reversed, so hashing won''t work.

2) An encrypted string will pretty much ALWAYS be a minimum of 16 characters.

3) You could always create a new table with an id column that auto-increments and aa stringfield that is x-characters long (I recommend 255 characters), and store the data in the new table. You can then get the ID that was created for it, and stored the ID in the 10-character column of your main table, thus setting up a proxy relationship between the two tables. After that, you can hit retrieve the appropriate data in the new table based on the id stored in the main table.

EDIT ==================

Since you can''t modify the database in any way, your only other viable solution is to store the 50-character data in a file on the disk (I''d go with XML myself), and do the same relationship thing I talked about above. Of course, this won''t work if another user has to get to the data as well, unless the XML file is on a shared storage resource.

You could also change the form where the data is coming from, and restrict the input to 10 characters.

If the data is coming from an outside source, just use the first 10 characters submitted, and throw the rest away.


您需要创建自己的算法来执行此操作. 50到10并非易事.您的算法就像,如果单词中包含重复字母,则可以将其与重复次数一起显示为单个字母.
就像这里的一个简单例子:如果有一个像AAAABBTTTT这样的词,您可以将其写为A3B2T4.你可以看到没有.的字母已减少.但这是一个非常简单的例子.
这是另一个例子,可以说BAT经常出现,您可以用"@"代替.同样,您可以尝试创建自己的方法或规则来执行此操作.希望我能在一定程度上为您提供帮助.
祝你好运.
You need to create your own algorithm to do this. 50 to 10 is not going to be easy. Your algorithm can be like, if you have repetitive letters in a word, you can instead present it as a single letter together with the number of repetitions.
Like here''s a simple ex: if there''s a word like AAAABBTTTT, you write it as A3B2T4. You can see the no. of letters have been reduced. But it was a very simple example.
Here is another ex, lets say a word BAT occurs frequently, you can substitute that with a "@" character. Likewise you can try to create your own methods or rules to do it. I hope I was able to help you to an extent.
Good luck.


这篇关于从长字符串创建短字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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