11 Replies Latest reply: Apr 4, 2008 6:25 PM by 807591 RSS

    ASCII code to string

    807591
      Hi.
      I've been searching a lot about this, and I've only seen methods to get a character from his ASCII code, but I want to get a String from his ascii code.
      I mean, if I have the int (or long) 104101108108111 this is the String "hello".

      If I have a Character, it's very simple: int i = 64; String end = (char) i;

      I've tried to do this:
      String end = "";
              Long b = new Long("104101108108111");
              byte[] c = b.toString().getBytes("US-ASCII");
              for(Byte d: c)
                  end += c.toString();
              
              return end;
          }
      But it returns something strange
      Thnsk you very much
        • 1. Re: ASCII code to string
          DrClap
          if I have the int (or long) 104101108108111 this is the String "hello"
          This is the "something strange" part for me. Could you explain how that works and what ASCII has to do with it? We'll go into "Why ASCII?" later.
          • 2. Re: ASCII code to string
            807591
            Sometimes I just think "what the hell is this poster doing, I mean besides crack cocaine". Then I get it:

            "104101108108111" = "104" + "101" + "108" + "108" + "111" = 'h' + 'e' + 'l' + 'l' + 'o'.

            I assume every letter will be represented by a three digit substring. Break the string in substrings and either use a Map to translate "104" to 'h', etc... or parse "104" to the int 104 and then cast it to a char.

            edit: whoops. I didn't see that 104101108108111 was a long, not a string. Even weirder. You can extract the codes for individual characters by using -- % 1000 and --- / 1000.

            Edited by: BigDaddyLoveHandles on Apr 4, 2008 1:50 PM
            • 3. Re: ASCII code to string
              807591
              Hi.
              DrClap: The strings in computer level are bits in some "format". For ASCII, you can see here: http://www.easycalculation.com/ascii-hex.php
              As BigDaddyLoveHandles said: "104101108108111" = "104" + "101" + "108" + "108" + "111" = 'h' + 'e' + 'l' + 'l' + 'o'.

              BigDaddyLoveHandles: The problem is that there characters, like "a" = 97 (only 2 digits)

              I thought there was a method in Java to do this. I've an alternative methos: convert from ascii to hex, and for ech 2 digits, converts it into character. But I think that there is a method or class with a static method to get the string of a ASCII value.

              Edited by: darkxer0x on Apr 4, 2008 2:02 PM
              • 4. Re: ASCII code to string
                807591
                This is no built-in method to do what you want, because what you want is quite odd.
                • 5. Re: ASCII code to string
                  807591
                  darkxer0x wrote:
                  BigDaddyLoveHandles: The problem is that there characters, like "a" = 97 (only 2 digits)
                  Okay. The printable ASCII characters are in the range 32-127. So if you see

                  XXX...XXX0X or
                  XXX...XXX1X or
                  XXX...XXX2X

                  Then you know the last code is three digits long. Otherwise it is two digits long.
                  • 6. Re: ASCII code to string
                    807591
                    This is no built-in method to do what you want, because what you want is quite odd.
                    ��?? And how do you send information or encrypt information? You must take the information in blocks, encrypt, and then decrypt it. And to do this the only way is to "convert" the information in ascii, utf-8, iso-***... and then to bits, to do operation bits (for encyption).
                    My only purpose is to get some information (a string), to encrypt, send it, decrypt it, and finally convert the ascii code to a String.
                    I think this is no odd..., because I don't know other way to do this.
                    • 7. Re: ASCII code to string
                      807591
                      darkxer0x wrote:
                      I think this is no odd..., because I don't know other way to do this.
                      I can only think "famous last words". You need to take about a dozen steps back and start again: what are you trying to do? What is your goal? I seriously doubt that a proper analysis will lead to you trying to turn the long 721011081081113287111114108100 into the string "Hello World".
                      • 8. Re: ASCII code to string
                        DrClap
                        There are plenty of ways to encrypt and decrypt information in Java. But that isn't one of them because it isn't encryption. Okay, it's sort of encryption but it's such a lame cipher it isn't even worth considering for real-life encryption. Maybe for encoding, so you can put control characters into text, but that's about the only use I can see for it. And I would use Base64 if that was my problem.
                        • 9. Re: ASCII code to string
                          807591
                          I have this algorithm (not made by me):
                          static public long xCrypt(long block, int key) {
                                long lkey = key < 0 ? ((long) (key)) + 0x80000000 : key;
                                lkey &= 0x3FFFFFFF;
                                long skey1 = (lkey >> 16) | (lkey << 16);
                                long skey2 = (lkey << 2) | (lkey >> 28);
                                long rblock = (((block + skey1) ^ skey2) & 0xFFFFFFFF) - skey1;
                                return rblock < 0 ? rblock + 0x80000000 : rblock;
                          }
                          It can encrypt and decrypt (if you pass a long with a key twice, you obtain the first long).
                          And I have some encrypt messages :
                           The encryption algorithm is encrypting data in blocks of 32 bits using
                          a key length of 30 bits. An interesting aspect the spies noted is that Alice seems to use the same
                          algorithm for encryption as well as for decryption. Since the smugglers are probably operating
                          all over the world they are most likely communicating using English text messages which are
                          represented by standard ASCII characters for compatibility.
                          08C7452B
                          
                          0F957A7D
                          
                          5999707E
                          
                          16DD7A19
                          
                          5ADC6665
                          
                          14C77045
                          
                          0BD97668
                          .....................
                          And my only way is to decrypt with a key (brute force), and if one of these messages is a string that I know, the key is OK.
                          I have to convert the 32 bits in hexadecimal to a long (decimal obviously), decrypt, and then convert it into a string.
                          • 10. Re: ASCII code to string
                            807591
                            UTF-8 is a super-set of standard ASCII, so you could copy 4/8 bytes from each word into a byte array and create a string with the UTF-8 charset.
                            Or you can use a string buffer and append the bytes. You probably want to use the string buffer approach if your message is not of a fixed size.
                            Eg:
                            public class LongBytesToString {
                              public static void main (String...args) {
                                // construct a long with the appropriate bytes, network order
                                // there are 64 bits in the long, each byte is 8, so you shift characters by
                                // 7*8, 6*8, ... 0*0 if you have all 8. Zeros are ignored. 
                                // values need to be longs as we're shifting them higher than ints go.
                                long data = (104L << 56) | (101L << 48) | (108L << 40) | (108L << 32) | (111L << 24);
                                
                                System.out.println(Long.toString(data, 16));
                                System.out.println(stringFromLong1(data));
                                System.out.println(stringFromLong2(data));
                              }
                              
                              static String stringFromLong1 (long data) {
                                try {
                                  // create a byte buffer and a UTF-8 string
                                  byte[] buffer = new byte[8];
                                  
                                  // copy the bytes of the long into the buffer
                                  for (int i = 0; i < 8; ++i)
                                    buffer[i] = (byte)(data >>> (56 - 8 * i));
                                  
                                  // create a string
                                  return new String(buffer, "UTF-8");
                                } catch (java.io.UnsupportedEncodingException ex) {
                                  // as encoding is passed as a string, compiler thinks that it can't be safe, 
                                  // even though UTF-8 is guaranteed to be supported
                                  return "";
                                }
                              }  
                              
                              static String stringFromLong2 (long data) {
                                // create a StringBuilder and pass each byte as a character to it
                                StringBuilder buffer = new StringBuilder(8);
                                
                                // copy the bytes of the long into the buffer,
                                // making sure they are low ASCII chars (0x7f)
                                for (int i = 0; i < 8; ++i)
                                  buffer.append((char)(0x7f & (data >>> (56 - 8 * i))));
                                
                                // create a string
                                return buffer.toString();
                              }
                            } 
                            • 11. Re: ASCII code to string
                              807591
                              Wuah, pm_kirkham. It works perfect!!