Posted By

adino on 08/19/11


Tagged

java puzzler


Versions (?)

Java puzzler - Letter went missing


 / Published in: Java
 

  1. import static org.junit.Assert.*;
  2. import org.junit.Test;
  3.  
  4. public class StringToolsTest {
  5.  
  6. static final char [] data = {0xca, 0xfe, 0xeb, 0xab, 0xee};
  7. static final String string5 = String.valueOf(data);
  8. static final String string0 = "";
  9. static final String string1 = "a";
  10. static final String string9 = "987654321";
  11.  
  12. @Test
  13. public void testFillBackwards() {
  14. long l = 0;
  15. assertEquals(0xcafeebabeeL, StringTools.fillBackwards(string5, l));
  16. assertEquals(0L, StringTools.fillBackwards(string0, l));
  17. assertEquals(0x61L, StringTools.fillBackwards(string1, l));
  18. assertEquals(0x3837363534333231L, StringTools.fillBackwards(string9, l));
  19. }
  20. }
  21.  
  22. public class StringTools {
  23. public static long fillBackwards(String s, long d)
  24. {
  25. long result = 0L;
  26. byte[] bytes = s.getBytes();
  27. for (int i=0; i<Math.min(8,bytes.length); i++){
  28. result |= ((bytes[bytes.length-i-1] & 0xff) << (8*i));
  29. }
  30. return result;
  31. }
  32. }

Report this snippet  

You need to login to post a comment.