找回密码
 注册
搜索
系统gho:最纯净好用系统下载站投放广告、加入VIP会员,请联系 微信:wuyouceo
查看: 777|回复: 0

闲来无事,做了个生成GUID的HTA……

[复制链接]
发表于 2012-3-2 15:45:33 | 显示全部楼层 |阅读模式
闲来无事,做了个生成GUID的HTA……写INF的TZ们不必再麻烦去写GUID了……
源码:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3.   <head>
  4.     <title>生成并复制GUID</title>
  5.     <META http-equiv="Content-Type" content="text/html; charset=utf-8">
  6.     <meta http-equiv="Content-Language" content="zh-cn">
  7.     <script language="javascript" type="text/javascript">
  8. /* A JavaScript implementation of GUID.
  9. *
  10. * Copyright (C) 2008-2009 HanZhiyang <sea_deep@163.com>
  11. * This library is free.  You can redistribute it and/or modify it.
  12. */
  13. $System = {"Math":{} , "DS":{} , "Type":{}};
  14. //判断类型的工具类。
  15. $System.Type.TypeUtil = function(){throw new Error("$System.Type.TypeUtil is a static class.");}

  16. $System.Type.TypeUtil.isString = function(value){
  17. return ((typeof(value) == "string")||((typeof value == "object") && (value instanceof String)));
  18. }
  19. $System.Type.TypeUtil.isDate = function(value){
  20. return ((typeof value == "object") && (value instanceof Date));
  21. }
  22. $System.Type.TypeUtil.isRegExp = function(value){
  23. return ((typeof value == "object") && (value instanceof RegExp));
  24. }
  25. $System.Type.TypeUtil.isArray = function(value){
  26. return ((typeof value == "object") && (value instanceof Array));
  27. }
  28. $System.Type.TypeUtil.isNumber = function(value){
  29. return !isNaN(value);
  30. }
  31. $System.Type.TypeUtil.isBoolean = function(value){
  32. return ((typeof(value) == "boolean")||((typeof value == "object") && (value instanceof Boolean)));
  33. }
  34. $System.Type.TypeUtil.isFunction = function(value){
  35. return ((typeof value == "function")||((typeof value == "object") && (value instanceof Function)));
  36. }
  37. $System.Type.TypeUtil.isInteger = function(value){
  38. return ($System.Type.TypeUtil.isNumber(value) && (Math.round(value) == value));
  39. }
  40. $System.Type.TypeUtil.isFloat = function(value){
  41. if($System.Type.TypeUtil.isString(value)){
  42.   return false;
  43. }
  44. return ($System.Type.TypeUtil.isNumber(value) && (Math.round(value) != value));
  45. }
  46. $System.Type.TypeUtil.isError = function(value){
  47. return ((typeof value == "object") && (value instanceof Error));
  48. }


  49. //哈希表类
  50. $System.DS.HashTable = function(){
  51. this.o = {};
  52. for(key in this.o){
  53.   delete this.o[key];
  54. }
  55. this.size = 0;
  56. }
  57. //获取元素数目
  58. $System.DS.HashTable.prototype.getSize = function(){
  59. return this.size;
  60. }
  61. //添加键值对
  62. $System.DS.HashTable.prototype.set = $System.DS.HashTable.prototype.add = function(key , value){
  63. if(!(key in this.o)){
  64.   this.size++;
  65. }
  66. this.o[key] = {"v":value};

  67. }
  68. //取值
  69. $System.DS.HashTable.prototype.get = function(key){
  70. if(key in this.o){
  71.   return this.o[key]["v"];
  72. }
  73. return null;
  74. }
  75. //检查值是否存在
  76. $System.DS.HashTable.prototype.hasThisKey = function(key){
  77. return (key in this.o);
  78. }
  79. //取值相同的键名数组
  80. $System.DS.HashTable.prototype.getKeyListByValue = function(value){
  81. var list = [];
  82. for(key in this.o){
  83.   if(this.o[key]["v"] == value){
  84.    list.push(key);
  85.   }
  86. }
  87. return list;
  88. }
  89. //删除
  90. $System.DS.HashTable.prototype.remove = function(key){
  91. if(key in this.o){
  92.   this.size--;
  93.   delete this.o[key]["v"];
  94.   delete this.o[key];
  95. }
  96. }
  97. //删除所有
  98. $System.DS.HashTable.prototype.removeAll = function(){
  99. for(key in this.o){
  100.   delete this.o[key]["v"];
  101.   delete this.o[key];
  102. }
  103. this.size = 0;
  104. }

  105. $System.DS.HashTable.prototype.getO = function(){
  106. return this.o;
  107. }
  108. //当str1="=",str2="&"的时候,组织查询字符串
  109. $System.DS.HashTable.prototype.join = function(str1 , str2){
  110. var a = new Array();
  111. for(key in this.o){
  112.   a.push(key + str1 + this.get(key));
  113. }
  114. return a.join(str2);
  115. }
  116. //销毁
  117. $System.DS.HashTable.dispose = function(oht){
  118. oht.dispose();
  119. oht = null;

  120. }
  121. //销毁
  122. $System.DS.HashTable.prototype.dispose = function(){
  123. this.removeAll();
  124. }

  125. //整数工具类(还有大整数类,加密类,md5类等等以后慢慢贴上来)
  126. $System.Math.IntUtil = function(){throw new Error("$System.Math.IntUtil is a static class.")}
  127. //十进制到二机制转换,注释里面的方式也可以用,下同。依赖js中的整数范围,不可靠。
  128. $System.Math.IntUtil.dec2Bin = function(decInt){
  129. /*
  130. var m = 0;
  131. var s = 0;
  132. var binStr = "";
  133. if(decInt == 0){
  134.   return "0";
  135. }
  136. while(decInt != 0){
  137.   m = decInt % 2;
  138.   decInt = (decInt - m) /2;
  139.   binStr = m + binStr;
  140. }
  141.   return binStr;
  142. */
  143. return decInt.toString(2);
  144. }
  145. $System.Math.IntUtil.hexNumArray = new Array("0" , "1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "A" , "B" , "C" , "D" , "E" , "F");
  146. //十进制到十六进制转换。
  147. $System.Math.IntUtil.dec2Hex = function(decInt){
  148. //var hexNumArray = new Array("0" , "1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "A" , "B" , "C" , "D" , "E" , "F");
  149. /*
  150. var hexNumArray = $System.Math.IntUtil.hexNumArray;
  151. var m = 0;
  152. var s = 0;
  153. var hexStr = "";
  154. if(decInt == 0){
  155.   return "0";
  156. }
  157. while(decInt != 0){
  158.   m = decInt % 16;
  159.   decInt = (decInt - m) / 16;
  160.   hexStr = hexNumArray[m] + hexStr;
  161. }
  162.   return hexStr;
  163. */
  164. return decInt.toString(16).toUpperCase();
  165. }
  166. $System.Math.IntUtil.numHexArray = {"0" : "0" , "1" : "1" , "2" : "2" , "3" : "3" , "4" : "4" , "5" : "5" , "6" : "6" , "7" : "7" , "8" : "8" , "9" : "9" , "A" : "10" , "B" : "11" , "C" : "12" , "D" : "13" , "E" : "14" , "F" : "15"};
  167. //十六进制到十进制转换。
  168. $System.Math.IntUtil.hex2Dec = function(oriHexStr){
  169. //var numHexArray = {"0" : "0" , "1" : "1" , "2" : "2" , "3" : "3" , "4" : "4" , "5" : "5" , "6" : "6" , "7" : "7" , "8" : "8" , "9" : "9" , "A" : "10" , "B" : "11" , "C" : "12" , "D" : "13" , "E" : "14" , "F" : "15"};
  170. /*
  171. var numHexArray = $System.Math.IntUtil.numHexArray;
  172. hexStr = oriHexStr.toUpperCase();
  173. var iniArray = hexStr.split("");
  174. var numArray = iniArray.reverse();
  175. var len = numArray.length;
  176. var decInt = 0;
  177. var num = 0;
  178. for(var i = 0 ; i < len ; i ++){
  179.   num += parseInt(numHexArray[numArray[i]]) * Math.pow(16 , i);
  180. }
  181. return num;
  182. */
  183. return parseInt(oriHexStr , 16);
  184. }
  185. //二进制到十进制转换。
  186. $System.Math.IntUtil.bin2Dec = function(binStr){
  187. /*
  188. var iniArray = binStr.split("");
  189. var numArray = iniArray.reverse();
  190. var len = numArray.length;
  191. var decInt = 0;
  192. var num = 0;
  193. for(var i = 0 ; i < len ; i ++){
  194.   num += parseInt(numArray[i]) * Math.pow(2 , i);
  195. }
  196. return num;
  197. */
  198. return parseInt(binStr , 2);
  199. }
  200. $System.Math.IntUtil.binHexArray = {"0000":"0","0001":"1","0010":"2","0011":"3","0100":"4","0101":"5","0110":"6","0111":"7","1000":"8","1001":"9","1010":"A","1011":"B","1100":"C","1101":"D","1110":"E","1111":"F"};
  201. //二进制到十六进制转换,这个很可靠,因为是字符串,不受整数类型范围限制。
  202. $System.Math.IntUtil.bin2Hex = function(binStr){
  203. var n = 0;
  204. while(binStr.charAt(n) == "0"){
  205.   n++;
  206. }
  207. if(n > 0){
  208.   binStr = binStr.substring(n , binStr.length);
  209. }
  210. var i , hexStrArray = [] , binHexArray = $System.Math.IntUtil.binHexArray , len = binStr.length;
  211. for(i = len ; i > 3 ; i =  i - 4){
  212.   hexStrArray.push(binHexArray[binStr.substring(i - 4 , i)]);
  213. }
  214. hexStrArray.push(binHexArray[$System.Math.IntUtil.fillZero(binStr.substring(i - 4 , i) , 4)]);
  215. return hexStrArray.reverse().join("");
  216. }
  217. $System.Math.IntUtil.hexBinArray = {"0":"0000","1":"0001","2":"0010","3":"0011","4":"0100","5":"0101","6":"0110","7":"0111","8":"1000","9":"1001","A":"1010","B":"1011","C":"1100","D":"1101","E":"1110","F":"1111"};
  218. //十六进制到二进制转换。
  219. $System.Math.IntUtil.hex2Bin = function(hexStr){
  220. var n = 0;
  221. while(hexStr.charAt(n) == "0"){
  222.   n++;
  223. }
  224. if(n > 0){
  225.   hexStr = hexStr.substring(n , hexStr.length);
  226. }
  227. var hexStrArray = hexStr.split("") , len = hexStrArray.length , binStrArray = [] , hexBinArray = $System.Math.IntUtil.hexBinArray;
  228. for(var i = 0 ; i < len ; i++){
  229.   binStrArray.push(hexBinArray[hexStrArray[i]]);
  230. }
  231. return binStrArray.join("");
  232. }

  233. //补零的方法。
  234. $System.Math.IntUtil.fillZero = function(str , stdLength , allowEmpty){
  235. if(str == "" && !allowEmpty){
  236.   return "";
  237. }
  238. c = stdLength - str.length;
  239. var str0 = ""
  240. for(var i = 0 ; i < c ; i++){
  241.   str0 = "0" + str0;
  242. }
  243. return  str0 + str;
  244. }
  245. /*
  246. $System.Math.IntUtil.__GUIDPool = {};
  247. for(key in $System.Math.IntUtil.__GUIDPool){
  248. delete $System.Math.IntUtil.__GUIDPool[key];
  249. }
  250. $System.Math.IntUtil.__GUIDCheckAndAdd = function(guid){
  251. if(!$System.Math.IntUtil.__GUIDPool[guid]){
  252.   return $System.Math.IntUtil.__GUIDPool[guid] = true;
  253. }
  254. else{
  255.   return false;
  256. }
  257. }
  258. */
  259. //guid池,避免重复guid。
  260. $System.Math.IntUtil.__GUIDPool = new $System.DS.HashTable();
  261. $System.Math.IntUtil.__GUIDCheckAndAdd = function(guid){
  262. if(!$System.Math.IntUtil.__GUIDPool.hasThisKey(guid)){
  263.   $System.Math.IntUtil.__GUIDPool.set(guid , true);
  264.   return true;
  265. }
  266. else{
  267.   return false;
  268. }
  269. }
  270. //不补零的guid。
  271. $System.Math.IntUtil.__genGUID = function(){
  272. var part1 = $System.Math.IntUtil.dec2Hex(Math.floor(Math.random() * ($System.Math.IntUtil.hex2Dec("FFFFFFFF") + 1)));
  273. var part2 = $System.Math.IntUtil.dec2Hex(Math.floor(Math.random() * 65536));
  274. var str4 = $System.Math.IntUtil.fillZero($System.Math.IntUtil.dec2Bin(Math.floor(Math.random() * 256)));
  275. var part3 = $System.Math.IntUtil.dec2Hex(Math.floor(Math.random() * 65536));
  276. //var part4 = $System.Math.IntUtil.dec2Hex($System.Math.IntUtil.bin2Dec(str4.substr(0 , 5) + "01" + str4.substr(7 , 1)));
  277. var part4 = $System.Math.IntUtil.bin2Hex(str4.substr(0 , 5) + "01" + str4.substr(7 , 1));
  278. var part5 = $System.Math.IntUtil.dec2Hex(Math.floor(Math.random() * 256));
  279. var part6 = $System.Math.IntUtil.dec2Hex(Math.floor(Math.random() * ($System.Math.IntUtil.hex2Dec("FFFFFFFFFFFF") + 1)));
  280. var guid = "GUID_" + part1 + "_" + part2 + "_" + part3 + "_" + part4 + part5 + "_" + part6;
  281. if($System.Math.IntUtil.__GUIDCheckAndAdd(guid)){
  282.   return guid;
  283. }
  284. else{
  285.   return $System.Math.IntUtil.__genGUID();
  286. }
  287. }
  288. //补了零的标准的guid格式(当然多了个“guid_”前缀,以及使用“_”代替“-”)。
  289. $System.Math.IntUtil.genGUID = function(){
  290. var part1 = $System.Math.IntUtil.dec2Hex(Math.floor(Math.random() * ($System.Math.IntUtil.hex2Dec("FFFFFFFF") + 1)));
  291. var part2 = $System.Math.IntUtil.dec2Hex(Math.floor(Math.random() * 65536));
  292. var str4 = $System.Math.IntUtil.fillZero($System.Math.IntUtil.dec2Bin(Math.floor(Math.random() * 256)));
  293. var part3 = $System.Math.IntUtil.dec2Hex(Math.floor(Math.random() * 65536));
  294. //var part4 = $System.Math.IntUtil.dec2Hex($System.Math.IntUtil.bin2Dec(str4.substr(0 , 5) + "01" + str4.substr(7 , 1)));
  295. var part4 = $System.Math.IntUtil.bin2Hex(str4.substr(0 , 5) + "01" + str4.substr(7 , 1));
  296. var part5 = $System.Math.IntUtil.dec2Hex(Math.floor(Math.random() * 256));
  297. var part6 = $System.Math.IntUtil.dec2Hex(Math.floor(Math.random() * ($System.Math.IntUtil.hex2Dec("FFFFFFFFFFFF") + 1)));
  298. part1 = $System.Math.IntUtil.fillZero(part1 , 8);
  299. part2 = $System.Math.IntUtil.fillZero(part2 , 4);
  300. part3 = $System.Math.IntUtil.fillZero(part3 , 4);
  301. part4 = $System.Math.IntUtil.fillZero(part4 , 2);
  302. part5 = $System.Math.IntUtil.fillZero(part5 , 2);
  303. part6 = $System.Math.IntUtil.fillZero(part6 , 12);
  304. var guid = "{" + part1 + "-" + part2 + "-" + part3 + "-" + part4 + part5 + "-" + part6 + "}";
  305. if($System.Math.IntUtil.__GUIDCheckAndAdd(guid)){
  306.   return guid;
  307. }
  308. else{
  309.   return $System.Math.IntUtil.genGUID();
  310. }
  311. }
  312. //V4的guid。
  313. $System.Math.IntUtil.genGUIDV4 = function(){
  314. var part1 = $System.Math.IntUtil.dec2Hex(Math.floor(Math.random() * ($System.Math.IntUtil.hex2Dec("FFFFFFFF") + 1)));
  315. var part2 = $System.Math.IntUtil.dec2Hex(Math.floor(Math.random() * 65536));
  316. var str3 = $System.Math.IntUtil.fillZero($System.Math.IntUtil.dec2Bin(Math.floor(Math.random() * 65536)) , 16);
  317. //var part3 = $System.Math.IntUtil.dec2Hex($System.Math.IntUtil.bin2Dec(str3.substr(0 , 12) + "0100"));
  318. var part3 = $System.Math.IntUtil.bin2Hex(str3.substr(0 , 12) + "0100");
  319. var str4 = $System.Math.IntUtil.fillZero($System.Math.IntUtil.dec2Bin(Math.floor(Math.random() * 256)));
  320. //var part4 = $System.Math.IntUtil.dec2Hex($System.Math.IntUtil.bin2Dec(str4.substr(0 , 5) + "01" + str4.substr(7 , 1)));
  321. var part4 = $System.Math.IntUtil.bin2Hex(str4.substr(0 , 5) + "01" + str4.substr(7 , 1));
  322. var part5 = $System.Math.IntUtil.dec2Hex(Math.floor(Math.random() * 256));
  323. var part6 = $System.Math.IntUtil.dec2Hex(Math.floor(Math.random() * ($System.Math.IntUtil.hex2Dec("FFFFFFFFFFFF") + 1)));
  324. part1 = $System.Math.IntUtil.fillZero(part1 , 8);
  325. part2 = $System.Math.IntUtil.fillZero(part2 , 4);
  326. part3 = $System.Math.IntUtil.fillZero(part3 , 4);
  327. part4 = $System.Math.IntUtil.fillZero(part4 , 2);
  328. part5 = $System.Math.IntUtil.fillZero(part5 , 2);
  329. part6 = $System.Math.IntUtil.fillZero(part6 , 12);
  330. var guid = "{" + part1 + "-" + part2 + "-" + part3 + "-" + part4 + part5 + "-" + part6 + "}";
  331. if($System.Math.IntUtil.__GUIDCheckAndAdd(guid)){
  332.   return guid;
  333. }
  334. else{
  335.   return $System.Math.IntUtil.genGUIDV4();
  336. }
  337. }
  338. //获取一个guid长度的随机字符串。
  339. $System.Math.IntUtil.__genRandomString = function(){
  340. var part1 = $System.Math.IntUtil.dec2Hex(Math.floor(Math.random() * ($System.Math.IntUtil.hex2Dec("FFFFFFFF") + 1)));
  341. var part2 = $System.Math.IntUtil.dec2Hex(Math.floor(Math.random() * 65536));
  342. var str4 = $System.Math.IntUtil.fillZero($System.Math.IntUtil.dec2Bin(Math.floor(Math.random() * 256)));
  343. var part3 = $System.Math.IntUtil.dec2Hex(Math.floor(Math.random() * 65536));
  344. //var part4 = $System.Math.IntUtil.dec2Hex($System.Math.IntUtil.bin2Dec(str4.substr(0 , 5) + "01" + str4.substr(7 , 1)));
  345. var part4 = $System.Math.IntUtil.bin2Hex(str4.substr(0 , 5) + "01" + str4.substr(7 , 1));
  346. var part5 = $System.Math.IntUtil.dec2Hex(Math.floor(Math.random() * 256));
  347. var part6 = $System.Math.IntUtil.dec2Hex(Math.floor(Math.random() * ($System.Math.IntUtil.hex2Dec("FFFFFFFFFFFF") + 1)));
  348. part1 = $System.Math.IntUtil.fillZero(part1 , 8);
  349. part2 = $System.Math.IntUtil.fillZero(part2 , 4);
  350. part3 = $System.Math.IntUtil.fillZero(part3 , 4);
  351. part4 = $System.Math.IntUtil.fillZero(part4 , 2);
  352. part5 = $System.Math.IntUtil.fillZero(part5 , 2);
  353. part6 = $System.Math.IntUtil.fillZero(part6 , 12);
  354. var guid = part1 + part2 + part3 + part4 + part5 +  part6;
  355. return guid;
  356. }
  357. //获取任意长度的随机字符串。
  358. $System.Math.IntUtil.genRandomString = function(len){
  359. if(!($System.Type.TypeUtil.isInteger(len) && len > 0)){
  360.   len = 2;
  361. }
  362. var rnd = "";
  363. for(var i = 0 ; i < len ; i++){
  364.   rnd += $System.Math.IntUtil.__genRandomString();
  365. }
  366. return "RND_" + rnd;
  367. }

  368.             function __copy(obj)
  369.             {
  370.               var bcd = $System.Math.IntUtil.genGUID();
  371.               var kkk = $System.Math.IntUtil.genGUIDV4();
  372.               obj.value = "GUID:" + bcd + "\n" + "GUIDV4:" + kkk;
  373.               var abc = window.clipboardData.setData("text","GUID:" + bcd + "\r\n" + "GUIDV4:" + kkk);
  374.                alert("已经复制到剪切板!");
  375.             }
  376.     </script>
  377.   </head>
  378.   <body>
  379.     <form id="form1" name="form1" method="post" action="">
  380.       <div align="right">
  381.          <table align="center" border>
  382.          <tr>          <td align="center">
  383.          <textarea id="text1" name="text1" cols="58" rows="18" readonly></textarea>
  384.          <br>
  385.          <p>
  386.          <br>
  387.          </p>
  388.          <p align="center">
  389.          <input value="生成并复制GUID" onClick="__copy(text1)" type="button">
  390.          <br>
  391.          <br>
  392.   
  393.                <br>
  394.              </p>
  395.             </td>
  396.           </table>
  397.         </div>
  398.     </form>
  399.   </body>
  400. </html>
复制代码

懒人下载:
生成并复制GUID.rar (3.81 KB, 下载次数: 36)
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|捐助支持|无忧启动 ( 闽ICP备05002490号-1|闽公网安备35020302032614号 )

GMT+8, 2026-9-1 17:35

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表