Float浮点数转二进制串和十六进制串

#include <String.au3>
#include <Array.au3>
#cs
 0.125  0000003e
 12.25  00004441
 -0.125 000000be
 1.3    6666a63f 1.299999952316284
 0.3125 0000a030
 0.2    c8cc4c3e 0.199999928474426
 0.3    9899993e 0.299999952316284
#ce

Local $aArray = StringSplit("0.125,12.25,-0.125,1.3,0.3125,0.2,0.3", ",")
For $i = 1 To UBound($aArray)-1
ConsoleWrite(@LF&‘----------------------------------------‘&@LF&@LF)
 Local $floatHex=FloatToHex($aArray[$i])
ConsoleWrite(‘=>‘ &$floatHex& @LF)
ConsoleWrite(@LF)
BinaryToFloat($floatHex)

Next

;1位符号位+8位指数+23位尾数
Func FloatToHex($floatStr)
 ;符号位 正数为0 负数为1
 Local $pegativeStr = ‘0‘
 If StringInStr($floatStr, "-") <> 0 Then $pegativeStr = ‘1‘
 Local $pos = StringInStr($floatStr, ".")
 ;小数点左边的值
 Local $left = StringLeft($floatStr, $pos - 1)
 ;小数点右边的值
 Local $right = StringRight($floatStr, StringLen($floatStr) - StringLen($left) - 1)
 Local $binaryStr = DecimalToBinary(Number($left)) & FloatSmailDecimalToBinary(Number(‘0.‘ & $right, 3))
 ConsoleWrite($floatStr & ‘=>‘ & $binaryStr & @LF)
 ;二进制串中,从左边算起第一个数字1的位置 例 1100.01 值为1
 Local $leftOnePos = StringInStr($binaryStr, "1", 0, 1);
 ;二进制串中,小数点的位置 例 1100.01 值为5
 Local $decimalPos = StringInStr($binaryStr, ".");
 ;指数部分为二进制浮点数正规化后,小数点移动的位数+127
 ;0.0011 2-5=-3     1001.01 5-1=4移动次数需再减1
 Local $exponent = $decimalPos - $leftOnePos + 127
 If ($decimalPos - $leftOnePos) > 0 Then $exponent -= 1
 Local $exponentBinaryStr = DecimalToBinary($exponent)
 ConsoleWrite(‘Exponent=>‘ & $exponent)
 ;指数左边补足8位
 $exponentBinaryStr = _StringRepeat("0", 8 - StringLen($exponentBinaryStr)) & $exponentBinaryStr
 ConsoleWrite(‘=>‘ & $exponentBinaryStr & @LF)
 ;尾数部分
 Local $tailBinaryStr = StringReplace(StringMid($binaryStr, $leftOnePos + 1), ‘.‘, "")
 ;用0补足23位
 $tailBinaryStr &= _StringRepeat("0", 23 - StringLen($tailBinaryStr))
 ConsoleWrite(‘Tail=>‘ & $tailBinaryStr & @LF)
 Local $floatBinaryStr = $pegativeStr & $exponentBinaryStr & $tailBinaryStr;
 ConsoleWrite(‘=>‘ & $pegativeStr & ‘ ‘ & $exponentBinaryStr & ‘ ‘ & $tailBinaryStr & @LF)
 Local $result = ‘‘
 For $i = 0 To StringLen($floatBinaryStr) / 4 - 1 Step 1
  $result &= DecimalToHex(BinaryToDecimal(StringMid($floatBinaryStr, $i * 4 + 1, 4)))
 Next
 Return StringLower(MemData($result))
EndFunc   ;==>FloatToBinary

Func BinaryToFloat($hexStr)
 Local $binaryStr = HexToBinary(MemData($hexStr))
 ConsoleWrite($hexStr & ‘=>‘ & $binaryStr & @LF)
 Local $pegativeStr = ‘‘;
 If StringLeft($binaryStr, 1) = ‘1‘ Then $pegativeStr = ‘-‘
 Local $exponentBinaryStr = StringMid($binaryStr, 2, 8)
 ConsoleWrite(‘ExponentBinary=>‘ & $exponentBinaryStr & @LF)
 Local $tailBinaryStr = StringMid($binaryStr, 10, 23)
 ConsoleWrite(‘TailBinary=>‘ & $tailBinaryStr & @LF)
 Local $exponent = BinaryToDecimal($exponentBinaryStr) - 127
 ConsoleWrite(‘Exponent=>‘ & $exponent & @LF)
 Local $tail = "1." & $tailBinaryStr
 Local $newTail = ‘‘
 If $exponent >= 0 Then
  $newTail = StringReplace(StringLeft($tail, $exponent + 2), ".", "") & ‘.‘ & StringMid($tail, $exponent + 3)
 Else
  $newTail = ‘0.‘ & _StringRepeat("0", -$exponent - 1) & StringReplace($tail, ‘.‘, ‘‘)
 EndIf
 ConsoleWrite(‘Tail=>‘ & $newTail & @LF)
 Local $decimalPos = StringInStr($newTail, ".");
 Local $result = BinaryToDecimal(StringLeft($newTail, $decimalPos - 1)) & BinaryToSmailDecimal(StringMid($newTail, $decimalPos + 1))
 ConsoleWrite(‘=>‘ & $result & @LF)
 Return $pegativeStr & $result
EndFunc   ;==>BinaryToFloat

Func MemData($hexStr)
 Local $result = ‘‘
 Local $index = StringLen($hexStr) / 2 - 1
 For $i = $index To 0 Step -1
  $result &= StringMid($hexStr, $i * 2 + 1, 2)
 Next
 Return $result
EndFunc   ;==>MemData

#cs
 如0.638
 1、将其乘二:0.638*2=1.276 则二进制小数的二分位是1.276整数部分1
 2、取小数0.276,0.276*2=0.552 则二进制小数的二十分位是0.522整数部分0
 3、重复1
 得:(0.638)10=(约等于)(0.1010)2
#ce
;小数部分转二进制
Func FloatSmailDecimalToBinary($float)
 Local $floatStr = String($float)
 Local $leftChar = ‘‘
 Local $result = ‘‘
 For $i = 1 To 23 Step 1
  $float = $float * 2
  If ($float = 0) Then ExitLoop ;
  $floatStr = String($float)
  $leftChar = StringLeft($floatStr, 1)
  $result &= $leftChar
  $temp = StringFormat(‘%f‘, ‘0.‘ & StringMid($floatStr, 3, StringLen($floatStr) - 2))
  $float = $temp;
 Next
 Return ‘.‘ & $result
EndFunc   ;==>FloatSmailDecimalToBinary

;十进制转二进制
Func DecimalToBinary($dec)
 Local $temp = 0, $shit = 0
 For $a = 1 To BinaryLen($dec) * 8 Step 1
  $shit = $a
  If (2 ^ $a > $dec) Then ExitLoop
 Next
 Local $result = ""
 For $i = 0 To $shit - 1 Step 1
  $result &= Chr(BitAND(0x1, BitShift($dec, $i)) + Asc("0"))
 Next
 Return _StringReverse($result)
EndFunc   ;==>DecimalToBinary

;二进制转十进制
Func BinaryToDecimal($bin)
 Local $result = 0
 Local $binArray = StringSplit($bin, "", 2)
 For $a = UBound($binArray) To 1 Step -1
  Local $number = StringMid($bin, $a, 1)
  If $number = "1" Then
   $result += 2 ^ (UBound($binArray) - $a)
  EndIf
 Next
 Return $result
EndFunc   ;==>BinaryToDecimal

;二进制小数转十进制
Func BinaryToSmailDecimal($bin)
 Local $result = 0
 Local $binArray = StringSplit($bin, "", 2)
 For $a = 1 To UBound($binArray) Step 1
  Local $number = StringMid($bin, $a, 1)
  If $number = "1" Then
   $result += 2 ^ - $a
  EndIf
 Next
 Return StringMid($result, 2)
