Jump to content
  • Advertisement
Sign in to follow this  
kooktroop

[java] String to long[] and back

This topic is 4823 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm looking to convert a String to an array of longs and back again. The following code has been tried, but doesnt work correctly.
public static String longToString( long[] source )
        {
            byte[] bytes = new byte[ source.length * 8 ];
            int b = 0;
            for ( int i = 0; i < source.length; i++, b += 8 )
            {
                bytes[ b ] = (byte)( source[ i ] & 0xFF );
                bytes[ b + 1 ] = (byte)( ( source[ i ] >> 8 ) & 0xFF );
                bytes[ b + 2 ] = (byte)( ( source[ i ] >> 16 ) & 0xFF );
                bytes[ b + 3 ] = (byte)( ( source[ i ] >> 24 ) & 0xFF );
                bytes[ b + 4 ] = (byte)( ( source[ i ] >> 32 ) & 0xFF );
                bytes[ b + 5 ] = (byte)( ( source[ i ] >> 40 ) & 0xFF );
                bytes[ b + 6 ] = (byte)( ( source[ i ] >> 48 ) & 0xFF );
                bytes[ b + 7 ] = (byte)( ( source[ i ] >> 56 ) & 0xFF );
            }
            
            return new String( bytes );
        }
        
        public static long[] stringToLong( String str )
        {
            int needsPadding = 8 - ( str.length() % 8 );
            
            while ( needsPadding > 0 )
            {
                //System.out.println( "adding padding:" + needsPadding );
                needsPadding--;
                str += ' ';
            }
            
            byte[] bytes = str.getBytes();
            int lengthInLong = bytes.length / 8; 
            long[] ret = new long[ lengthInLong ];
           
            int b = 0;
            
            System.out.println( "length:"+lengthInLong+" str:'"+str+"' bytes length:"+ bytes.length+" strlength:"+str.length() );
            for ( int i = 0; i < lengthInLong; i++, b+= 8 )
            {
                System.out.println( "i:"+i);
                ret[ i ] = bytes[ b ] + ( bytes[ b + 1 ] << 8 ) + ( bytes[ b + 2 ] << 16 ) + ( bytes[ b + 3 ] << 24 ) + ( bytes[ b + 4 ] << 32 ) 
                            + ( bytes[ b + 5 ] << 40 ) + ( bytes[ b + 6 ] << 48 ) + ( bytes[ b + 7 ] << 56 );
            }
            
            return ret;
        }  
Also, ive used a different way of doing things, however this doesnt work correctly when some of the longs become negative (due to encryption and the like).
        /**
         * Converts an array of longs into a string
         *
         * @param source The long's to convert into a string
         *
         * @return The longs in string form
         */
        public static String longToString( long[] source )
        {
            StringBuffer ret = new StringBuffer();
            for ( int i = 0; i < source.length; i++ )
            {
                ret.append( (char)( source[ i ] & 0xFF ) );
                ret.append( (char)( ( source[ i ] >>> 8 ) & 0xFF ) );
                ret.append( (char)( ( source[ i ] >>> 16 ) & 0xFF ) );
                ret.append( (char)( ( source[ i ] >>> 24 ) & 0xFF ) );
            }
            
            return ret.toString();
        }
        
        /**
         * Converts a string into an array of longs
         *
         * @param str The string to be converted
         *
         * @return An array of longs created from the string.
         */
        public static long[] stringToLong( String str )
        {
            int needsPadding = str.length() % 4;
            while ( needsPadding > 0 )
            {
                needsPadding--;
                str += ' ';
            }
            int lengthInLong = str.length() / 4;
            long[] ret = new long[ lengthInLong ];
            
            for ( int i = 0; i < lengthInLong; i++ )
            {
                ret[ i ] = str.charAt( i << 2 ) + ( str.charAt( (i << 2) + 1 ) << 8 ) + ( str.charAt( (i << 2) + 2 ) << 16 ) + ( str.charAt( (i << 2) + 3 ) << 24 );
            }
            
            return ret;
        }

Any help, or suggestions on how to do this would be greatly appreciated! Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
What exactly are you trying to do? Give an example of some input and output expected.

If you're trying to do what i'm thinking, I see no need for all that code.

Share this post


Link to post
Share on other sites
Couldn't you just loop through the characters in the string and cast its value to a long, then put it in the array of longs?

ace

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!