数据包转义处理VC6完整工程

数据包转义处理VC6完整工程
代码如下:

// VerifyCode.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>


unsigned char  Mac_Id[6]={0x01,0x60,0x20,0x76,0x84,0x41};

unsigned char GPS_Info[49]={0x7e,
							02,00,00,0x22,
							0x01,0x60,0x20,0x76,0x84,0x41,
							00,0x7c,
							00,00,00,00,//报警标志
							00,0x40,00,02,//状态
							0x01,0xd2,0xee,0x39,
							0x07,0x38,0xee,0xdd,
							00,00,
							00,00,
							00,00,
							0x19,//年
							0x03,
							0x05,
							0x15,
							0x56,//分
							0x55,//秒
							00,00,00,00,00,00,
							0xe7,
							0x7e
							};


unsigned char Heart_Info[29]={
							0x7e,
							00,0x02,
							00,0x0e,
							0x01,0x60,0x20,0x76,0x84,0x41,
							00,0x01,
							0x40,//终端扩展信息
							0x15,0xe7,//电压
							0x03,//GSM信号
							0x0f,//卫星
							0x00,
							00,00,00,00,00,00,
							00,00,
							0x1e,
							0x7e
							};

void gps_verify()
{
	//校验码
	unsigned char result=GPS_Info[1];
	for(int k=2;k<=46;k++)
	{
		result^=GPS_Info[k];
	}
	GPS_Info[47]=result;
	//打印输出
	for(int s=0;s<49;s++)
	{
		printf("%02x",GPS_Info[s]);
	}
}

void heart_verify()
{
	//校验码
	unsigned char result=Heart_Info[1];
	for(int k=2;k<=26;k++)
	{
		result^=Heart_Info[k];
	}
	Heart_Info[27]=result;
	//打印输出
	for(int s=0;s<29;s++)
	{
		printf("%02x",Heart_Info[s]);
	}
}
int main(int argc, char* argv[])
{

	gps_verify();
	printf("\r\n");printf("\r\n");printf("\r\n");

	return 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、付费专栏及课程。

余额充值