Jump to content

I'm creating a hash tree that reveives Strings with 16 char characters. The problem is that each char having 8 bits the string should have 128 bits, but when I read words from a txt file, add blank characters to add up 16 and convert it to a string of 0 and 1 I get only 127 digits. What's going on?

 


  

  public static void main(String[] args) {

        ArvorePatricia dicionario = new ArvorePatricia(127);

        try {
            ExtraiPalavra palavras = new ExtraiPalavra(
                    "C:\\Users\\olive\\Documents\\NetBeansProjects\\TP01\\src\\tp01\\exemplo1.txt");
            String palavra = null;
            long l;
            while ((palavra = palavras.proximaPalavra()) != null) {
                if (palavra.length() < 16) {
                    for (int i = palavra.length(); i < 16; i++) {
                        palavra += ' ';
                    }
                }
                String binary = new BigInteger(palavra.getBytes()).toString(2);
                System.out.println("inseriu: " + binary);
                dicionario.insere(binary);
            }

Link to comment
https://linustechtips.com/topic/923199-hash-tree-using-strings/
Share on other sites

Link to post
Share on other sites

Should be i <= 16 The for loop is getting to 15 adding one then ending the loop.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
https://linustechtips.com/topic/923199-hash-tree-using-strings/#findComment-11306460
Share on other sites

Link to post
Share on other sites

23 minutes ago, vorticalbox said:

Should be i <= 16 The for loop is getting to 15 adding one then ending the loop.

Arrays are 0 based so that's not a problem. Plus OP should be getting 90 bits instead of 127, assuming the entire input needed padding

Link to comment
https://linustechtips.com/topic/923199-hash-tree-using-strings/#findComment-11306554
Share on other sites

Link to post
Share on other sites

32 minutes ago, M.Yurizaki said:

Arrays are 0 based so that's not a problem. Plus OP should be getting 90 bits instead of 127, assuming the entire input needed padding

Yeah, the for isn't the issue. I'm going to try the suggestion from the link you sent.

Link to comment
https://linustechtips.com/topic/923199-hash-tree-using-strings/#findComment-11306662
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×