How do I convert character to ANSI number

 

Hi guys

Please can someone point me in the right direction here...

I have created a simple indicator using Wingding Arrows which are nested under an OBJ_LABEL and anchored in CORNER_1. Obviously, there are 3 different iterations of the arrow UP (241), Sideways (240) and Down (242).

My problem is that I am wanting to use the value of the current Arrow (241, 240 or 242) as a condition for an EA. I have been able to get the text value of the current arrow displayed as a comment on the chart so I know the value is changing according to the current arrow BUT... it is a Latin text character.

How do I convert this character back to its ANSI number so that I can use the value in my code?

PS: I have tried all the StringToChar/Double/Array/Integer Conversion Functions and nothing seems to work....

Any ideas would be appreciated.... 

 

hello,

i have barely done objects in MQL , if any, so you should take my words with a grain of salt :) Well... I suppose you can have the text (string / character) of the OBJ_LABEL by using ObjGetString() function; then, you just use a cast to integer, like

char character='b';

MessageBox( (int)(character) );

 

 Of course, "character" should be a single character for it to cast properly; that could mean that you may also cast the return string from ObjGetString() to a char, althought I  am not really sure that you have to do that if the string is only a single character

 PS. By re-reading your post, I realised that the problem may be in the fact that ObjGetString returns a string; then, you can not use StringToChar() or any cast, butt, you can use

StringGetCharacter() :)

PS2. a side note, MessageBox does not need an (int), as it seems that passing a character like MessageBox(character), will cast it all on its own to an int 

 

best regards 

 
Demos:

hello,

i have barely done objects in MQL , if any, so you should take my words with a grain of salt :) Well... I suppose you can have the text (string / character) of the OBJ_LABEL by using ObjGetString() function; then, you just use a cast to integer, like

 

 Of course, "character" should be a single character for it to cast properly; that could mean that you may also cast the return string from ObjGetString() to a char, althought I  am not really sure that you have to do that if the string is only a single character

 

best regards 

Hi Demos

Yes... I was thinking along the same lines. I was able to solve my problem with the following:

void OnTick()
  {
//---


//---
string ArrowProp = ObjectDescription("TrendArrow"); //Get Object text value
string ArrowNo;
string Up = "241";
string Sideways = "240";
string Down = "242";

   if(ArrowProp="ò")ArrowNo=Down;
   if(ArrowProp="ö")ArrowNo=Sideways;
   if(ArrowProp="ñ")ArrowNo=Up;
   
   
   Comment("5min Arrow = ",ArrowNo);
   
  }

 It works great as I can now use the "ArrowNo" variable in my code....

It just seemed strange that there was no Conversion Function to be able to do this quickly...

Anyways... thanx for the suggestion

MikeT 

 
Mike.T:

Hi Demos

Yes... I was thinking along the same lines. I was able to solve my problem with the following:

 It works great as I can now use the "ArrowNo" variable in my code....

It just seemed strange that there was no Conversion Function to be able to do this quickly...

Anyways... thanx for the suggestion

MikeT 

hey, no problem, just pay attention, you have wrote 

if(ArrowProp="ö")ArrowNo=Sideways; 

but I guess you mean if ==

 if(ArrowProp=="ö")ArrowNo=Sideways;

 

also, I  edited my first post, where you can see StringGetCharacter()  

 
Demos:

hey, no problem, just pay attention, you have wrote 

if(ArrowProp="ö")ArrowNo=Sideways; 

but I guess you mean if ==

 if(ArrowProp=="ö")ArrowNo=Sideways;

Good point.... thanx
 
Mike.T:
Good point.... thanx
ok, you may have missed that once more as I  keep editing my posts hehe what about StringGetCharacter
 
Demos:
ok, you may have missed that once more as I  keep editing my posts hehe what about StringGetCharacter

That actually did not work.... when I changed the code to "==" it returned ""

I get Warnings that the "expression not boolean"... but it works....so all's good I guess...

Cheers 

 

also, you have to put quotes on assigning a string to ArrowNo such as 

if (ArrowProp=="ö") ArrowNo="Sideways";  

 

but, I say again, check StringGetCharacter :)

 
Demos:

also, you have to put quotes on assigning a string to ArrowNo such as 

 

but, I say again, check StringGetCharacter :)

Aaaaargh... sorry... you are correct about the "=="

I had a look at the StringGetCharacter function but that is not really suitable here....unless I'm getting it wrong... The StringGetCharacter "finds" a character in a string where as the ObjectDescription returns the value of the Object...which is what I am after...

I'm not so sure about the Quotation marks you are suggesting as Down, Up and Sideways are declared variables... so they go as is...

Anyways... all's good... she works... all I gotta do now is put it all together... 

Thanx for your input...

MikeT 

 

Well, StringGetCharacter returns the ASCII code of a character from a string, (so character '1' will return 49), as per your original question. But in general, I believe you are right that StringGetCharacter is not appropriate for you particular problem, and that the approach you came after is better, where you check for equality to a character string.

As for the quotes, I see again your code and ok, of course you do not need quotes, i passed by and did not pay great attention 

 

best regards 

 
Demos:

Well, StringGetCharacter returns the ASCII code of a character from a string, (so character '1' will return 49), as in you original question. But in general, I believe you are right that StringGetCharacter is not appropriate for you particular problem, and that the approach you came after is better, where you check for equality to a character string.

As for the quotes, I see again your code and ok, of course you do not need quotes, i passed by and did not pay attention 

 

best regards 

Thanx Demos