EndFunc   ;==>BinaryToSmailDecimal

;十进制转十六进制
Func DecimalToHex($dec)
 Local $temp = 0, $shit = 0
 Local $hexChar = ‘0123456789ABCDEF‘
 Local $hexCharArray = StringSplit($hexChar, ‘‘, 2)
 For $a = 1 To BinaryLen($dec) * 8 Step 1
  $shit = $a
  If (16 ^ $a > $dec) Then ExitLoop
 Next
 Local $result = ""
 For $i = 0 To $shit - 1 Step 1
  $dddd = BitAND(0xf, BitShift($dec, $i * 4))
  $result &= $hexCharArray[$dddd]
 Next
 Return _StringReverse($result)
EndFunc   ;==>DecimalToHex

;十六进制转二进制
Func HexToBinary($hex)
 If StringUpper(StringLeft($hex, 2)) = "0x" Then $hex = StringUpper(StringRight($hex, StringLen($hex) - 2))
 Local $bits = "0000|0001|0010|0011|0100|0101|0110|0111|1000|1001|1010|1011|1100|1101|1110|1111"
 $bits = StringSplit($bits, ‘|‘, 2)
 Local $hexInputArray = StringSplit($hex, ‘‘, 2)
 Local $result = ""
 For $a In $hexInputArray
  $result &= $bits[Dec($a)]
 Next
 Return $result
EndFunc   ;==>HexToBinary

运行结果:

--------------------------------------------------------------------------------

0.125=>0.001
Exponent=>124=>01111100
Tail=>00000000000000000000000
=>0 01111100 00000000000000000000000
=>0000003e

0000003e=>00111110000000000000000000000000
ExponentBinary=>01111100
TailBinary=>00000000000000000000000
Exponent=>-3
Tail=>0.00100000000000000000000000
=>0.125

--------------------------------------------------------------------------------

12.25=>1100.01
Exponent=>130=>10000010
Tail=>10001000000000000000000
=>0 10000010 10001000000000000000000
=>00004441

00004441=>01000001010001000000000000000000
ExponentBinary=>10000010
TailBinary=>10001000000000000000000
Exponent=>3
Tail=>1100.01000000000000000000
=>12.25

--------------------------------------------------------------------------------

-0.125=>0.001
Exponent=>124=>01111100
Tail=>00000000000000000000000
=>1 01111100 00000000000000000000000
=>000000be

000000be=>10111110000000000000000000000000
ExponentBinary=>01111100
TailBinary=>00000000000000000000000
Exponent=>-3
Tail=>0.00100000000000000000000000
=>0.125

--------------------------------------------------------------------------------

1.3=>1.01001100110011001100110
Exponent=>127=>01111111
Tail=>01001100110011001100110
=>0 01111111 01001100110011001100110
=>6666a63f

6666a63f=>00111111101001100110011001100110
ExponentBinary=>01111111
TailBinary=>01001100110011001100110
Exponent=>0
Tail=>1.01001100110011001100110
=>1.299999952316284

--------------------------------------------------------------------------------

0.3125=>0.0101
Exponent=>125=>01111101
Tail=>01000000000000000000000
=>0 01111101 01000000000000000000000
=>0000a03e

0000a03e=>00111110101000000000000000000000
ExponentBinary=>01111101
TailBinary=>01000000000000000000000
Exponent=>-2
Tail=>0.0101000000000000000000000
=>0.3125

--------------------------------------------------------------------------------

0.2=>0.00110011001100110011001
Exponent=>124=>01111100
Tail=>10011001100110011001000
=>0 01111100 10011001100110011001000
=>c8cc4c3e

c8cc4c3e=>00111110010011001100110011001000
ExponentBinary=>01111100
TailBinary=>10011001100110011001000
Exponent=>-3
Tail=>0.00110011001100110011001000
=>0.199999928474426

--------------------------------------------------------------------------------

0.3=>0.01001100110011001100110
Exponent=>125=>01111101
Tail=>00110011001100110011000
=>0 01111101 00110011001100110011000
=>9899993e

