Archive: Str2Bin


Str2Bin
Hi,

has anyone an idea to convert a string into it's binary values?

I need to set the username for ms office and its written as Binary Registry Key.

My problem is that parsing through an input variable byte by byte won't help me because nsis Strcpy isn't case sensitive.

regards

flizebogen


1)To convert an entire string into decimal numbers, you can use the CharToASCII Function. It's case insensitive, so it will help you in your cause.

2)To convert a decimal number to a hexadecimal number to be used in the hex2bin (said on step 3) use:

IntFmt $var "%08x" $var


3)To convert an hexadecimal number into a binary number, you need to call hex2bin (found in CreateBitmap Function Thread). Probably it has limitations, like the number can't have the "0x" string appended to the start of the number, and 8 digits length limit (the function is too old for me to remember how to use it...). I re-posted the function here for reference:


;Hex2Bin

;Converts a hexadecimal number string to binary.

;Push "HexadecimalNumber"
;Call bin2dec
;Pop "BinaryNumberVariable"

Function Hex2Bin

Push $0
Exch
Exch $1
Exch
Push $2
Push $3
Push $4
Push $5
Push $6
Push $7

; Set/Get Source Hex Number

StrLen $0 $1
StrCpy $3 $0

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

StrCmp $3 '0' End

IntOP $3 $3 - 1
StrCpy $2 $1 1 $3
StrCpy $6 '256'

IntOP $6 $6 / 2
IntOP $7 0x$2 & $6
IntCmp $7 '0' +2
StrCpy $7 '1'
StrCpy $5 '$5$7'
StrCmp $6 '1' NextLetter -5

End:

StrCpy $1 $4 "" 4
StrCpy $1 $1 4

Pop $7
Pop $6
Pop $5
Pop $4
Pop $3
Pop $2
Pop $0
Exch $1

FunctionEnd

Here's another solution using AfrowUk's NSISArray plugin. (see attached.)

here's the overview of the steps:
1. Create an array containing the binary equivalents of each hex number, starting with zero. (0000,0001,0010,etc.)

2. The Hex2Bin function then converts the number to hex using IntFmt. (This ensures that the number is really hex. If an invalid string is used, then IntFmt converts it to zero)

3. Now, read each hex digit one at a time (left to right). Convert the digit back to a number. This digit becomes the index of the array. Substitue the array's value. concatenate all values and you get the binary number.

It's hard to explain, so have a look at the sample script. (You need to have the NSISArray installed.)

using this method, you are limited to a 64-digit binary number, but that should more than adequate for what you'd ever need! The only caveat is that you may have to trim off the excess zeros at the beginning of the number.

Let me know if you find this useful. (Perhaps when I get time, I'll add a function to strip off the leading zeros and add it to the Wiki...)


I was finally able to convert the characters using deguix logiclib and now i'm failing to add it to the registry :-)

WriteRegBin only accepts hardcoded values. No Variables allowed.

The only solution i see is to create a valid .reg file, and than to execute regedit silently. What a hack.


There is a Registry plug-in by Instructor. I never tested it, but it as it says in the feature list, it can write any type of data to registry.


http://forums.winamp.com/showthread....56#post1753856

see:
registry::StrToHex
registry::Write


@Instructor

Your Plugin enhancements are really cool. The key i want to store in Registry is Unicode. Unfortunately Unicode is not supported by NSIS. That means the content of my variable is cut after the first byte. And ideas how to store a unicode string that needs to be converted to BIN in registry?


As I know NSIS variable can't hold unicode string, because one of two bytes often equal to "00" (end of string).
To store a unicode string in registry probably you need stand alone program with unicode support.


The System plug-in can handle Unicode strings. Just allocate a buffer instead of using internal variables.