Convert string to single number [Resolved]

Just starting out? Need help? Post your questions and find answers here.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Convert string to single number [Resolved]

Post by Kwai chang caine »

Hi at all :D

I want use compiler condition, because i have the same PBI, one time for a DLL and one time for an EXE
The problem is that in this PBI i have a "END"
And "END" not can be in a DLL :(

So i use CompilerSelect for exit with END if it's an EXE, and other way for a DLL.
Compiler condition need only numerical value, and me i want use string value

So i have the idea to convert the sentence i want to use, to numerical :idea:

At your mind, what is the more simple way for convert a sentence to the smaller number. :roll:
I can convert letter by ASCII value, but the result is surely too long :cry:
This number must be single for not have two sentence for the same number :?

Thanks and good day
Last edited by Kwai chang caine on Thu Aug 26, 2010 2:40 pm, edited 1 time in total.
ImageThe happiness is a road...
Not a destination
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

Re: Convert string to single number

Post by cas »

Kwaï chang caïne wrote:At your mind, what is the more simple way for convert a sentence to the smaller number.
In what range that number needs to be? Only positive numbers?
My first idea was to use CRC32Fingerprint.

Code: Select all

MyString.s="test"
Debug CRC32Fingerprint(@MyString.s,StringByteLength(MyString.s))
But if you want to use that number with CompilerIf then you are doing it wrong way :)
Just add this constant to your dll main source file:

Code: Select all

#DLL=1
and add this in pbi include:

Code: Select all

CompilerIf Defined(DLL, #PB_Constant)
  End
CompilerElse
  ;nothing
CompilerEndIf
Last edited by cas on Thu Aug 26, 2010 10:59 am, edited 1 time in total.
User avatar
ar-s
Enthusiast
Enthusiast
Posts: 344
Joined: Sat Oct 06, 2007 11:20 pm
Location: France

Re: Convert string to single number

Post by ar-s »

Hello Kwai,
Why don't you use replacestring()..
abcd => 10 11 12 13
don't use 0 until 9, that will be easier to decode 2 num by 2 num...
i don't know if there is an eseayer way..

----edit----

cas's idea is a good way ;)
~Ar-S~
My Image Hoster for PB users
My webSite (french) with PB apps : LDVMULTIMEDIA
PB - 3.x / 5.7x / 6 - W11 x64 - Ryzen 7 3700x / #Rpi4

Code: Select all

r3p347 : 7ry : un71l d0n3 = 1
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

Re: Convert string to single number

Post by cas »

Kwaï chang caïne wrote:So i use CompilerSelect for exit with END if it's an EXE, and other way for a DLL.
Oh, i switched those two in my previous post, but not important, main part is CompilerIf Defined(DLL, #PB_Constant) that checks if you have #DLL constant in your code which imports that pbi.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Convert string to single number

Post by Kwai chang caine »

Thanks at you two for your quick answer 8)

@ARS, because like ASCII, it's surely not the better short result :|

@CAS you have right, i need always a positive number, and i have see that your code give negative result :cry:
You have again right, the more simple method, is "#Dll = 1" and i use it actually.
But in my program, i ask what is the type of the file, and the procedure send the word "DLL" or "EXE" and i want use this result for two things.

I say to me, if i create two constant, that's works
#Kcc_EXE = 1
#Kcc_DLL = 2

But, when i ask my question, the return is "1" or "2", and i'm forced to stored somewhere

Code: Select all

If Result = 1
 A$ = "EXE"
ElseIf Result = 2
 A$ = "DLL"
EndIf
My idea is ...like i'm forced to use number, how don't put in this constant the value of "DLL" and "EXE", like this, for example :

Code: Select all

#Kcc_EXE = 53893587375353737
A$ = FunctionOfTheDead(#Kcc_EXE)
Debug A$ = "EXE"
Like this....i have my number for the compilerIf of PB, and he is very happy, and kiss me.... and my answer in String for me :mrgreen: :idea:

The idea of CRC32Fingerprint, is the style of idea that i search, because i don't know anything at all this MD5, CRC32....etc... :oops:
I want just a function who return a positive number..if she exist, or perhaps someone have created this style of code...somewhere :roll:
ImageThe happiness is a road...
Not a destination
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Convert string to single number

Post by Trond »

Something like this?

Code: Select all

Procedure.s FunctionOfTheDead(V.q)
  ProcedureReturn PeekS(@V, 8)
EndProcedure

#KCC_Exe = 'EXE' ; Type the string in reverse, three characters max
#KCC_Dll = 'LLD' ; Type the string in reverse, three characters max

#KCC_Type = #KCC_Dll
; #KCC_Type = #KCC_Exe

CompilerSelect #KCC_Type
  CompilerCase #KCC_Exe
    Debug "Selected EXE"
  CompilerCase #KCC_Dll
    Debug "Selected DLL"
CompilerEndSelect

Debug FunctionOfTheDead(#KCC_Type)
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Convert string to single number

Post by Kwai chang caine »

Hello MASTER TROND glad to talk to you :D

Waouuuaaahh !!!! i have not all understand, but your tips seems genial :shock:

I have write
Debug '0'
I have 48 like result so the ASCII of 0 :D

After i have write
Debug '01'
I have 12337, apparently it's a number who want to say 01, in something that i don't know what :roll:

It's cool like idea, obviously "12337" is more long that "4849" (the ASCII convert) but this function is managed by PB, not need to write it.
What is the reverse of "12337" ???
Is it possible to return "01" without pass by the memory ??? :roll:
ImageThe happiness is a road...
Not a destination
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

Re: Convert string to single number

Post by cas »

Very cool idea, Trond.
If i see it right, you can type 4 characters max if you compile in unicode and 8 characters max without unicode. We don't need 4th(8th in non-unicode) character to be zero because there is explicitly said for PeekS to go max 8 bytes from @V.
Kwaï chang caïne wrote:After i have write
Debug '01'
I have 12337, apparently it's a number who want to say 01, in something that i don't know what :roll:
Just play a little with it and you will get it ( start with small one digit numbers and letters.... :wink: ):

Image
Kwaï chang caïne wrote: It's cool like idea, obviously "12337" is more long that "4849" (the ASCII convert) but this function is managed by PB, not need to write it.
What is the reverse of "12337" ???
Is it possible to return "01" without pass by the memory ??? :roll:
But you will need extra variables. Trond's way is simpler, you can use this to get string back from number:

Code: Select all

val.q=12337
Debug ReverseString(PeekS(@val,8))
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Convert string to single number

Post by Kwai chang caine »

Thanks a lot CAS for your nice explanation 8)

It's a pity, i see it's not possible to use this tips with Constant :(

Code: Select all

#Kcc_Type=12337
Debug ReverseString(PeekS(@#Kcc_Type,8))
ImageThe happiness is a road...
Not a destination
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

Re: Convert string to single number

Post by cas »

Just create procedure:

Code: Select all

Procedure.s Convert(val.q)
  ProcedureReturn ReverseString(PeekS(@val.q,8))
EndProcedure

#Kcc_Type=12337
Debug Convert(#Kcc_Type)
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Convert string to single number

Post by Kwai chang caine »

Ok thanks CAS, i believe that this time i have understand :wink:

It's funny, but this discussion thinking me that a constant have no adress ??? :shock:
It's easy to have an adress of a variable "@Variable"...but for a constant it's not the same thing apparently :roll:
Yet a constant is surely writing somewhere in the memory ??? :roll:
ImageThe happiness is a road...
Not a destination
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

Re: Convert string to single number

Post by cas »

All constants are replaced with their values when you compile your program so your code:

Code: Select all

PeekS(@#Kcc_Type,8)
will be like this when compiling:

Code: Select all

PeekS(@12337,8)
which doesn't make sense because '@' is used to get address of variable in memory and we put number there, not variable name.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Convert string to single number

Post by Kwai chang caine »

Aaahh ok :shock:
It's very difficult for the beginner to understand this story of before and after compilation :|

In fact a constant is nowhere and everywhere in the same time :roll:
So for this reason a constant not have personal adress, because she leave inside the house of everybody :lol: :lol:

Thanks a lot for your precious help and explanation CAS 8)
And thanks two at TROND and his devil tips :lol:

I wish you a very good day, with too much sun and flowers :mrgreen:
ImageThe happiness is a road...
Not a destination
Post Reply