Archive: Binary Numbers


Binary Numbers
Has any way I can convert binary numbers to string in NSIS without using System plugin?


Do you mean "DEC <-> HEX"?


Do you know the number system of the number below?

01100011 01011011

It's called the binary number system.

To create 1 bit and 4 bit bitmaps the user has to put a binary number to be converted after by the function into a hexadecimal number and then write the result into a .bmp file.


If you want "Hex -> Binary String" , try this below.

Or you just want to convert Binary number to Hex number?

OutFile NoToBinStr.exe
Name NoToBinStr
XPStyle on

Var NumberLen
Var HexSource
Var LetterTemp
Var LetterLocate
Var TargetString
Var TargetTemp
Var Count
Var BinTemp

Section Sec1Name sec1
SectionEnd

Function .onInit

; Set/Get Source Hex Number
StrCpy $HexSource 'ABCD'

StrLen $NumberLen $HexSource
StrCpy $LetterLocate $NumberLen

; loop - Get 1 Letter per loop from right for process
NextLetter:
StrCpy $TargetString '$TargetTemp $TargetString'
StrCpy $TargetTemp ''

StrCmp $LetterLocate '0' 0 +3
MessageBox MB_OK 'Hex ($HexSource) = Binary String ($TargetString)'
Quit

IntOP $LetterLocate $LetterLocate - 1
StrCpy $LetterTemp $HexSource 1 $LetterLocate
StrCpy $Count '256'

IntOP $Count $Count / 2
IntOP $BinTemp 0x$LetterTemp & $Count
IntCmp $BinTemp '0' +2
StrCpy $BinTemp '1'
StrCpy $TargetTemp '$TargetTemp$BinTemp'
StrCmp $Count '1' NextLetter -5

FunctionEnd

Result:

Hex (ABCD)= Binary String (00001010 00001011 00001100 00001101 )

the user has to put a binary number to be converted after by the function into a hexadecimal number and then write the result into a .bmp file
This means Binary -> Hexadecimal.

How about this function?

Function bin2hex

Exch $R9 ; binary input string (max 16 bits)
Push $R8
Push $R7 ; 4 character hex output string (empty if error detected)
Exch 2

StrLen $R8 $R9
IntCmp $R8 16 start start too_long

start:
StrCpy $R7 0

loop:
StrCpy $R8 $R9 1
StrCmp $R8 "" done
StrCpy $R9 $R9 "" 1
StrCmp $R8 "0" good_digit
StrCmp $R8 "1" good_digit bad_digit

good_digit:
IntOp $R7 $R7 * 2
IntOp $R7 $R7 + $R8
Goto loop

too_long:
MessageBox MB_OK|MB_ICONEXCLAMATION "Binary Input ($R9) is longer than 16 bits"
StrCpy $R7 ""
Goto exit

bad_digit:
MessageBox MB_OK|MB_ICONEXCLAMATION "Input is not binary (contains '$R8' character)"
StrCpy $R7 ""
Goto exit

done:
IntFmt $R7 "%04X" $R7

exit:
Pop $R9
Pop $R8
Exch $R7

FunctionEnd
usage:
Push "10101010"
Call bin2hex
Pop $0 ; holds "00AA"

Hmmm... thanks both of you for the function.