23 Replies Latest reply on Sep 2, 2009 8:46 PM by newFlexP

    How to cast integer to character using flex

    newFlexP

      Hi,

       

      Am a new person using flex..hope some one can help me to solve this problem

       

      The error is:

      Error #1034: Type Coercion failed: cannot convert 248 to flashx.textLayout.utils.CharacterUtil.

       

       

      I unable to convert integer to character. Below is my code:

       

       

      var s:String = iptusrpass.text;
              
                       var length:int = s.length;

       

                        var cl:int;
                        var result:String = "";
                        cl = length / 2 + length % 2;

       

                 
                       for (var i:int = 0; i < cl; i++) {
                      
                         var test1:String = s.charAt(i);
                         var s1:int = int(test1);
                         var s2:int;
                         var c1:int;
                         var c2:int;
                        
                      
                         if (cl + i > length - 1)
                         {
                             s2 = 0;
                         }
                         
                         else
                         {
                             var test2:String = s.charAt(cl + i);
                          s2 = int(test2);
                 
                        
                         }
                        
                         c1 = ((s1 & 0xF0) | (s2 & 0x0F)) ^ 0xF8;
                         c2 = ((s2 & 0xF0) | (s1 & 0x0F)) ^ 0xF8;

       

                           result += CharacterUtil(c1);
                           result += CharacterUtil(c2);

       

                        }
                       
                       password = StringUtil.trim(result);

       

       

       

      thk.

        • 1. Re: How to cast integer to character using flex
          Gregory Lafrance Level 6

          I'm not sure what CharacterUtil is, but does it's constructor support casting numbers? You may want to examine that class to see how it is used.

           

          If this post answers your question or helps, please mark it as such.

          • 2. Re: How to cast integer to character using flex
            Flex harUI Adobe Employee

            You can maybe use toString() or fromCharCode()

             

            Alex Harui

            Flex SDK Developer

            Adobe Systems Inc.

            Blog: http://blogs.adobe.com/aharui

            • 3. Re: How to cast integer to character using flex
              Barna Biro Level 3

              Or simply cast the integer to a string with: String(243); whenever you need to treat the interger as a string.

              • 4. Re: How to cast integer to character using flex
                msakrejda Level 4

                Barna Biro wrote:

                 

                Or simply cast the integer to a string with: String(243); whenever you need to treat the interger as a string.

                 


                To bring some pendantism to the discussion (everyone loves pedants, right?), String() is not a cast but a top-level function ( http://livedocs.adobe.com/flex/3/langref/package.html#String() ) that converts things to Strings. A cast to String would fail because an int is not a String. But the usage is still be the same.

                 

                --
                Maciek Sakrejda
                Truviso, Inc.
                www.truviso.com
                Truviso is hiring: http://www.truviso.com/company-careers.php?i=87

                • 5. Re: How to cast integer to character using flex
                  Barna Biro Level 3

                  If that makes you feel better, ok, but it's still called casting.

                   

                  Regarding the "failing part": you should at least try the things you say before posting. If you think it will fail for who knows what reason, it's your problem, the facts show that it won't fail because there's simply nothing that could make it fail. Cast your integer to String either using: String(234); or 234 as String and you'll be amazed to see that it's correctly valid and it returns 234 just as expected ( if you don't believe it's a string, then check with typeof and again, be amazed and see that it's of type string ).

                   

                  * Sarcasm *

                  • 6. Re: How to cast integer to character using flex
                    msakrejda Level 4

                        <mx:Label text="{'123 as String = \'' + (123 as String) + '\''}"/>

                     

                    (on my 3.0 SDK, that label reads "123 as String = 'null'")

                     

                    And when using the String() syntax, as I said before, you're not actually casting (at least not as that term is typically understood when talking about type systems). String (like Date, Boolean, and Number) has a global function defined with the same name as the type, so the Type(value)-style casts using this syntax aren't really traditional casts, they're more general type conversions.

                     

                    As I disclaimed in my last post, the distinction is largely pedantic, but I find it helpful to understand how this sort of thing works under the covers. I thought others would, too, so that's why I posted. I'm sorry if it came across as argumentative.

                     

                    --
                    Maciek Sakrejda
                    Truviso, Inc.
                    www.truviso.com
                    Truviso is hiring: http://www.truviso.com/company-careers.php?i=87

                    • 7. Re: How to cast integer to character using flex
                      Barna Biro Level 3

                      EDIT: Never mind. Let's just forget about it. There's no use in destroying this thread, but I do agree that the distinction is largely pedantic. Good luck.

                       

                      PS: This http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9 b90204-7f87.html might help clear things up a bit. Check out the "Casting to String" section ( or the whole article if you have time ).

                       

                      I also have to correct myself, the "as" keyword is a more "special case" ( so (213 as String) will indeed return "null" ). It would take too much to explain why it's "special" but if you have time, just surf the documentation ( all the answers can be found there ). Thanks for correcting me with that, I was too focused on the casting using String(value); and I accidentally slipped in the "as" part which is a more delicate subject.

                       

                      PPS:

                       

                      so the Type(value)-style casts using this syntax aren't really traditional casts, they're more general type conversions.

                       

                      In my dictionary, casting = type conversion, and from what I can see, the guys at Adobe are using the same disctionary as I do.

                       

                      Explicit conversion, which is also called casting , occurs when your code instructs the compiler to treat a variable of one data type as if it belongs to a different data type.

                      • 8. Re: How to cast integer to character using flex
                        dave cragg Level 2

                        I found your explanation useful, and less than pedantic. I've been caught out before by using (someInt as String) and have learned to use String(someInt) or someInt.toString() when I need to convert. It's good to know these things.

                        • 9. Re: How to cast integer to character using flex
                          msakrejda Level 4

                          Edit: wrote this up before I saw your answer--I'll leave it here in case anyone is interested.

                          Barna Biro wrote:

                           

                          Ohh, Jesus, if that's how you use binding or define values on certain objects then just ignore what I've said. I don't really think it can get much worse than that.

                           

                           

                          No, that's how I illustrate my arguments. My pedantry doesn't extend all the way to trivial examples. It shows what I intended: that "123 as String", which really *does* perform a cast, evaluates to null because an integer is not a String.

                           

                          "Traditional casts" ? Ok, now you're making me curious... Let's please see an example of a "traditional cast".

                           

                          By "traditional casts", I just mean actual type conversions based on an inheritance hierarchy. E.g., suppose you have

                           

                          class Animal {}

                          class Dog extends Animal { public var name:String; }

                          class RobotDog { public var name:String; }

                           

                          var myAnimal:Animal = new Animal();

                          var myDog:Dog = new Dog();

                           

                          // upcast follows; okay because Dog is an Animal

                          var alsoMyAnimal:Animal = Animal(myDog);

                           

                          // downcast follows; okay because this particular Animal (the statically declared type above)

                          // is in fact holding a reference to a Dog instance

                          var myOtherDog:Animal = new Dog();

                          var alsoMyOtherDog:Dog = Dog(myOtherDog);

                           

                          // The following is not a valid cast, since RobotDog is not really a dog (does not extend the Dog class),

                          // even though it may behave like a Dog.

                          var roboDog:RobotDog = new RobotDog();

                          var realDog:Dog = Dog(roboDog); // this will fail

                           

                          This is a pretty standard interpretation of casting (at least in object-oriented circles).

                          What ActionScript is doing with its String() function is, I believe, more or less analogous to

                           

                          function Dog(maybeADog:Object):Dog {

                            if (maybeADog is Dog) {

                              return maybeADog as Dog;

                            else if (maybeADog is RobotDog) {

                              var newDog:Dog = new Dog();

                              newDog.name = maybeADog.name;

                              return newDog;

                            } else {

                              throw new Error("Can't convert from " + maybeADog + " to Dog");

                            }

                          }

                           

                          which means that (unlike with what I call a traditional cast) the argument to String() does not have to be related to String in terms of inheritance.

                          • 10. Re: How to cast integer to character using flex
                            Barna Biro Level 3

                            What you are talking about is polymorphism and I don't think that it has too much to do with the initial question of the thread author ( it is related just that I think that we are wondering into different lands here ). What you wrote is totally valid, just that in my world, polymorphism is polymorphism and type conversion ( casting ) is type conversion ( it is true that usually, one implies the other but that's not always the case ). So, I believe that we are both right in our way.

                             

                            PS:

                             

                            var alsoMyAnimal:Animal = Animal(myDog);

                             

                            Totally valid just that if I'm not mistaking then you are explicitly casting myDog without a serious reason that would "force you" to do so. I mean: the compiler should recognize the relationship between the objects and it should implicitly cast myDog if needed ( if you explicitly casted myDog to make your point even clearer then it's totally valid ). But again, my memory might be playing tricks on me on this one...

                             

                            Either way, I think we have both made "a clear stand" and in essence, we are talking about the same thing just that we're kinda explaining it differently (  which is a bit weird ). I think we should stop here, the author of the thread now has more than enough information to get started ( if you want to continue the "debate" in a different thread, please go ahead, I'll be right behind you - although, as I said, I believe that we're both talking about the same thing just that we didn't find the correct "communication channel" yet, that would help avoid confusion in our statements ). Good luck.

                            • 11. Re: How to cast integer to character using flex
                              newFlexP Level 1

                              Thank for the reply...

                               

                               

                              But my isues is, I wan to convert the integer to a character. like an ASCII code.

                               

                               

                              For example..

                              when a integer is 53

                              Then i convert it to character it will become a character  =  A.

                               

                               

                              But in flex4 using CharcaterUtil it just able to support till 128 ASCII code only..i nt sure is true or not.

                               

                              Got someone have a idea?

                               

                              Thk

                              • 12. Re: How to cast integer to character using flex
                                Flex harUI Adobe Employee

                                String.fromCharCode()

                                 

                                Alex Harui

                                Flex SDK Developer

                                Adobe Systems Inc.

                                Blog: http://blogs.adobe.com/aharui

                                • 13. Re: How to cast integer to character using flex
                                  newFlexP Level 1

                                  thk flex harUI

                                   

                                  I till need need your help.

                                   

                                  Problem :

                                  how to get a character from a string and convert the character to integer.

                                   

                                  example string ="halo"

                                  get a specific character at position 1 is a

                                  then convert a to ASCII code..

                                   

                                  hope you can help me..

                                  thank a lot

                                  • 14. Re: How to cast integer to character using flex
                                    christopher.mina

                                    This gives you the ASCII code at index 0 of the string (ie: "h");

                                    var str:String = "halo";

                                    var asciiCode:Number = str.charCodeAt(0);

                                    • 15. Re: How to cast integer to character using flex
                                      newFlexP Level 1

                                      Thk for answering my question

                                       

                                      I have found another issues


                                      not all the ascii code can convert to character using String.fromCharCode().

                                       


                                      Example:

                                      ascii code 136 is unable to be convert to the exact character that is ^

                                      Using String.fromCharCode()

                                      It will replace by ? character.

                                       

                                      Ascii code reference:

                                      http://www.asciitable.com/

                                       

                                       

                                      Hope someonce can help me.

                                      thk

                                      • 16. Re: How to cast integer to character using flex
                                        christopher.mina Level 1

                                        This function is used to convert a string value to an ascii code.  If you

                                        run this:var str:String = "^";

                                        var code:Number = str.charCodeAt(0);

                                        trace(code.toString());

                                         

                                        you will see that the ascii character is 94.

                                         

                                        -C

                                        1 person found this helpful
                                        • 17. Re: How to cast integer to character using flex
                                          newFlexP Level 1

                                          Thk for the reply..

                                          94 is dec type in ascii code is equal to  ^ that is correct.

                                          .

                                          but why ascii code 136 will nt convert to any character when using

                                          String.fromCharCode()

                                           

                                           

                                          but in deplhi,java applet able to convert to ^, but in flex having this problem..

                                           

                                          I using this type of method is for custom encryption.


                                          Example:

                                           

                                          From a text field in string form then will get the character and convert it to ascii code.

                                           

                                          var s1:int = str.charCodeAt(0);

                                          s2:int  0

                                          from the ascii code will go trought this process

                                           

                                          var c1:int  = ((s1 & 0xF0) | (s2 & 0x0F)) ^ 0xF8

                                          var c2:int  = ((s2 & 0xF0) | (s1 & 0x0F)) ^ 0xF8;

                                           

                                          var result:String =  String.fromCharCode(c1)

                                           

                                           

                                           

                                          Hope can help me..

                                          thk

                                          • 18. Re: How to cast integer to character using flex
                                            paul.williams Level 4

                                            The fromCharCode() function expects a decimal value representing a

                                            unicode character-code. The first 256 characters in unicode are known as

                                            Latin-1. Of these the initial 128 correspond to the Ascii character set.

                                            Within the Latin-1 range, the characters from decimal 127 to decimal 159

                                            are all non-displayable. Since 136 is in the non-displayable range,

                                            Flash player substitutes a '?' when renderering the character.

                                             

                                            If it is working in Java and Delphi then you must be converting your

                                            value from octal to decimal at some point. You'll need to do something

                                            similar in ActionScript.

                                            1 person found this helpful
                                            • 19. Re: How to cast integer to character using flex
                                              newFlexP Level 1

                                              thk for the reply.

                                               

                                              You are correct, i have check it but till having the problem

                                              even i have change from oct to dec. the ascii code from 127-159 is till nt displayed even i have change to dec...

                                               

                                              Below is the code:

                                               

                                              var s:String = iptusrname.text;

                                              s1 = s.charCodeAt(i);

                                              c1 = ((s1 & 0xF0) | (s2 & 0x0F)) ^ 0xF8;

                                               

                                              var ij:int= parseInt(c1,8);

                                               

                                              // comment when the ij is from 127-159 ascii code it will unable to convert to character and display as ? this is error.

                                               

                                              var str1:String = String.fromCharCode(ij)
                                                                 
                                              trace("str1="+str1);

                                               

                                               

                                               

                                               

                                              Waiting someonce to reply..

                                              thk

                                              • 20. Re: How to cast integer to character using flex
                                                paul.williams Level 4

                                                This is not an error. The characters in unicode range of decimal 127 to

                                                decimal 159 are NOT displayable. If your value of ij is between decimal

                                                127 and decimal 159 then it will not convert to a displayable character.

                                                 

                                                I'm also not clear why you think the expression below has an octal

                                                result. I can't see why it should.

                                                 

                                                c1 = ((s1 & 0xF0) | (s2 & 0x0F)) ^ 0xF8;

                                                 

                                                As it stands, this algorithm can be expected to produce character codes

                                                in the non-displayable range. If you have it working in another

                                                language, can you post the source?

                                                • 21. Re: How to cast integer to character using flex
                                                  newFlexP Level 1

                                                  thk for the reply...

                                                   

                                                  Issues is how to convert the unicode range of decimal 127 to decimal 159 are NOT displayable but in dephi it able to display.

                                                   

                                                  because I need do encryption in front end so that can be decrypt by the middle end which using dephi source code which develop by another developer.

                                                  I have found that in java they  using some unicode library to convert it to character is a type of tools provide by java open source.

                                                   

                                                   

                                                  Below is the source code of delphi it, it purpose is for encryption the note is the trace of the variable.

                                                   

                                                  function EncryptString(Key:integer; const s:st255):st255;
                                                  var  L,CL,i,c1,c2,s1,s2 :integer;
                                                  begin
                                                    if Key = 0 then
                                                      begin
                                                        L := length(s); Result := '';
                                                        if L mod 2 > 0 then CL := L div 2 + 1
                                                          else CL := L div 2;
                                                        i := 1;

                                                        // NOTE CL = 1 here

                                                   

                                                         
                                                        while i <= CL do begin
                                                          s1 := ord(s[i]);


                                                      // NOTE s1 = 116 here

                                                   

                                                          if CL+i > L then s2 := 0 else s2 := ord(s[CL+i]);

                                                       // s2 = 0

                                                   

                                                          c1 := ((s1 and $F0) or (s2 and $0F)) xor $F8;
                                                          c2 := ((s2 and $F0) or (s1 and $0F)) xor $F8;

                                                   

                                                      // NOTE c1 = 136 c2 = 252 here


                                                          Result := Result + chr(c1) + chr(c2);

                                                   

                                                      // NOTE  chr(c1) = ^      chr(c2) = ü

                                                   

                                                        inc(i);
                                                        end;
                                                      end
                                                  end

                                                   

                                                   

                                                   

                                                  below source code in flex:

                                                         
                                                               if (StringUtil.trim(iptusrname.text) !=""){
                                                                  
                                                                  
                                                                   var s:String = iptusrname.text;
                                                                   trace( iptusrname.text);
                                                                  
                                                                   var L,CL,c1,c2,s1,s2:int;
                                                                  
                                                                   L = s.length;               

                                                                   trace("L="+ L);
                                                                   var result:String ="";
                                                                   if (L%2 > 0)
                                                                   {
                                                                        CL= L/2 + 1 ;
                                                                        trace("CL="+ CL);
                                                                    }
                                                                    else
                                                                    {
                                                                      CL = L/2 ;
                                                                      trace("CL="+ CL);
                                                                    }
                                                                                   
                                                                   for (var i:int =0; i <= CL; i++) {
                                                                  
                                                                    s1 = s.charCodeAt(i);
                                                                   
                                                                     trace("s1="+ s1);
                                                                  
                                                                     if (CL + i > L)
                                                                     {
                                                                         s2 = 0;
                                                                     }
                                                                     
                                                                     else
                                                                     {
                                                                      s2 =int( s.charAt(CL + i));
                                                                       trace("s2="+ s2);
                                                             
                                                                    
                                                                     }
                                                                    
                                                                     c1 = ((s1 & 0xF0) | (s2 & 0x0F)) ^ 0xF8;
                                                                     c2 = ((s2 & 0xF0) | (s1 & 0x0F)) ^ 0xF8;
                                                                     trace("c1="+c1);
                                                                     trace("c2="+c2);
                                                                    
                                                                     // var ij:int= parseInt(c1,8);    // note convert oct to dec
                                                                     // var str1:String = String.fromCharCode(ij)
                                                                    //  trace("str1="+str1);
                                                                     
                                                                     
                                                                      var iz:String= String.fromCharCode(c1);
                                                                      trace("iz="+iz);
                                                                      result = result + String.fromCharCode(c1)+ String.fromCharCode(c2);
                                                                      trace("result="+result);

                                                   

                                                                    }

                                                   

                                                   

                                                   

                                                   

                                                  Reference website

                                                  http://www.webmasterworld.com/forum21/7301.htm

                                                  http://forums.devshed.com/java-help-9/extended-ascii-characters-53373.html

                                                  • 22. Re: How to cast integer to character using flex
                                                    paul.williams Level 4

                                                    Ok - looks like your Delphi code is following WinLatin-1 for its

                                                    character conversion. In this character set, the circumflex accent (^)

                                                    character is at decimal 94 (0x5E), but there is a variant of the

                                                    character at decimal 136 (0x88):

                                                     

                                                    http://wellington.pm.org/archive/200707/unicode/slide10.html

                                                     

                                                    Flash follows Latin-1, which is slightly different. You could write your

                                                    own functions for mapping Latin-1 character codes to and from WinLatin-1

                                                    character codes. The mappings are defined here:

                                                     

                                                    http://www.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP1252.TXT

                                                     

                                                    As far as I can see the differences are in the 127 - 159 range, so you

                                                    may only need to do some extra work for these codes.

                                                    • 23. Re: How to cast integer to character using flex
                                                      newFlexP Level 1

                                                      Thk for the reply,

                                                       

                                                      I will like to know how could i write own functions for mapping Latin-1 character codes to and from WinLatin-1

                                                      character codes.

                                                       

                                                      Can show me some source code or how to mapping it. Because i nt so gd it coding just begineer.

                                                       

                                                      Hope you able to help me..

                                                      Thk you, sry for late respond too