9899993e=>00111110100110011001100110011000
ExponentBinary=>01111101
TailBinary=>00110011001100110011000
Exponent=>-2
Tail=>0.0100110011001100110011000
=>0.299999952316284

时间: 2024-10-10 14:58:24

Float浮点数转二进制串和十六进制串的相关文章

float浮点数的二进制存储方式及转换

int和float都是4字节32位表示形式.为什么float的范围大于int? float精度为6-7位.1.66*10^10的数字结果并不是166 0000 0000 指数越大,误差越大. 这些问题,都是浮点数的存储方式造成的. float和double在存储方式上都是遵从IEEE的规范的,float遵从的是IEEE R32.24 ,而double 遵从的是R64.53. 无论是单精度还是双精度在存储中都分为三个部分: 符号位(Sign) : 0代表正,1代表为负 指数位(Exponent):

浮点数的二进制表示

基础知识: 十进制转十六进制: 十六进制转二进制: IEEE制定的浮点数表示规则: 了解: 目前C/C++编译器标准都遵照IEEE制定的浮点数表示法来进行float,double运算.这种结构是一种科学计数法,用符号.指数和尾数来表示,底数定为2--即把一个浮点数表示为尾数乘以2的指数次方再添上符号.下面是具体的规格: 符号位     阶码      尾数     长度 float           1          8        23      32 double         

hdu5384 AC自动机模板题,统计模式串在给定串中出现的个数

http://acm.hdu.edu.cn/showproblem.php?pid=5384 Problem Description Danganronpa is a video game franchise created and developed by Spike Chunsoft, the series' name is compounded from the Japanese words for "bullet" (dangan) and "refutation&q

POJ 3461 Oulipo (求模式串在文本串中出现的次数)

Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 36128   Accepted: 14584 Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a member of the Oulipo group. A quot

【通过操作指针,与指针做函数參数&amp;#39;实现字串在主串中出现的次数,然后将出现的部分依照要求进行替换 】

#include<stdio.h> #include<stdlib.h> int strTime(const char *str1, const char *str2, int *time) { int count = 0; char *p1 = str1; char *p2 = str2; //p1是第一次出现的位置 p1 = strstr(p1, p2); //注意这里不要写成*p1!=NULL 由于p1 是null的地址一旦读取*p1 会出错的!!!!不能读取操作系统的数据

通过操作指针,与指针做函数参数&#39;实现字串在主串中出现的次数,然后将出现的部分按照要求进行替换

#include<stdio.h> #include<stdlib.h> int strTime(const char *str1, const char *str2, int *time) { int count = 0; char *p1 = str1; char *p2 = str2; //p1是第一次出现的位置 p1 = strstr(p1, p2); //注意这里不要写成*p1!=NULL 因为p1 是null的地址一旦读取*p1 会出错的!!!!不能读取操作系统的数据

二进制,八进制,十进制,十六进制!!!!

一直以为进制转换离我很远,不会碰到这个问题,就算碰到了,百度一下就能解决,所以一直没关注过这个问题.直到有一天,碰到了一个进制转换问题,却不可以用百度...FUCK. 所以总结一下,二进制,八进制,十进制,十六进制如下: * 二进制全是0.1 * 八进制以0开头表示,0-7 * 十进制不能以0开头表示,0-9 * 十六进制以0x开头表示,0-9,A-F java中进制转换: A.十进制转换其他 十进制转成二进制  Integer.toBinaryString(int i) 十进制转成八进制  I

3、将二进制转换成十六进制

/* 将二进制转换成十六进制 */ class BinaryToHex { public static void main(String[] args) { int num = 120; char[] result = new char[1024]; int count = 0; while((num & 15) > 0) { int tmp1 = num & 15; if(tmp1 > 9) { result[count] = (char)(tmp1 - 10 + 'A');

(KMP 1.2)hdu 1686 Oulipo(计算模式串在文本串中出现的次数)

题目: Oulipo Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5985    Accepted Submission(s): 2404 Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition,