关于进制、BCC、字符串反转的几种用法(python和jmeter)、随机字符串、签名算法

  一、BCC校验码计算

def bcc(value):#  BCC校验码计算
numb = len(value)
#print(numb)
nb = int(numb / 2)
#print(nb)
bcc = 0
for i in range(nb):
a = 2 * i
b = 2 * (i + 1)
aa = value[a:b]
bcc = bcc ^ int(aa, 16)#^在算术运算中,表示异或。4 ^ 3表示4异或3,结果为7。

#print("===============================")
# print("生成bcc码:%s" % f'{bcc:x}')
#print("===============================")f'{bcc:x}'
return f'{bcc:x}'.upper()

二、十六进制字符串文本文件生成二进制文件
def changBinary(value):  #十六进制字符串文本文件生成二进制文件
len_s = int(len(value)/2) #计算字节的个数
list_nums = [] #定义一个空的list

i = 0
for i in range(0, len_s): #循环将解析出来的字节填充到list中
chs = value[2*i : 2*i + 2]
num = int(chs, 16)#以16进制规则解析为整数
list_nums.append(num)
bys = bytes(list_nums) #将list转换成字节数组(二进制字节流)
return bys

三、二进制字节流(组)转换成十六进制字符串
def changHexadecima(value): #二进制字节流(组)转换成十六进制字符串
lis_str=[]
# print(value)
lis_hex=list(value)
# print(lis_hex)
for hex_number in lis_hex:
hex_str=hex(hex_number)
# print(hex_str)
if hex_str.startswith('0x'):
hex_str = hex_str[2:]
if len(hex_str)==1:
hex_str='0'+hex_str
lis_str.append(hex_str)
result = ''.join(lis_str)
return result.upper()

四、10进制字符串和16进制字符、2进制串转换(计算器验证结果正确性)
10进制转16进制:hex(‘10进制字符串’)[2:].upper()
16进制转10进制整数:int(‘16进制字符串’, 16)
16进制字符串转2进制(01):bin(int(‘16进制’, 16))

 

 

    
五、字符串和16进制字符串之间转换
def hex_str(value): # 字符串转16进制
data = bytes(value, encoding='utf-8')
output = list(binascii.hexlify(data))
lis_str = []
for hex_number in output:
hex_str = hex(hex_number)
# print(hex_str)
if hex_str.startswith('0x'):
hex_str = hex_str[3:]
lis_str.append(hex_str)
result = ''.join(lis_str)
# print(result, type(result))
return result

def str_hex(value):  #16进制转字符串
flag = binascii.unhexlify(value).decode()
return flag


六、字符串反转
#第一种方法:使用字符串切片

a='123456789'
a = a[::-1]

#第二种方法:使用reversed() 可读行好,但速度较慢

number=''.join(reversed('123456789'))


七、JMETER的beanshell字符串/进制转换
// 将int字符串转为16进制字符串
String hex = Integer.toHexString(Integer.valueOf(“123”));

// 将Long字符串转为16进制字符串
String hex = Long.toHexString(Long.valueOf(“0123456789”));

// 将float转为16进制字符串
String hex = Integer.toHexString(Float.floatToIntBits(10.10));

// 将含字母或符号的字符串转为16进制(ASCII码转十六进制)
public String convertStringToHex(String str){
char[] chars = str.toCharArray();
StringBuffer hex = new StringBuffer();
for (int i = 0; i < chars.length; i++)
{
hex.append(Integer.toHexString((int)chars[i]));
}
return hex.toString();
}

// 将16进制转为含字母或符号的字符串(十六进制转ASCII码)
public String convertHexToString(String hex){
StringBuilder sb = new StringBuilder();
StringBuilder temp = new StringBuilder();
for (int i=0; i < hex.length() - 1; i += 2 ){
// grab the hex in pairs
String output = hex.substring(i, (i + 2));
// convert hex to decimal
int decimal = Integer.parseInt(output, 16);
// convert the decimal to character
sb.append((char)decimal);
temp.append(decimal);
}
return sb.toString();
}

