# Reading letters and convert to numbers

## Recommended Posts

Hey... I've been thinking much about how i should work this out... I want to write a script that can read a .txt file and then take the data from that file and make every letter into numbers... I mean so everytime there will come lets say an a then it'd make it to 1. and everytime there would come a b it would make it to 2 and so on... Anyone have any ideas to do this? And i don't want anyone to write the whole program, ofc not. Just how to start it.

-XInFisk

##### Share on other sites

Hey... I've been thinking much about how i should work this out... I want to write a script that can read a .txt file and then take the data from that file and make every letter into numbers... I mean so everytime there will come lets say an a then it'd make it to 1. and everytime there would come a b it would make it to 2 and so on... Anyone have any ideas to do this? And i don't want anyone to write the whole program, ofc not. Just how to start it.

-XInFisk

##### Share on other sites

Also you want to consider your logic, assuming you want to be able to convert it back...

How are you going to tell the difference between 24 (which could be "be") and 24 which might be "x"?

What are you going to do with numbers from the original file?

How about spaces, and punctuation (some people still use it, rare though it is)

##### Share on other sites

Also you want to consider your logic, assuming you want to be able to convert it back...

How are you going to tell the difference between 24 (which could be "be") and 24 which might be "x"?

What are you going to do with numbers from the original file?

How about spaces, and punctuation (some people still use it, rare though it is)

Yeah, i've been thinking about... the numbers was just an eg.

##### Share on other sites

Perhaps you are considering a prime number hash:

1. Assign each letter a prime number, i.e. a = 2, b = 3, c = 5, d = 7, e = 11, f = 13, etc...

2. Multiply the numbers for each letter together and you get a unique hash for that 'word', or combination of letters, i.e. 'fad' = 13 * 2 * 7 = 182

3. Factor out the primes from the hash and you will have a list of the letters in the original word (but not their position in the word).

There are more complicated versions that basic scheme to achieve many different outcomes. Which one applies depends on the application - which is...?

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

##### Share on other sites

Perhaps you are considering a prime number hash:

1. Assign each letter a prime number, i.e. a = 2, b = 3, c = 5, d = 7, e = 11, f = 13, etc...

2. Multiply the numbers for each letter together and you get a unique hash for that 'word', or combination of letters, i.e. 'fad' = 13 * 2 * 7 = 182

3. Factor out the primes from the hash and you will have a list of the letters in the original word (but not their position in the word).

There are more complicated versions that basic scheme to achieve many different outcomes. Which one applies depends on the application - which is...?

Uhm. I'll just make this for making some text unreadable and then readable again... I'm most doing this for fun, and could be nice to show it to the friends <: So i've just been thinking about something like a = (&) and b = (/#%) and so on or something like that

##### Share on other sites

Simple substitution cipher then:

1. Make a table of values that associates each letter with an alternate character.

2. Substitute the alternate char for the letter.

This can be made easier if you avoid the table and just throw in a little math to add/subtract a number to the ASCII code for the letter:

```While 1
\$RedData = InputBox("Substitution Cipher", "Enter string to substitute: ")
If @error Then Exit
\$BlackData = _StringSubstCipher(\$RedData, 3)
MsgBox(64, "Substitution Cipher", "Substitution cipher yielded: " & @CRLF & \$BlackData)
\$ClearData = _StringSubstCipher(\$BlackData, -3)
MsgBox(64, "Substitution Cipher", "Substitution cipher reversed yielded: " & @CRLF & \$ClearData)
WEnd

Func _StringSubstCipher(\$sString, \$iIndex)
Local \$avSplit = StringSplit(\$sString, "")
Local \$RET = ""
For \$n = 1 To \$avSplit[0]
\$RET &= Chr(Asc(\$avSplit[\$n]) + \$iIndex)
Next
Return \$RET
EndFunc   ;==>_StringSubstCipher```

Notice that you pass the function a number to add to the ASC() value, and deciphering is just passing the negative value for this. In the above example, the cipher is done by adding 3, and decipher by adding -3. Not very secure, but cute...

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

##### Share on other sites

Take a look at Asc() and Chr() in the help file. You could do some simple obfuscation with those two functions.

```\$file_input = @ScriptDir & "\input.txt"
\$file_output = @ScriptDir & "\output.txt"

\$hInput = FileOpen(\$file_input, 0)
FileClose(\$hInput)
\$text = ""
For \$i = 1 To StringLen(\$sInput)
\$asc = Asc(StringMid(\$sInput, \$i, 1))
\$chr = Chr(\$asc + 128) ; shift to the extended char set
\$text = \$text & \$chr
Next
\$hOutput = FileOpen(\$file_output, 2) ; Write in Overwrite mode
FileWrite(\$hOutput, \$text)
FileClose(\$hOutput)

; Restoring
\$file_restore = @ScriptDir & "\restore.txt"
\$hOutput = FileOpen(\$file_output, 0)
\$text = ""
For \$x = 1 To StringLen(\$sOutput)
\$asc = Asc(StringMid(\$sOutput, \$x, 1))
\$chr = Chr(\$asc - 128) ; shift back
\$text = \$text & \$chr
Next
\$hRestore = FileOpen(\$file_restore, 2)
FileWrite(\$hRestore, \$text)
FileClose(\$hRestore)```

