报文格式为byte[]的转义处理

一般为解码时将报文数组里的两位转为一位,编码时将一位转为两位。

具体情况试报文协议而定。

/**
 * byte数组报文转义
 * @param bs 整个byte数组,可包括不转义部分
 * @param start 转义数组的开始位置
 * @param end 转义数组的结束位置
 * @return 转义后的整个数组
 * @throws IOException 
 */

public static byte[] doEscape(byte[] bs, int start, int end) throws IOException {
    if (start < 0 || end > bs.length) {
        throw new ArrayIndexOutOfBoundsException("doEscape error : index out of bounds(start=" + start
                + ",end=" + end + ",bytes length=" + bs.length + ")");
    }
    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
        int i = 0;
		//写入不需转义的前部分
        for (; i < start; i++) {
            outputStream.write(bs[i]);
        }
        //开始转义需要转义部分
        for (i = start; i <=end; i++) {
            //将接受到的0x7d0x01转为0x7d
            if (bs[i] == 0x7d && bs[i + 1] == 0x01) {
                outputStream.write(0x7d);
                //多转几位,i加几
                i++;
            }//将接受到的0x0d转为0x0a0x01
            else if (bs[i] == 0x0d) {
                outputStream.write(0x0a);
                outputStream.write(0x01);
            } //写入不符合转义需求部分
            else {
                outputStream.write(bs[i]);
            }
        }
		//写入不需转义的后部分
        for (; i < bs.length; i++) {
            outputStream.write(bs[i]);
        }
        return outputStream.toByteArray();
    } catch (IOException e) {
        throw new UnsupportedOperationException(e);
    }
}


 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用pymysql执行SQL语句时,如果字符串中包含引号,需要进行转义处理,否则会导致SQL语句语法错误或SQL注入攻击。下面介绍两种常见的引号转义处理方法: 1. 使用pymysql.escape_string()函数 pymysql.escape_string()函数可以将字符串中的特殊字符进行转义,包括引号、反斜杠等。示例代码如下: ```python import pymysql # 连接数据库 conn = pymysql.connect(host='localhost', user='root', password='123456', db='test') # 创建游标 cursor = conn.cursor() # 定义要插入的数据 name = 'Tom' age = 20 remark = 'He said, "I\'m fine."' # 对remark字段进行转义处理 remark = pymysql.escape_string(remark) # 执行插入语句 sql = "INSERT INTO users (name, age, remark) VALUES ('%s', '%d', '%s')" % (name, age, remark) cursor.execute(sql) # 提交事务 conn.commit() # 关闭游标和连接 cursor.close() conn.close() ``` 在上面的代码中,使用`pymysql.escape_string()`函数将`remark`字段中的双引号进行了转义处理转义后的结果为`He said, \"I\'m fine.\"`,这样就可以避免插入语句中的引号导致语法错误。 2. 使用占位符 使用占位符的方法可以避免手动进行引号转义处理,使代码更加简洁和安全。示例代码如下: ```python import pymysql # 连接数据库 conn = pymysql.connect(host='localhost', user='root', password='123456', db='test') # 创建游标 cursor = conn.cursor() # 定义要插入的数据 name = 'Tom' age = 20 remark = 'He said, "I\'m fine."' # 执行插入语句 sql = "INSERT INTO users (name, age, remark) VALUES (%s, %s, %s)" params = (name, age, remark) cursor.execute(sql, params) # 提交事务 conn.commit() # 关闭游标和连接 cursor.close() conn.close() ``` 在上面的代码中,使用了占位符`%s`来代替插入语句中的字符串,然后使用`cursor.execute()`函数的第二个参数`params`来传递参数,由pymysql自动进行转义处理。这样就可以避免手动进行引号转义处理,提高了代码的可读性和安全性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值