Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. String.fromCharCode and charCodeAt

String.fromCharCode and charCodeAt

Scheduled Pinned Locked Moved C#
csharpc++javascriptdata-structurestutorial
13 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tim_gunning
    wrote on last edited by
    #1

    hi all does anyone know how to convert the following js into c# ?

    my_str += String.fromCharCode((src.charCodeAt(c) ^ keys[cc]));

    which is contained in the following loop (for clarity):

    for(var c:Number = 0; c < src.length; c ++){
    if(cc>=i) {
    cc = 0;
    }
    my_str += String.fromCharCode((src.charCodeAt(c) ^ keys[cc]));
    cc++;
    }

    I've searched and found the equivalent for both but dont seem to be able to put it together, it should be something like this in c#:

    my_str += new String(new char[]{(Int32)Data[c] ^ keys[cc]});

    but that obviously fails and im just confusing myself now. any pointer very very much appreciated. keys is already converted into an array of ASCII codes and Data is a string at present, i was thinking I should convert it into an array of ASCII codes as well..... thank you tim

    T G 2 Replies Last reply
    0
    • T tim_gunning

      hi all does anyone know how to convert the following js into c# ?

      my_str += String.fromCharCode((src.charCodeAt(c) ^ keys[cc]));

      which is contained in the following loop (for clarity):

      for(var c:Number = 0; c < src.length; c ++){
      if(cc>=i) {
      cc = 0;
      }
      my_str += String.fromCharCode((src.charCodeAt(c) ^ keys[cc]));
      cc++;
      }

      I've searched and found the equivalent for both but dont seem to be able to put it together, it should be something like this in c#:

      my_str += new String(new char[]{(Int32)Data[c] ^ keys[cc]});

      but that obviously fails and im just confusing myself now. any pointer very very much appreciated. keys is already converted into an array of ASCII codes and Data is a string at present, i was thinking I should convert it into an array of ASCII codes as well..... thank you tim

      T Offline
      T Offline
      tim_gunning
      wrote on last edited by
      #2

      ignore ;p was having one of those moments when you cant see the woods for the trees (or whatever!)

      my_str += (char)(letters[c] ^ keys[cc]);

      does the job after creating the letters array using (Int32)input[i] in a for loop thanks anyway. t

      1 Reply Last reply
      0
      • T tim_gunning

        hi all does anyone know how to convert the following js into c# ?

        my_str += String.fromCharCode((src.charCodeAt(c) ^ keys[cc]));

        which is contained in the following loop (for clarity):

        for(var c:Number = 0; c < src.length; c ++){
        if(cc>=i) {
        cc = 0;
        }
        my_str += String.fromCharCode((src.charCodeAt(c) ^ keys[cc]));
        cc++;
        }

        I've searched and found the equivalent for both but dont seem to be able to put it together, it should be something like this in c#:

        my_str += new String(new char[]{(Int32)Data[c] ^ keys[cc]});

        but that obviously fails and im just confusing myself now. any pointer very very much appreciated. keys is already converted into an array of ASCII codes and Data is a string at present, i was thinking I should convert it into an array of ASCII codes as well..... thank you tim

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        You are on the right track, you just need a few more parentheses so that you are casting just the character instead the entire result. Don't use += to build the string, put all the characters in an array and create a string from that.

        char[] chars = new char[Data.Length];
        for (int c = 0; c < Data.Length; c++) {
        chars[c] = (char)(((int)Data[c]) ^ keys[cc++ % i]);
        }
        string my_str = new string(chars);

        Despite everything, the person most likely to be fooling you next is yourself.

        T 1 Reply Last reply
        0
        • G Guffa

          You are on the right track, you just need a few more parentheses so that you are casting just the character instead the entire result. Don't use += to build the string, put all the characters in an array and create a string from that.

          char[] chars = new char[Data.Length];
          for (int c = 0; c < Data.Length; c++) {
          chars[c] = (char)(((int)Data[c]) ^ keys[cc++ % i]);
          }
          string my_str = new string(chars);

          Despite everything, the person most likely to be fooling you next is yourself.

          T Offline
          T Offline
          tim_gunning
          wrote on last edited by
          #4

          hi guffa whats the difference between creating an array(then string) or building the string as we go. I dont mean that in a rude way ;) I'm intrigued as they produce two different results from what *should* be the same string or is it the char creation that differs the result ? tim

          G 1 Reply Last reply
          0
          • T tim_gunning

            hi guffa whats the difference between creating an array(then string) or building the string as we go. I dont mean that in a rude way ;) I'm intrigued as they produce two different results from what *should* be the same string or is it the char creation that differs the result ? tim

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            The difference is that if you use += to create the string, you are creating every intermediate string as a separate object. If your code creates the string "This is a test", it will also have created the strings "T", "Th", "Thi", "This", "This ", "This i", "This is", "This is ", "This is a", "This is a ", "This is a t", "This is a te" and "This is a tes". Strings in .NET are immutable. When you use the += operator, you are actually creating a new string object every time, copying the data from the original string. For very short strings, this is not a big problem, but it quickly gets bad. To create a 100 character string, you will have created 10 kB of strings. To create a 1000 character string, you will have created 1 MB of strings.

            Despite everything, the person most likely to be fooling you next is yourself.

            T N 2 Replies Last reply
            0
            • G Guffa

              The difference is that if you use += to create the string, you are creating every intermediate string as a separate object. If your code creates the string "This is a test", it will also have created the strings "T", "Th", "Thi", "This", "This ", "This i", "This is", "This is ", "This is a", "This is a ", "This is a t", "This is a te" and "This is a tes". Strings in .NET are immutable. When you use the += operator, you are actually creating a new string object every time, copying the data from the original string. For very short strings, this is not a big problem, but it quickly gets bad. To create a 100 character string, you will have created 10 kB of strings. To create a 1000 character string, you will have created 1 MB of strings.

              Despite everything, the person most likely to be fooling you next is yourself.

              T Offline
              T Offline
              tim_gunning
              wrote on last edited by
              #6

              hi thanks for that, thats very interesting and something I didn't realise. but... strangely enough that must be the way that javascript handles it (i think *gulp*) as the original way I was doing it : c#

              my_str += (char)(letters[c] ^ keys[cc]);

              produces exactly the same result as: js

              my_str += String.fromCharCode((src.charCodeAt(c) ^ keys[cc]));

              which was(after to base64 encoding for transport(the unencoded isn't really printable)):

              OhFBBxQWEBFLGgsEHgoVRBwSEAYCCw==

              and yours produced:

              OhhBBh4eCRNPEQkMBQQVVRgaHggBBQ==

              I was doing this as an little encryption thing between flash and .net as i couldn't find anything that was compatible, I tried many different ones (rijndael, TEA etc) but all the flash conversions of them produced completely different results from the c# end and I was tearing my hair out trying to do my own conversions of them so I thought i would write a simple (obviously not that strong - but it doesnt need to be super difficult) bit of encryption that i could rewrite in javascript(for the .as files in flash and also for php etc) that would suffice. but... like you say the larger the string then the larger the problem(or at least load) so now i'm wondering if theres a way to reproduce what you've said in js - this isn't a problem for what I'm doing now but it would be nice to have a better model in place for future. something for me over the weekend ;) thanks for your replies and if you have any thoughts I'd be really interested to hear them. tim i suppose in js i would just create an array and then use push to add them in and then create the string...but i still don't get why it produces different results from what *should* be the same string...hmmmm time for some script outputting...

              modified on Friday, July 4, 2008 8:33 PM

              G 1 Reply Last reply
              0
              • T tim_gunning

                hi thanks for that, thats very interesting and something I didn't realise. but... strangely enough that must be the way that javascript handles it (i think *gulp*) as the original way I was doing it : c#

                my_str += (char)(letters[c] ^ keys[cc]);

                produces exactly the same result as: js

                my_str += String.fromCharCode((src.charCodeAt(c) ^ keys[cc]));

                which was(after to base64 encoding for transport(the unencoded isn't really printable)):

                OhFBBxQWEBFLGgsEHgoVRBwSEAYCCw==

                and yours produced:

                OhhBBh4eCRNPEQkMBQQVVRgaHggBBQ==

                I was doing this as an little encryption thing between flash and .net as i couldn't find anything that was compatible, I tried many different ones (rijndael, TEA etc) but all the flash conversions of them produced completely different results from the c# end and I was tearing my hair out trying to do my own conversions of them so I thought i would write a simple (obviously not that strong - but it doesnt need to be super difficult) bit of encryption that i could rewrite in javascript(for the .as files in flash and also for php etc) that would suffice. but... like you say the larger the string then the larger the problem(or at least load) so now i'm wondering if theres a way to reproduce what you've said in js - this isn't a problem for what I'm doing now but it would be nice to have a better model in place for future. something for me over the weekend ;) thanks for your replies and if you have any thoughts I'd be really interested to hear them. tim i suppose in js i would just create an array and then use push to add them in and then create the string...but i still don't get why it produces different results from what *should* be the same string...hmmmm time for some script outputting...

                modified on Friday, July 4, 2008 8:33 PM

                G Offline
                G Offline
                Guffa
                wrote on last edited by
                #7

                How did you turn the string into a byte array, so that it could be base64 encoded? I think that the problem is that the codes are using different character sets. The characters outside the ASCII set uses different character codes in different character sets.

                Despite everything, the person most likely to be fooling you next is yourself.

                T 1 Reply Last reply
                0
                • G Guffa

                  How did you turn the string into a byte array, so that it could be base64 encoded? I think that the problem is that the codes are using different character sets. The characters outside the ASCII set uses different character codes in different character sets.

                  Despite everything, the person most likely to be fooling you next is yourself.

                  T Offline
                  T Offline
                  tim_gunning
                  wrote on last edited by
                  #8

                  hi in c# i used

                  Convert.ToBase64String(Encoding.ASCII.GetBytes(my_str))

                  and in js i used the base64 encoding i found in amongst the encryption files from http://labs.boulevart.be/index.php/2007/05/23/encryption-in-as2-and-as3/[^] which is where my problems began.... not that i'm putting down what they have there, I just couldn't get it to produce the same results from my end with the rijndael/TEa encryption, the base64 seems to be fine tim edit: this is the function that encodes to base64 in js:

                  \*/
                  private static var base64chars:String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
                  
                  /\*\*
                  \* Encodes a base64 string.
                  \*/
                  public static function encode(src:String):String {
                  	var i:Number = 0;
                  	var output:String = new String("");
                  	var chr1:Number, chr2:Number, chr3:Number;
                  	var enc1:Number, enc2:Number, enc3:Number, enc4:Number;
                  	while (i < src.length) {
                  		chr1 = src.charCodeAt(i++);
                  		chr2 = src.charCodeAt(i++);
                  		chr3 = src.charCodeAt(i++);
                  		enc1 = chr1 >> 2;
                  		enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
                  		enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
                  		enc4 = chr3 & 63;
                  		if(isNaN(chr2)) enc3 = enc4 = 64;
                  		else if(isNaN(chr3)) enc4 = 64;
                  		output += base64chars.charAt(enc1)+base64chars.charAt(enc2);
                  		output += base64chars.charAt(enc3)+base64chars.charAt(enc4)
                  	}
                  	return output;
                  }
                  
                  G 1 Reply Last reply
                  0
                  • G Guffa

                    The difference is that if you use += to create the string, you are creating every intermediate string as a separate object. If your code creates the string "This is a test", it will also have created the strings "T", "Th", "Thi", "This", "This ", "This i", "This is", "This is ", "This is a", "This is a ", "This is a t", "This is a te" and "This is a tes". Strings in .NET are immutable. When you use the += operator, you are actually creating a new string object every time, copying the data from the original string. For very short strings, this is not a big problem, but it quickly gets bad. To create a 100 character string, you will have created 10 kB of strings. To create a 1000 character string, you will have created 1 MB of strings.

                    Despite everything, the person most likely to be fooling you next is yourself.

                    N Offline
                    N Offline
                    N a v a n e e t h
                    wrote on last edited by
                    #9

                    Guffa wrote:

                    To create a 100 character string, you will have created 10 kB of strings. To create a 1000 character string, you will have created 1 MB of strings.

                    Great ! I am curious to know how you calculated this ?

                    All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

                    G 1 Reply Last reply
                    0
                    • N N a v a n e e t h

                      Guffa wrote:

                      To create a 100 character string, you will have created 10 kB of strings. To create a 1000 character string, you will have created 1 MB of strings.

                      Great ! I am curious to know how you calculated this ?

                      All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

                      G Offline
                      G Offline
                      Guffa
                      wrote on last edited by
                      #10

                      You just add the length of the strings, and multiply by two (as each character is two bytes). (1+2+3+4+...+98+98+99+100) * 2 = (101 * 50) * 2 = 10100 (1+2+3+4+...+998+999+1000) * 2 = (1001 * 500) * 2 = 1001000 The actual amount of memory used is slightly more, as there is some overhead in each object.

                      Despite everything, the person most likely to be fooling you next is yourself.

                      modified on Sunday, July 6, 2008 9:25 AM

                      N 1 Reply Last reply
                      0
                      • T tim_gunning

                        hi in c# i used

                        Convert.ToBase64String(Encoding.ASCII.GetBytes(my_str))

                        and in js i used the base64 encoding i found in amongst the encryption files from http://labs.boulevart.be/index.php/2007/05/23/encryption-in-as2-and-as3/[^] which is where my problems began.... not that i'm putting down what they have there, I just couldn't get it to produce the same results from my end with the rijndael/TEa encryption, the base64 seems to be fine tim edit: this is the function that encodes to base64 in js:

                        \*/
                        private static var base64chars:String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
                        
                        /\*\*
                        \* Encodes a base64 string.
                        \*/
                        public static function encode(src:String):String {
                        	var i:Number = 0;
                        	var output:String = new String("");
                        	var chr1:Number, chr2:Number, chr3:Number;
                        	var enc1:Number, enc2:Number, enc3:Number, enc4:Number;
                        	while (i < src.length) {
                        		chr1 = src.charCodeAt(i++);
                        		chr2 = src.charCodeAt(i++);
                        		chr3 = src.charCodeAt(i++);
                        		enc1 = chr1 >> 2;
                        		enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
                        		enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
                        		enc4 = chr3 & 63;
                        		if(isNaN(chr2)) enc3 = enc4 = 64;
                        		else if(isNaN(chr3)) enc4 = 64;
                        		output += base64chars.charAt(enc1)+base64chars.charAt(enc2);
                        		output += base64chars.charAt(enc3)+base64chars.charAt(enc4)
                        	}
                        	return output;
                        }
                        
                        G Offline
                        G Offline
                        Guffa
                        wrote on last edited by
                        #11

                        tim_gunning wrote:

                        in c# i used Convert.ToBase64String(Encoding.ASCII.GetBytes(my_str))

                        The ASCII encoding only handles characters with character code 0 to 127. To get the equivalent of the JScript code you would have to loop through the string and get the character codes into a byte array, so that you get the characters with character codes from 0 to 255. The JScript code claims to encode a string to base64, but that it not really possible. It assumes that there are never any character codes above 255. If there is, the result will be incorrect. To correctly turn a string into base64, you first have to encode it using an encoding that can handle the full unicode character set, like UTF-8: Convert.ToBase64String(Encoding.UTF8.GetBytes(my_str))

                        Despite everything, the person most likely to be fooling you next is yourself.

                        T 1 Reply Last reply
                        0
                        • G Guffa

                          You just add the length of the strings, and multiply by two (as each character is two bytes). (1+2+3+4+...+98+98+99+100) * 2 = (101 * 50) * 2 = 10100 (1+2+3+4+...+998+999+1000) * 2 = (1001 * 500) * 2 = 1001000 The actual amount of memory used is slightly more, as there is some overhead in each object.

                          Despite everything, the person most likely to be fooling you next is yourself.

                          modified on Sunday, July 6, 2008 9:25 AM

                          N Offline
                          N Offline
                          N a v a n e e t h
                          wrote on last edited by
                          #12

                          Thanks. :)

                          All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

                          1 Reply Last reply
                          0
                          • G Guffa

                            tim_gunning wrote:

                            in c# i used Convert.ToBase64String(Encoding.ASCII.GetBytes(my_str))

                            The ASCII encoding only handles characters with character code 0 to 127. To get the equivalent of the JScript code you would have to loop through the string and get the character codes into a byte array, so that you get the characters with character codes from 0 to 255. The JScript code claims to encode a string to base64, but that it not really possible. It assumes that there are never any character codes above 255. If there is, the result will be incorrect. To correctly turn a string into base64, you first have to encode it using an encoding that can handle the full unicode character set, like UTF-8: Convert.ToBase64String(Encoding.UTF8.GetBytes(my_str))

                            Despite everything, the person most likely to be fooling you next is yourself.

                            T Offline
                            T Offline
                            tim_gunning
                            wrote on last edited by
                            #13

                            hi originally I wss using UTF8 but the js wasnt coping and then I realised chars were missing from the c# end (anything over 127 or so) which I needed (long story....;p) anyway to cut it short I've encoded the js to UTF8 and the c# and then both to base64, used StringBuilder to create my return string and both the js in the flash and c# are producing identical results which is what I needed, I'll post it up here at some point as it may be of interest to someone. thanks for all your help/pointers guffa, very much appreciated. tim

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups