`
thingkau
  • 浏览: 74553 次
  • 性别: Icon_minigender_1
  • 来自: 泉州
社区版块
存档分类
最新评论

字节数增倍编码

F# 
阅读更多
byte能表示从0~127(128个)字符,字符表示最多占用前6位,即111111=127,前两位基本上属于多余。在网上传输时,显然用base64比较节省带宽。
不过,字节数增倍编码编码速度可能比较快,虽然占用大的带宽。
public class GetDoubleBytes {

	public static void main(String[] args){
		String ds = getDoubleBytesStr("张".getBytes());
		System.out.println(ds);
	}

	/**
	 * 主调方法
	 * @param s
	 * @return
	 */
	public static String getDoubleBytesStr(byte[] bs){
		StringBuilder sb = new StringBuilder();
		for(int i = 0; i < bs.length; i++){
			sb.append(padHexString(toHexString(bs[i])));
		}
		return sb.toString();
	}

	/** Add '0' in front of one byte hexa character to make it 2 bytes. */
	public static String padHexString(String hexNum) {
		if (hexNum.length() < 2) {
			return "0" + hexNum;
		}
		return hexNum;
	}
	
	/**
	 * 用十六进制0-9,a-f表示一个byte,并以字符串的形式返回
	 * 16进去,"10"出来
	 * 15进去,"f"出来
	 */
	public static String toHexString(byte b){
		return Integer.toHexString(0xff & b);
	}

}


结果打印:d5c5

另法:
public class GetDoubleBytes2 {
	public final static String[] _hexLookupTable = {"00","01","02","03","04","05","06","07","08","09","0a","0b","0c","0d","0e","0f","10","11","12","13","14","15","16","17","18","19","1a","1b","1c","1d","1e","1f","20","21","22","23","24","25","26","27","28","29","2a","2b","2c","2d","2e","2f","30","31","32","33","34","35","36","37","38","39","3a","3b","3c","3d","3e","3f","40","41","42","43","44","45","46","47","48","49","4a","4b","4c","4d","4e","4f","50","51","52","53","54","55","56","57","58","59","5a","5b","5c","5d","5e","5f","60","61","62","63","64","65","66","67","68","69","6a","6b","6c","6d","6e","6f","70","71","72","73","74","75","76","77","78","79","7a","7b","7c","7d","7e","7f","80","81","82","83","84","85","86","87","88","89","8a","8b","8c","8d","8e","8f","90","91","92","93","94","95","96","97","98","99","9a","9b","9c","9d","9e","9f","a0","a1","a2","a3","a4","a5","a6","a7","a8","a9","aa","ab","ac","ad","ae","af","b0","b1","b2","b3","b4","b5","b6","b7","b8","b9","ba","bb","bc","bd","be","bf","c0","c1","c2","c3","c4","c5","c6","c7","c8","c9","ca","cb","cc","cd","ce","cf","d0","d1","d2","d3","d4","d5","d6","d7","d8","d9","da","db","dc","dd","de","df","e0","e1","e2","e3","e4","e5","e6","e7","e8","e9","ea","eb","ec","ed","ee","ef","f0","f1","f2","f3","f4","f5","f6","f7","f8","f9","fa","fb","fc","fd","fe","ff"};

	public static void main(String[] args){
		String ds = getDoubleBytesStr("张".getBytes());
		System.out.println(ds);
	}

	/**
	 * 主调方法
	 * @param s
	 * @return
	 */
	public static String getDoubleBytesStr(byte[] bs){
		StringBuilder sb = new StringBuilder();
		for(int i = 0; i < bs.length; i++){
			sb.append(_hexLookupTable[ 0xff & bs[i] ]);
		}
		return sb.toString();
	}

}


画蛇添足:
你可能需要如下方法
	/**
	 * 生成_hexLookupTable的字符串
	 * @return
	 */
	public static String gen_hexLookupTable(){
		StringBuilder sb = new StringBuilder();
		sb.append("public final static String[] _hexLookupTable = {");
		for(int i=0;i<256;i++){
			
			sb.append("\"");
			
			sb.append(padHexString(toHexString((byte)i)));
			
			sb.append("\"");
			sb.append(",");
			
		}
		sb.deleteCharAt(sb.length()-1);
		sb.append("};");
		return sb.toString();
	}


以下是解码:
public class DoubleBytesDecoder {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		String s = "张3";
		
		String es = GetDoubleBytes.getDoubleBytesStr(s.getBytes());
		
		System.out.println(es);//打印d5c533
		
		System.out.println(new String(decoderDoubleBytes(es)));//打印 "张3"
		
	}
	
	/**
	 * 将双倍字节字符串解码成单倍字节码数组
	 * @param encodedStr
	 * @return
	 * @throws Exception
	 */
	public static byte[] decoderDoubleBytes(String encodedStr) throws Exception {
		
		if(encodedStr.length()%2!=0){
			throw new Exception("无效的编码");
		}
		byte[] bs = new byte[encodedStr.length()/2];
		
		for(int i=0;i<bs.length; i++){
			bs[i]=(byte)Integer.parseInt(encodedStr.substring(2*i, 2*(i+1)), 16);
		}
		
		return bs;
	}

}
分享到:
评论

相关推荐

    huffman编码的8种实现方式

    - **字节处理**:编码可能不是8位的整数倍,因此需要进行字节填充或字节编码,确保输出文件的字节对齐。 4. **解码文件**: - **读取编码**:从编码文件中逐字节读取,根据编码字典解码成字符。 - **恢复文本**...

    java使用Hex编码解码实现Aes加密解密功能示例

    在这个示例中,密钥首先是以字符串形式存在,然后通过`Hex.decodeHex()`方法将其从Hex编码转换回原始的字节数组,这是因为AES算法需要字节形式的密钥。如果密钥是经过Hex编码的,那么在使用之前必须进行解码。 接...

    行业分类-设备装置-具有三路字节交错纠错码的位交错1617比率调制码.zip

    1617比率调制码是一种编码率,意味着编码后的输出比特数(17)是编码前输入比特数(16)的1.0625倍。这种高效率的编码方式可以在不显著增加数据量的情况下提供额外的纠错能力。位交错是将数据位按照特定规则打乱,以...

    基于哈夫曼编码的文本文件压缩与解压缩

    这个过程叫做编码,但需要注意的是,由于编码过程中可能会产生额外的位(例如,非整数倍的字节),所以编码后的文件大小可能会比原文件大。编码后的文件可以存储为二进制格式,如`code.dat`。 解压缩的过程则是编码...

    Base64 编码程序源代码

    在编码过程中,如果原始数据不是3的倍数,会在末尾添加0填充位,以达到24位的整数倍。编码后的结果会比原始数据略长,因为每个原始8位字节变成了4个6位的Base64字符,增加了大约33%的长度。 在解码时,Base64字符串...

    Base 64编码的方法

    4. **填充**:如果最后一个分组中的数据少于24位,那么在编码结果的末尾添加一个或多个“=”号来表示填充的字节数。 ### 实际应用案例 Base 64编码在多种场合下都有广泛的应用,其中最常见的是在网络传输中,将二...

    base64编码和解码c++代码

    3. 如果原始数据不是3字节的整数倍,那么在末尾添加0位,直到长度为3字节的倍数。最后不足4个6位组时,使用'='字符进行填充。 C++实现Base64编码: 1. 定义一个查找表,包含Base64字符集的所有字符。 2. 读取输入的...

    base64代码base64代码base64代码

    这种编码方法基于标准的64个字符集,包括大小写字母、数字以及"+"和"/",并在末尾可能添加一个或多个等于号("=")来确保编码结果的长度是8位字节的整数倍。 标题中的“base64代码base64代码base64代码”可能是指涉及...

    matlab代码csma-cobss-matlab:用于MATLAB的流式一致开销字节填充(COBS)

    在内存受限的硬件应用中,流方法需要的内存比块编码方法少得多,块编码方法需要的内存几乎是编码消息大小的两倍。 我开发了这个专门用于我的半双工项目,我在硬件实现之前在 MATLAB 中建模。 我的更快的块编码方法的...

    Base64 - MetaTrader 4程序库.zip

    此外,Base64编码通常会在末尾添加等于号('=')作为填充,以确保编码结果是8位字节的整数倍。 了解和熟练使用`Base64.mq4`中的编码和解码功能,对于MT4的开发者来说至关重要,因为它能够帮助他们创建更复杂、更...

    数字信息摘要常见算法1

    由于字符集包含64个字符,每个字符代表6位二进制数,所以编码后的数据长度大约是原数据的4/3倍,不足位数用'='进行填充。 3. **HASH算法**: - HASH算法,也称为散列算法,它的主要特点是将任意长度的信息转化为...

    从字符串中截取等长字节的Java代码

    中文字符在计算机内部是以双字节的形式存储的,一个中文字符占用的字节数是英文字符的两倍。因此,当我们需要截取等长字节的字符串时,不能简单地按照字符数进行截取,而需要考虑每个字符对应的字节数。这个问题在...

    基于Matlab的密码编码教学思考与实践.pdf

    通过Matlab编程,可以动态演示点的加法和倍点运算,加深学生对ECC算法的理解。 文章还指出,结合Matlab进行密码编码教学,不仅解决了理论与实践脱节的问题,还能够激发学生的兴趣,提高他们的动手能力和问题解决...

    b64.c-master_decoder_源码

    如果原始数据不是3字节的整数倍,那么最后可能会剩下不足3字节的数据,这时会在编码结果的末尾添加等于(4 - (原始字节数 % 3))个'=',以确保编码的完整性和正确性。 在"b64.c-master"的源代码中,解码过程通常包括...

    ASP源码—图片base64编码互转工具.zip

    Base64编码把每3个字节表示为4个字符,每个字符在65到90、97到122、48到57以及加号(+)和斜线(/)的范围内,最后可能用等于号(=)填充以保持字符数的整数倍。 2. **图片与Base64的关系**:在Web开发中,Base64...

    计算机网络课程习题(解答详细)

    计算表明,为了达到这个目标,信噪比需要增大约109.5倍。 进一步地,如果将信噪比增加到1000倍,即S/N0的109.5倍后再乘以10,通过计算发现最大信息速率只能增加18.41%,没有达到20%的目标。这说明信噪比的提升并不...

    Android数据加密之Base64编码算法的简单实现

    需要注意的是,Base64编码虽然提高了数据传输的稳定性,但会增加数据量,因为每个6位的“单词”在ASCII表示下需要1个字节,所以编码后的数据大约是原数据的1.33倍。此外,由于Base64编码是可逆的,因此它不提供安全...

    精易模块[永久免费] V3.12

    ' 修复 - 编码_ansi到usc2() 加快N倍的转换速度 ' 修复 - 编码_URL编码GBK () 第二个参数无效的问题 ' 修复 - 运算_表达式计算() 出现数据格式错误的部分问题 ' 修复 - 文本_颠倒 () '命令中文字出现乱码 ' 修复 - ...

    Delphi10.2 DES/CBC/PKCS5Padding/UTF-8

    如果原始数据不是8字节的整数倍,PKCS5填充会在数据末尾添加额外的字节,使得总长度为8的倍数,填充字节的值等于填充的字节数。 UTF-8是一种广泛使用的Unicode字符编码,可以表示所有Unicode字符。在加密过程中,...

    高一信息技术5月月考试题(无答案) 试题.doc

    图像编码信息的字节数X应该是8×8/8=8字节。第一行图像信息的二进制编码Y,根据题图无法直接确定,因为图像内容未给出。 3. ASCII码:ASCII码是一种字符编码标准,字符A的ASCII码为65,G在A之后6位,所以G的ASCII码...

Global site tag (gtag.js) - Google Analytics