pb实现十六进制hex转float

前几天接到一个以前接口开发需求,串口传出的数据要以十六进制接收,部分数据的字符串是需由十六进制转float,查遍整个百度也没有PB开发此功能的案例,因此自己根据网络上指引,用PB开发这个功能。
参考技术网页
https://www.jianshu.com/p/e5d72d764f2f IEEE754关于浮点数在计算机存储的介绍
https://www.cnblogs.com/pezy/archive/2011/08/30/Hex2Double.html 十六进制转double的其他语言代码

以下是我写完的一个PB类(uo_maxlab_hex2float) 调用此包的uf_hex2float(十六进制字符串)即可。

forward
global type uo_maxlab_hex2float from nonvisualobject
end type
end forward

global type uo_maxlab_hex2float from nonvisualobject
end type
global uo_maxlab_hex2float uo_maxlab_hex2float

forward prototypes
public function string uf_char2bin (character ac_temp)
public function string uf_hex2bin (string as_str)
public function integer uf_bin2int (string as_str)
public function decimal uf_bin2dec (string as_str)
public function decimal uf_hex2float (string as_str)
end prototypes

public function string uf_char2bin (character ac_temp);//将单个字符转换为2进制字符串
choose case upper(ac_temp)
	case ‘0‘
		return ‘0000‘
	case ‘1‘
		return ‘0001‘
	case ‘2‘
		return ‘0010‘
	case ‘3‘
		return ‘0011‘
	case ‘4‘
		return ‘0100‘
	case ‘5‘
		return ‘0101‘
	case ‘6‘
		return ‘0110‘
	case ‘7‘
		return ‘0111‘
	case ‘8‘
		return ‘1000‘
	case ‘9‘
		return ‘1001‘
	case ‘A‘
		return ‘1010‘
	case ‘B‘
		return ‘1011‘
	case ‘C‘
		return ‘1100‘
	case ‘D‘
		return ‘1101‘
	case ‘E‘
		return ‘1110‘
	case ‘F‘
		return ‘1111‘
end choose
return ‘‘
end function

public function string uf_hex2bin (string as_str);//将十六进制字符串转为2进制
string ls_temp
ls_temp = ‘‘
do while as_str> ‘‘
	ls_temp = ls_temp +uf_char2bin(left(as_str,1))
	as_str = mid(as_str,2)
loop

return ls_temp
end function

public function integer uf_bin2int (string as_str);//将2进制字符串转为十进制int类型
int j,i 
string ls_i
int li_return=0
i = len(as_str)
for j= 1 to i
	ls_i =mid(as_str,j,1)
	li_return = integer(ls_i)*2^(i - j) +li_return
next
return li_return
end function

public function decimal uf_bin2dec (string as_str);//如0.010101  将二进制小数部分字符串的010101转为十进制

int j,i 
string ls_i
decimal ldc_return=0
i = len(as_str)
if i = 0 then return 0
for j= 1 to i
	ls_i =mid(as_str,j,1)
	ldc_return = integer(ls_i)/2^j +ldc_return
next
return ldc_return
end function

public function decimal uf_hex2float (string as_str);//将16进制字符串转为float类型,PB无float类型,以decimal或real代替
string ls_binary,ls_int,ls_ben,ls_decimal,ls_exponent
Decimal ldc_return,ldc_ben
integer li_exp,ldc_int
integer sign

ls_binary = uf_hex2bin(as_str)
sign = 0
if left(ls_binary,1) = ‘1‘ then sign = 1

ls_exponent = mid(ls_binary,2,8)//获取指数位二进制字符串
li_exp = uf_bin2int(ls_exponent)-127//转换后减去移位exponential bias,得到指数

ls_decimal = ‘1‘+ mid(ls_binary,10,23)//获取2进制原始数据的小数(尾数),2进制科学记数法最左边总是1,要补上1

if li_exp < 0 then
	ls_ben = fill(‘0‘,abs(li_exp)-1)+ls_decimal//当指数小于0时,小数部分2进制字符串要根据指数绝对值在字符串前面补0
	ldc_int = 0//十进制整数部分为0
else
	ls_int = left(ls_decimal,li_exp+1)//整数部分字符串
	ldc_int = uf_bin2int(ls_int)//整数	
   ls_ben = mid(ls_decimal,li_exp+2)//小数部分2进制字符串
end if

ldc_ben = uf_bin2dec(ls_ben)//转换2进制小数部分为10进制

if sign = 0 then
   ldc_return = (ldc_int +ldc_ben)
else
	ldc_return = (ldc_int +ldc_ben)* -1
end if 

return ldc_return
end function

on uo_maxlab_hex2float.create
call super::create
TriggerEvent( this, "constructor" )
end on

on uo_maxlab_hex2float.destroy
TriggerEvent( this, "destructor" )
call super::destroy
end on

  

pb实现十六进制hex转float

上一篇:【UVa】1374 Power Calculus(IDA*)


下一篇:jenkins第一个pipeline