Archive: Please help to create function StrToCodes


Please help to create function StrToCodes
Please help to create function StrToCodes

That she found for instance word Qwerty after passing through this function became aea8baadaba6

Q W E R T Y U I O P A S D F G H J K L Z X C V B N M
ae a8 ba ad ab a6 aa b6 b0 af be ac bb b9 b8 b7 b5 b4 b3 a5 a7 bc a9 bd b1 b2

q w e r t y u i o p a s d f g h j k l z x c v b n m
8e 88 9a 8d 8b 86 8a 96 90 8f 9e 8c 9b 99 98 97 95 94 93 85 87 9c 89 9d 91 92

1 2 3 4 5 6 7 8 9 0
ce cd cc cb ca c9 c8 c7 c6 cf

` - = ~ ! @ # $ % ^ & * ( ) _ + { } | [ ] \ ; ' : " , . / < > ?
9f d2 c2 81 de bf dc db da a1 d9 d5 d7 d6 a0 d4 84 82 83 a4 a2 a3 c4 d8 c5 dd d3 d1 d0 c3 c1 c0

Sorry for bad English


The tricky part of this is the conversion from a single character to ASCII. A function in the wiki does that; the rest was pretty straight-forward.

The codes you want to convert the letters into are inverted ASCII (ASCII that has been subtracted from 255).

Don


The attached should work. (I've not tested it very well, but from the little I did, it works.)

It relies on outputing a "translation file" (basically a config file I use to "look up" each letter of the string.) It relies on logiclib, ConfigReadS, and ConfigWriteS.

One problem, however, is that I wasn't able to write the translation code for the backward quote (`). Not sure whether or not this is a bug with NSIS.

Here's the line that failed:


${ConfigWriteS} "${TranslatorFile}" "`=" "9f" $1


Maybe someone else would have some suggestions.

Since it uses a file, speed might become an issue if you have to translate large sets of data. (If you need to boost performance, you might try using the NSIS Array plugin instead of a file to use for the lookup table).

I've wrapped it into an NSH file. Usage is simple:
1) Include TextToCode.nsh in the top of your script
2) Add the command ${InitTranslator} to .OnInit
3) to translate text, use ${TextToCode} "text" $var (where $var is your output variable)

See the attached sample.

hmm... I must have posted the same time as demiller9...

I was thinking it looked like HEX codes, but they didn't seem to be the same HEX codes as a regular PC uses. (Maybe I'm wrong?)


Thanks

Both of a variant approach, but at demiller9 the idle time (that that is necessary)

Sorry for bad English =)


Comperio wrote:

I was thinking it looked like HEX codes, but they didn't seem to be the same HEX codes as a regular PC uses.
It is standard hex ASCII that has been XOR'd with FF. I caught onto it when I looked at the codes for the digits 1-9. The Qwerty layout in the original message makes it harder to spot for the letters.

I didn't check a backquote in my method until I read your post; my test program handles `, ', and " OK.

Don

Originally posted by demiller9
The tricky part of this is the conversion from a single character to ASCII. A function in the wiki does that; the rest was pretty straight-forward.

The codes you want to convert the letters into are inverted ASCII (ASCII that has been subtracted from 255).

Don
My profound thanks to demiller for saving me tons of time :D Paying it forward, I used this as a base for a URLEncode function and these are the adjustments that I made:

Add these two lines at the appropriate places:
!include "WordFunc.nsh"
!insertmacro WordFind

And then this is URLEncode function, based on demiller's StrToCodes (with un-inverted ASCII :) )

Function UrlEncode
Exch $0 ; save $0, get string
Push $1
Push $2

StrCpy $2 "" ; initialize the output codes
${While} $0 != ""
StrCpy $1 $0 1 ; get a single char
${If} $1 == " " ; space becomes +
StrCpy $2 "$2+"
${Else}
${WordFind} ${SAFECHARS} $1 "E+1{" $R0
IfErrors convert copy ; error means the character was not found in the safechars string
convert:
Push $1
Call CharToASCII ; convert a char to ascii value
Pop $1
IntFmt $1 "%02x" $1 ; convert ascii to hex
StrCpy $2 "$2%$1" ; append new code to previous codes
goto continue
copy:
StrCpy $2 "$2$1"
continue:
${Endif}
StrCpy $0 $0 "" 1 ; remove first character
${EndWhile}
Exch $2 ; return the result
Exch 2
Pop $0 ; restore registers
Pop $1
FunctionEnd