[Closed] How To Encode String To Number And Decode Number To String
hello my name is Nam , i com from vietnam , i am a newbie in maxscript , i found a simple script to encode string to number , but i dont know how to decode from number to string , can you help me , thank you very much
macid = ((((dotNetClass “System.Net.NetworkInformation.NetworkInterface”).GetAllNetworkInterfaces())[1].getPhysicalAddress()).tostring())
encodingString = “abcdefghijklmnopqrstuvwxyz1234567890”
fn encodeString stringToEncode =
(
encodedSysID = “”
for i = 1 to stringToEncode.count do
(
charIndex = findstring encodingString stringToEncode[i]
encodedSysID += charIndex as string +” “
)
return encodedSysID
)
encodeString macid
first of all for string with only digits you can used a simple cast to integer
"12345" as integer
any integer (and all other built-in value classes as well) you can cast to string
1234 as string
now about your case. you probably want to get all end digits of a string and convert them to integer…
first we have to get ending substring which contains only digits:
fn getStringID name = (substring name ((trimright name "0123456789").count+1) -1)
str_id = getStringID "absdef12345"
after that we can cast the string that contains only digits to the Integer or Int64 if the number is larger than max integer.
num_id = str_id as integer
Just use a simple casting “as integer” or “as integer64”
also you can find several examples on this forum how to use Regex and .NET for string operation including getting leading or ending digits.
thank you for your quick reply , my english is not good ,maybe i used google translate and you don not know what i mean , i want convert mac adress to a new number array and i do not know how to convert that number array to mac adress again ? can you help me ? thank you very much
i use my script convert my mac adress : “64809932747C” , after convert : “32 30 34 36 35 35 29 28 33 30 33 3 “
how i convert “32 30 34 36 35 35 29 28 33 30 33 3 ” to mac adress ?
Really a strange need…
Here you go:
fn decodeString str =
(
encodingString = "abcdefghijklmnopqrstuvwxyz1234567890"
decodedString = ""
charCodes = filterString str " " splitEmptyTokens:false
for ch in charCodes do decodedString += encodingString[ch as integer]
return decodedString
)
-- TEST
decodeString "32 30 34 36 35 35 29 28 33 30 33 3 "
If you need to convert any MacID to its real integer value and viceversa:
MacID = "64809932747c"
MacIDasInt64 = ("0x" + MacID + "L") as integer64 -- Convert Hexadecimal to Decimal
Int64asMacID = trimRight (bit.intAsHex MacIDasInt64) "L" -- Convert Decimal to Hexadecimal
Thank you very much , i can do it , i create some scripts , i want to make some keys
Best regard