Sign in to follow this  
Followers 0
XInFisk

Reading letters and convert to numbers

13 posts in this topic

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.

I'd be very glad with a fast answer :shocked:

-XInFisk

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

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.

I'd be very glad with a fast answer :shocked:

-XInFisk

FileOpen(),FileRead(),StringSplit, FileWrite() Edited by Generator

Share this post


Link to post
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)


[u]Helpful tips:[/u]If you want better answers to your questions, take the time to reproduce your issue in a small "stand alone" example script whenever possible. Also, make sure you tell us 1) what you tried, 2) what you expected to happen, and 3) what happened instead.[u]Useful links:[/u]BrettF's update to LxP's "How to AutoIt" pdfValuater's Autoit 1-2-3 Download page for the latest versions of Autoit and SciTE[quote]<glyph> For example - if you came in here asking "how do I use a jackhammer" we might ask "why do you need to use a jackhammer"<glyph> If the answer to the latter question is "to knock my grandmother's head off to let out the evil spirits that gave her cancer", then maybe the problem is actually unrelated to jackhammers[/quote]

Share this post


Link to post
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 this post


Link to post
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...?

:shocked:


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 this post


Link to post
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...?

:shocked:

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 this post


Link to post
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...

:shocked:


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 this post


Link to post
Share on other sites

#8 ·  Posted (edited)

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)
$sInput = FileRead($hInput)
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)
$sOutput = FileRead($hOutput)
$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

[u]Helpful tips:[/u]If you want better answers to your questions, take the time to reproduce your issue in a small "stand alone" example script whenever possible. Also, make sure you tell us 1) what you tried, 2) what you expected to happen, and 3) what happened instead.[u]Useful links:[/u]BrettF's update to LxP's "How to AutoIt" pdfValuater's Autoit 1-2-3 Download page for the latest versions of Autoit and SciTE[quote]<glyph> For example - if you came in here asking "how do I use a jackhammer" we might ask "why do you need to use a jackhammer"<glyph> If the answer to the latter question is "to knock my grandmother's head off to let out the evil spirits that gave her cancer", then maybe the problem is actually unrelated to jackhammers[/quote]

Share this post


Link to post
Share on other sites

Thank you all for the replys :shocked:

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
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 this post


Link to post
Share on other sites

#12 ·  Posted (edited)

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 this post


Link to post
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."

From my dad

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

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0