Chinaunix首页 | 论坛 | 博客
  • 博客访问: 536501
  • 博文数量: 142
  • 博客积分: 2966
  • 博客等级: 少校
  • 技术积分: 1477
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-07 22:37
文章分类

全部博文(142)

文章存档

2013年(3)

2012年(21)

2011年(53)

2010年(33)

2009年(32)

分类: Python/Ruby

2011-06-10 17:58:56

PHP实现:
  1. function str2hex($string)
  2.  {
  3.        $hex="";
  4.        for($i=0;$i<strlen($string);$i++)
  5.        $hex.=dechex(ord($string[$i]));
  6.        //$hex=strtoupper($hex);
  7.        return $hex;
  8.  }
  9. function hex2str($hex)
  10.   {
  11.        $string="";
  12.        for($i=0;$i<strlen($hex)-1;$i+=2)
  13.        $string.=chr(hexdec($hex[$i].$hex[$i+1]));
  14.        return $string;
  15.  }

 

Python实现:

 

  1. def hex2str(s):
  2.     if s[:2] == '0x' or s[:2] == '0X':
  3.         s = s[2:]
  4.     res = ""
  5.     for i in range(0,len(s),2):
  6.         hex_dig = s[i:i+2]
  7.         res+=(chr(int(hex_dig,base=16)))
  8.     return res

  9. def str2hex(string):
  10.     res = ""
  11.     for s in string:
  12.         hex_dig = hex(ord(s))[2:]
  13.         if len(hex_dig)==1:
  14.             hex_dig = "0"+hex_dig
  15.         res+=hex_dig
  16.     return res
阅读(768) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~