`
zzjjzzgggg
  • 浏览: 124298 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

encode and decode

阅读更多
package test;

/**
 * 
 * @author jzzhao
 * @date 2011-3-18 下午09:30:46
 */
class EscapeUnescape {
	public static String escape(String src) {
		int i;
		char j;
		StringBuffer tmp = new StringBuffer();
		tmp.ensureCapacity(src.length() * 6);

		for (i = 0; i < src.length(); i++) {

			j = src.charAt(i);

			if (Character.isDigit(j) || Character.isLowerCase(j)
					|| Character.isUpperCase(j))
				tmp.append(j);
			else if (j < 256) {
				tmp.append("\\");
				if (j < 16)
					tmp.append("0");
				tmp.append(Integer.toString(j, 16));
			} else {
				tmp.append("\\u");
				tmp.append(Integer.toString(j, 16));
			}
		}
		return tmp.toString();
	}

	public static String unescape(String src) {
		StringBuffer tmp = new StringBuffer();
		tmp.ensureCapacity(src.length());
		int lastPos = 0, pos = 0;
		char ch;
		while (lastPos < src.length()) {
			pos = src.indexOf("\\", lastPos);
			if (pos == lastPos) {
				if (src.charAt(pos + 1) == 'u') {
					ch = (char) Integer.parseInt(
							src.substring(pos + 2, pos + 6), 16);
					tmp.append(ch);
					lastPos = pos + 6;
				} else {
					ch = (char) Integer.parseInt(
							src.substring(pos + 1, pos + 3), 16);
					tmp.append(ch);
					lastPos = pos + 3;
				}
			} else {
				if (pos == -1) {
					tmp.append(src.substring(lastPos));
					lastPos = src.length();
				} else {
					tmp.append(src.substring(lastPos, pos));
					lastPos = pos;
				}
			}
		}
		return tmp.toString();
	}

	public static void main(String[] args) {
		//String tmp = "种好";
		//System.out.println("testing escape : " + tmp);
		//tmp = escape(tmp);
		//System.out.println(tmp);
//		System.out.println("testing unescape :" + tmp);
		System.out.println(unescape("\u66b4\u6da8\u8fc7\u540e\u662f\u8d44\u672c"));
	}
}

分享到:
评论

相关推荐

    Video Encode and Decode GPU Support Matrix.xlsx

    Video Encode and Decode GPU Support Matrix.xlsx

    DVI encode and decode source code for FPGA

    DVI 1.0 encode and decode source code, write with verilog, with simulation project, synplify project and all soure code. They're have been running on xilinx spartan3A FPGA. Can be used in real project...

    HDB3 ENCODE AND DECODE

    HDB3 ENCODE AND DECODE,详细设计与实现,参考一下下啊

    labview DTMF encode and decode

    using labview to encode DTMF tone and play through sound card and record DTMF tone from sound card and decode.

    Turbo encode and decode

    Turbo编码和解码是无线通信领域中的关键技术,特别是在LTE(Long Term Evolution)系统中,它对数据传输的高效性和可靠性起着至关重要的作用。本文将深入探讨Turbo编码的工作原理、编码过程、解码策略以及在LTE系统...

    jpeg encode and decode

    在IT领域,JPEG(Joint Photographic Experts Group)是一种广泛使用的有损图像压缩标准,它能够有效地减少图像文件的大小,适合于在网络上传输和存储。本文将深入探讨JPEG编码解码过程,以及如何使用C/C++语言实现...

    bch encode and decode

    【 BCH 编码与解码详解】 BCH(Bose-Chaudhuri-Hocquenghem)编码是一种广泛应用于通信、存储系统以及错误检测和纠正的纠错编码技术。由印度科学家Raj Chandra Bose、Dipak Chaudhuri和Joseph Hocquenghem在29世纪...

    Base64 encode and decode tool-crx插件

    语言:English (United States) 简单的base64编码和解码工具 将原始文本转换为base64编码文本的基本工具。

    Base64编码和解码「Base64 Encode and Decode」-crx插件

    快速编码和解码Base64字符串。 Chrome扩展程序是一个简单的Base64编码器和解码器。 通过选择并右键单击所选内容,可对任何页面中的内联字符串进行编码和解码。 编码或解码字符串,使其符合Base64数据编码规范(RFC ...

    ZXing2.1 条码/二维码扫描识别

    However the project can be used to encode and decode barcodes on desktops and servers as well. We currently support these formats: UPC-A and UPC-E EAN-8 and EAN-13 Code 39 Code 93 Code 128 ITF ...

    Alvas.Audio 音频转换组件

    主要是用来转化各种音频格式的... 内含发布到服务器无法转换音频的参考博客地址. Alvas.Audio allows C# and VB.Net developers ... Encode and decode Dialogic . Alvas.Audio can convert audio and record audio.

    BrainTools

    You can run Brainfuck programs and encode/decode Brainfuck code using Brainloller and Braincopter. Plus, given that Brainloller produces very small images, the utility features an image resizer. ...

    JSONLab: a toolbox to encode/decode JSON files 源码

    A toolbox to encode/decode JSON/UBJSON/MessagePack files in MATLAB/Octave JSONLab is a free and open-source JSON/UBJSON/MessagePack encoder and decoder written in the native MATLAB language. It can be...

    Python库参考手册(Python Library Reference).zip

    第一章内建对象Built-In Objects 1 ... 7.1 base64 — 编码和解码MIME Base64数据Encode and decode MIME base64 data . . . . . . . 13 7.2 xml.dom — The Document Object Model 文档对象模式API

    jQuery JSON with PHP json_encode and json_decode

    "jQuery JSON with PHP json_encode and json_decode"这个主题涉及到如何利用JSON(JavaScript Object Notation)这一轻量级的数据交换格式,结合PHP的`json_encode`和`json_decode`函数,实现前后端之间的数据通信...

    conv2047:RFC 2047 Unicode 电子邮件标头字段转换器

    conv2047 ...to decode or encode them. Operation modes: -d decode (MIME-Header --&gt; UTF-8) -e encode (UTF-8 --&gt; MIME-Header) Options: -c concatenate multiple output lines into 1

    Teach Your Kids to Code: A Parent- Friendly Guide to Python Programming 2015

    Write programs to encode and decode messages, play Rock-Paper-Scissors, and calculate how tall someone is in Ping-Pong balls Create fun, playable games like War, Yahtzee, and Pong Add interactivity, ...

Global site tag (gtag.js) - Google Analytics