This is what the code above looks like when passed through the script:

`¤æéìåßéîðõô ½ ÀÓãòéðôÄéò ¦ ¢Üéîðõô®ôøô¢Å ¤æéìåßïõôðõô ½ ÀÓãòéðôÄéò ¦ ¢Üïõôðõô®ôøô¢Å Å ¤èÉîðõô ½ ÆéìåÏðåî¨¤æéìåßéîðõô¬ °©Å ¤óÉîðõô ½ ÆéìåÒåáä¨¤èÉîðõô©Å ÆéìåÃìïóå¨¤èÉîðõô©Å ¤ôåøô ½ ¢¢Å Æïò ¤é ½ ± Ôï ÓôòéîçÌåî¨¤óÉîðõô©Å   ¤áóã ½ Áóã¨ÓôòéîçÍéä¨¤óÉîðõô¬ ¤é¬ ±©©Å    ¤ãèò ½ Ãèò¨¤áóã « ±²¸© » óèéæô ôï ôèå åøôåîäåä ãèáò óåôÅ   ¤ôåøô ½ ¤ôåøô ¦ ¤ãèòÅ ÎåøôÅ ¤èÏõôðõô ½ ÆéìåÏðåî¨¤æéìåßïõôðõô¬ ²© » ×òéôå éî Ïöåò÷òéôå íïäåÅ Æéìå×òéôå¨¤èÏõôðõô¬ ¤ôåøô©Å ÆéìåÃìïóå¨¤èÏõôðõô©Å Å » ÒåóôïòéîçÅ ¤æéìåßòåóôïòå ½ ÀÓãòéðôÄéò ¦ ¢Üòåóôïòå®ôøô¢Å ¤èÏõôðõô ½ ÆéìåÏðåî¨¤æéìåßïõôðõô¬ °©Å ¤óÏõôðõô ½ ÆéìåÒåáä¨¤èÏõôðõô©Å ¤ôåøô ½ ¢¢Å Æïò ¤ø ½ ± Ôï ÓôòéîçÌåî¨¤óÏõôðõô©Å     ¤áóã ½ Áóã¨ÓôòéîçÍéä¨¤óÏõôðõô¬ ¤ø¬ ±©©Å  ¤ãèò ½ Ãèò¨¤áóã ­ ±²¸© » óèéæô âáãëÅ   ¤ôåøô ½ ¤ôåøô ¦ ¤ãèòÅ ÎåøôÅ ¤èÒåóôïòå ½ ÆéìåÏðåî¨¤æéìåßòåóôïòå¬ ²©Å Æéìå×òéôå¨¤èÒåóôïòå¬ ¤ôåøô©Å ÆéìåÃìïóå¨¤èÒåóôïòå©`
Edited by SpookMeister

##### Share on other sites

Thank you all for the replys

##### Share on other sites

can anyone help me and tell me how to "Assign each letter a prime number".....i mean excactly....thanks

##### Share on other sites

Each letter is a bit vague.

If you insist on "each" and take "letter" to mean "possible character, independant of AutoIt", you end up with the very large number of possible characters being defined in Unicode reference documents.

If you mean "dependant on AutoIt string representation" your target is much smaller since AutoIt can only handle 65535 distinct characters.

If you mean "english letters" then it boils down to 26. But you may have to deal with your own language using a larger number of characters (like traditional chinese).

...

You see that depending on what you actually have in mind, the answer can vary tremendously.

BTW, Unicode v5.2 currently assigns 246943 codepoints. Making a table of primes up to the 246943th is extremely easy (we have that Prime[246943] = 3450383, not a large number). But consider that using a prime hash will likely force you to use a BigInt library (there is one available for AutoIt, use search feature), even for coding the english alphabet: Apply[Times,Prime[Range[26]]] = 232862364358497360900063316880507363070

While encoding with primes can be made, decoding is a bit trickier: not only you have to routinely factor larger-than-average integers(*), but you also have to rebuild the word from the letters it contains (nothing gives you any hint regarding the order of letters).

I don't want to waste forum bandwidth posting a list of primes up to any undecent limit.

(*) integers produced by this encoding are called "smooth" which means they only have small divisors. Trial divide is the best way to factor smooth integers.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

##### Share on other sites

You could also mangle things up at the bit level:

```#include <Array.au3>
\$sStr = "This is a string"
\$aStr = StringSplit(\$sStr, "")
_ArrayDisplay(\$aStr)
For \$i = 1 to \$aStr[0]
\$aStr[\$i] = BitNOT(BitRotate(Asc(\$aStr[\$i]), 3))
Next
_ArrayDisplay(\$aStr)
For \$i = 1 to \$aStr[0]
\$aStr[\$i] = Chr(BitRotate(BitNOT(\$aStr[\$i]), -3))
Next
_ArrayDisplay(\$aStr)```

Edit: switched from code to autoit tags, formatting still not retained... what's the trick?

Edited by Spiff59

##### Share on other sites

why not just use the _Crypt_EncryptFile and _Crypt_DecryptFile as it already does what you want - but if not you may get some ideas from these functions

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go."

"Everybody catches up with everyone, eventually"

"As you teach others, you are really teaching yourself."

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace \$ghGDIPDll with \$__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

# Ternary operator

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...