// 将16进制字符串转为xx字节16进制字符串,不足位数,前面补0
public static String add_zore(String str, int size){
if (str.length() < size){
str= "0" + str;
str= add_zore(str, size);
return str;
} else {
return str;
}
}

// 将16进制数据,转为小端模式(低位转为高位)
public static String reverseHex(String hex) {
char[] charArray = hex.toCharArray();
int length = charArray.length;
int times = length / 2;
for (int c1i = 0; c1i < times; c1i += 2){
int c2i = c1i + 1;
char c1 = charArray[c1i];
char c2 = charArray[c2i];
int c3i = length - c1i - 2;
int c4i = length - c1i - 1;
charArray[c1i] = charArray[c3i];
charArray[c2i] = charArray[c4i];
charArray[c3i] = c1;
charArray[c4i] = c2;
}
return new String(charArray);
}

// 从响应值中,截取xx位,并将截取的16进制字符串转为10进制字符串
String time = response.substring(62, 74);
log.info("time_16:" + time);
long time_10 = Long.parseLong(time, 16);

String time_10 = String.valueOf(Long.valueOf(time, 16));
log.info("time_10:" + time_10);

// 16 进制float转10进制
Float value = Float.intBitsToFloat(Integer.valueOf(test.trim(), 16));
log.info("value:" + value);


八、JMETER的beanshell字符串转换/Base64加密

import org.apache.commons.net.util.Base64;


//String source = ${tong30};
String source30 = vars.get("tong30"); //读取变量
String source29 = vars.get("tong29");
String sourceSH = vars.get("SH-030");
String sourceT = vars.get("T33");


byte[] encodedBytes30 = Base64.encodeBase64(source30.getBytes("UTF-8")); //base64加密
byte[] encodedBytes29 = Base64.encodeBase64(source29.getBytes("UTF-8"));
byte[] encodedBytesSH = Base64.encodeBase64(sourceSH.getBytes("UTF-8"));
byte[] encodedBytesT = Base64.encodeBase64(sourceT.getBytes("UTF-8"));


String encoded30 = new String(encodedBytes30);
String encoded29 = new String(encodedBytes29);
String encodedSH = new String(encodedBytesSH);
//String encodedT = new String(encodedBytesT);


vars.put("yuntong30", encoded30);
vars.put("yuntong29", encoded29);
vars.put("ban30", encodedSH);
vars.put("Tong_33", encodedT);


字符串转int型


使用:Integer.parseInt() 


1
if (Integer.parseInt(vars.get("subtotal"))>0){#subtotal 变量,为1
1
log.info("转换成功,大于0"); }else{ log.info("转换失败"); }

 


 


字符串转double型 


使用:Double.parseDouble(),一定要注意,打印时,log.info要转成字符串


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.text.DecimalFormat;
double calculate=0.00;
double total=0.00;
String price = vars.get("subtotal"); #是变量1.2
String  grandtotal=vars.get("GrandTotal");#是变量2.3
log.info(price);
log.info(grandtotal);
 
calculate = Double.parseDouble(price);
log.info(calculate.toString());
calculate=calculate+6.99+1.64;
log.info(calculate.toString());
 
total= Double.parseDouble(grandtotal);
log.info("计算总价格"+calculate.toString());
log.info("结算总价格"+total.toString());
 
if (calculate==total){
    Failure = false;
    log.info("结算总价格与计算价格一致");  
}else{
    Failure = true;
    FailureMessage = "结算总价格与计算价不一致";
     
}
 
九、随机字符串、签名算法
import hashlib
import hmac
import random
import string

SECRET_KEY = "test"

# 生成 15 位长度的随机字符串
def gen_random_string(str_len):
random_char_list = []
for _ in range(str_len):
random_char = random.choice(string.ascii_letters + string.digits)
random_char_list.append(random_char)

random_string = ''.join(random_char_list)
return random_string

# 签名算法
def get_sign(*args):
content = ''.join(args).encode('ascii')
sign_key = SECRET_KEY.encode('ascii')
sign = hmac.new(sign_key, content, hashlib.sha1).hexdigest()
return sign



如有误,请纠正,谢!

 
 
posted @ 2021-04-21 14:26  大圣原来姓毛  阅读(781)  评论(0编辑  收藏  举报