Jump to content

How can i get 64-bit from string?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
13 replies to this topic

#1
gandudu

gandudu

    Newbie

  • Members
  • PipPip
  • 14 posts
i have to get 64-bit from a string.
Eg:
String ss = "Hello fifa world i am coming";
I wish to split the ss into 64-bit each;

how can i do that?

#2
CyrillC

CyrillC

    Newbie

  • Members
  • Pip
  • 8 posts
Hmm. ASCII is a 8-Bit system, so if you cut the String into 8 pairs of 8 characters, you'll have 64bit parts.
Just an Idea.. Not really sure..
What language do you use?

#3
gandudu

gandudu

    Newbie

  • Members
  • PipPip
  • 14 posts
Java, Netbeans
Can provide with coding example?

#4
abzero

abzero

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 217 posts
Use getBytes function, and then combine each group of 8 using left shifts and the bitwise or operator.

#5
gandudu

gandudu

    Newbie

  • Members
  • PipPip
  • 14 posts
abzero, actually i'm new to java.
can you provide the sample coding?
Thanks

#6
abzero

abzero

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 217 posts

    Bytes[] stringBytes = str.getBytes();

    

    for(int c=0;c<stringBytes.length;c+=8)

    {

        long res = (stringBytes[c]<<(64-8) |

                         stringBytes[c+1] <<(64-16) |

                         stringBytes[c+2] <<(64-24) |

                         stringBytes[c+3] <<(64-32) |

                         stringBytes[c+4] <<(64-40) |

                         stringBytes[c+5] <<(64-48) |

                         stringBytes[c+6] <<(64-56) |

                         stringBytes[c+7]);

            //Res contains the packed 64-bit representation of the string.        

    }



This code will fail if the data doesn't contain a multiple of 8 data items. I havn't tested the code, their might be a better way that one of the good java guys might know.

Edited by abzero, 30 June 2010 - 02:17 AM.
Err off by 4 error


#7
gandudu

gandudu

    Newbie

  • Members
  • PipPip
  • 14 posts
abzero, actually i'm new to java.
can you provide the sample coding?
Thanks

#8
gandudu

gandudu

    Newbie

  • Members
  • PipPip
  • 14 posts
i tested with string "12345678"
The output is as below:

Quote

892745532
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
at testcoding.Main.main(Main.java:23)

an exception is thrown.

#9
gandudu

gandudu

    Newbie

  • Members
  • PipPip
  • 14 posts
abzero, actually i'm new to java.
can you provide the sample coding?
Thanks

#10
abzero

abzero

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 217 posts

gandudu said:

abzero, actually i'm new to java.
can you provide the sample coding?
Thanks

You can stop posting this message you've posted it three times now. There was an error in my code, I've edited it so it should not throw with that exception.

#11
gandudu

gandudu

    Newbie

  • Members
  • PipPip
  • 14 posts
abzero, actually i'm new to java.
can you provide the sample coding?
Thanks

#12
gandudu

gandudu

    Newbie

  • Members
  • PipPip
  • 14 posts
abzero, actually i'm new to java.
can you provide the sample coding?
Thanks