Results 1 to 19 of 19
Like Tree1Likes
  • 1 Post By jashburn

Thread: Converting strings to bits/bytes and vice versa

  1. #1
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    161
    Rep Power
    0

    Default Converting strings to bits/bytes and vice versa

    I have:
    Java Code:
    String s = "111100100111011011000010110011101";
    I am trying to convert that to bits/bytes.

    AKA, I need to make that into a series of bits with the same exact values..."1" = a 1 bit, and "0" = a 0 bit.

    Remarkably, I haven't been able to find much on this sort of conversion - possibly I am just not searching with the right keywords? As typically, stackexchange or other parts of the web have the same question that I have, asked (and answered) by many others.

    How do I go about doing this?

    Furthermore, how would I go about saving this to a file - and are there already good "kinds" of files to save this into. If not, how do I go about making my "own" type of "file."

    CONTEXT:
    I've written a compression system. To keep things simple, I've been using a string to hold the 1s and 0s, so that debugging is simpler, and overall, everything is easier to write. Now, however, my algorithm is finished - and I'm moving on to create a GUI and a working system. This is the last step that I need for the non-GUI stuff (which I'm writing through javafx by the way - is this the right thing to use? I've been told that that is where people are moving towards. Away from swing).
    Last edited by AlexGraal; 02-23-2014 at 01:36 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Converting strings to bits/bytes and vice versa

    Look at the Integer or Long classes for method to convert Strings to numbers using a radix/base of 2
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    161
    Rep Power
    0

    Default Re: Converting strings to bits/bytes and vice versa

    Thanks norm, but as I mentioned before, I had no luck finding any sort of stuff for this - can you please give me the link to that?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Converting strings to bits/bytes and vice versa

    Its in the API doc for the Integer and Long classes: Java Platform SE 7
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    161
    Rep Power
    0

    Default Re: Converting strings to bits/bytes and vice versa

    All right. That wasn't entirely helpful unfortunately - so I'll simply continue to ask questions until a resolving answer comes along.

    Here is the API from the from the Long class in the API for Java 7 for the byteValue function, which I think is the right function - though I can't really tell, as you didn't specify that part. It is one of two functions that deal with bytes in the Long class - where the other one has nothing to do with what I'm doing. So I'll assume it is this one.

    public byte byteValue()

    Returns the value of this Long as a byte.

    Overrides:
    byteValue in class Number
    Returns:
    the numeric value represented by this object after conversion to type byte.
    Here is what I tried:
    Java Code:
    import java.*;
    class test { 
      public static void main ( String[] args ) {
        long hold = 1L;
        System.out.println(hold.byteValue(hold));
        
      }
    }
    Will not compile.
    -------------
    *** Error ***
    -------------
    File: C:\Users\Alex\Documents\Programming 1\compressionSystem\test.java [line: 5]
    Error: long cannot be dereferenced
    What is the issue? I'd prefer a more indepth answer as to why this won't work - I understand what "long cannot be dereferenced" means. I don't understand how it is supposed to be otherwise, nor do I understand WHY long cannot be dereferenced.

    A small footnote - I'll continue doing this - showing what I'm doing and asking these questions step by step, if that is the only way you will help me. But it would be most beneficial to me if you simply gave me the name of the function I should look up, and I would go from there. Thanks.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Converting strings to bits/bytes and vice versa

    I didn't realize that there were so many methods in the Integer class that took a String and returned an int value. I'll have to go take a look to see where the confusion is.
    Which of the methods did you try?

    What type of numeric values are you trying to get from the String? byte, short, int, long or ???

    long cannot be dereferenced
    Only object references can be used to call methods. A long is a primitive. Primitives do NOT have methods that can be called.
    Last edited by Norm; 02-23-2014 at 02:56 AM.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Converting strings to bits/bytes and vice versa

    I suppose it depends on what you want to do after converting the String into bits/bytes.

    A few points of note:

  8. #8
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    161
    Rep Power
    0

    Default Re: Converting strings to bits/bytes and vice versa

    I'm looking to save a long string of bits into a file.

    It is simple compression, through a huffman encoding system.

    My series of bits represent various characters - however various characters have different lengths to represent them, possibly as short as a single bit, and up to (if there are a LOT of different characters) maybe 6 bits? It all depends on the number of different characters.

    That is my main issue. It is not that each 8bit group represents a new character.
    For me, a long chain of bits is the easiest to decode.

    Here is my decompression function - which is actually a time consuming way to do it. There is a faster way, but it'll work similarly.

    Java Code:
      public static String stringDecode(String comp) {
        String decoded = "";
        boolean stop = false;
        while(comp.length() > 0) {
          stop = false;
          for(int i = 1; stop == false; i++) {
            if(codedChunks.contains(comp.substring(0, i))) {
              decoded += chunksRef.get(codedChunks.indexOf(comp.substring(0, i)));
              comp = comp.substring(i);
              stop = true;
            }
          }
        }
        return decoded;
      }
    To demonstrate what happens:
    String s = "0101111010"
    String[] chunksRef = {"a","b","c"};
    String[] codedChunks = {"0","11","10"}

    Read in substring 0-1
    if it matches any element in codedChunks, add the element from chunksRef with the same index to the answer string and remove the read substring from encoded string, then start over
    otherwise, repeat with upper limit increased by 1


    Ideally, I'd want some sort of bit array that I could save to a file, then when I give that file to a decompression program, I'd simply create a long string, and work from there.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Converting strings to bits/bytes and vice versa

    looking to save a long string of bits into a file.
    I assume that means you want to convert a String of "0"s and "1"s to byte values with each 8 characters in the String going into a byte of 8 bits. For example a String of length 16 0s and 1s would be stored in the file in two bytes (16 bits).
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,377
    Rep Power
    5

    Default Re: Converting strings to bits/bytes and vice versa

    Have you checked out the BigInteger class? It may help but it sounds like the problem you are having is dealing with the situation when encoded sequences overlap the long or integer boundaries. So you may have to virtually increase the size of your chosen primitive by 6 bits.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  11. #11
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    161
    Rep Power
    0

    Default Re: Converting strings to bits/bytes and vice versa

    Jim,

    I'm actually more confused by that answer - could you elaborate on what you mean?

    I simply want a long string of bits. I have no issues with any sort of overlapping or anything.

    Norm,

    If that is the way to do it, then yes. I guess I could simply take my long string and turn it into bytes - but then I'd need to turn those bytes back into a long string in the decompression part.


    I'm not sure if it is me not explaining what I'm trying to do clearly enough, or if this simply isn't something very many people have experience with? If you want me to elaborate on the problem, please let me know - I'll gladly do so.

  12. #12
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,377
    Rep Power
    5

    Default Re: Converting strings to bits/bytes and vice versa

    Quote Originally Posted by AlexGraal View Post

    I'm actually more confused by that answer - could you elaborate on what you mean?

    I simply want a long string of bits. I have no issues with any sort of overlapping or anything.
    Imagine reading in a text file in 100 character chunks. It is possible a word may span the end of the first chunk and the start of the second. The same problem could arise when decoding files. So one needs to logically append both chunks or have a buffer to handle the situation. I was not trying to confuse you and I probably just made matters worse so forget the question.

    But I believe the BigInteger class may have what you need. You can convert back and forth between BigInteger and bytes, and manipulate arbitrarily large strings of bits, etc. The class is documented in the JDK.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Converting strings to bits/bytes and vice versa

    I simply want a long string of bits
    A long holds 64 bits. If you want more bits you would need to use an array of primitives and have logic to find the variable then the bit within that variable. For example if using byte to hold the bits, the 8th bit(0 based) would be in the second byte: bits 0-7 in first byte, 8-15 in second byte etc

    Why do you want to work with a String of "0"s and "1"s? Have you done any tests to see if working with bits is faster than working with Strings?
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Converting strings to bits/bytes and vice versa

    Using BigInteger...

    Java Code:
    BigInteger bigInt = new BigInteger(str, 2);
    byte[] bytes = bigInt.toByteArray();
    where str is the String of 0s and 1s. You can then write the byte array into a file.

    Getting back the String from the byte array (after reading back from the file) involves

    Java Code:
    BigInteger bigInt = new BigInteger(bytes);
    String recoveredStr = bigInt.toString(2);
    However I think this doesn't actually convert the series of 0s and 1s into the equivalent bytes, but instead the series is converted into the bytes representation of the BigInteger. (Jim, Norm, what do you think?) Having said that, it probably doesn't really matter.

    Another way to do this is by using BitSet...

    Java Code:
    str += "1"; // Append a 1 to ensure if the string ends with a 0, the 0 is preserved on conversion back.
    BitSet origBitSet = new BitSet(str.length());
    
    for (int i = 0; i < str.length(); i++) {
    	origBitSet.set(i, str.charAt(i) == '1');
    }
    byte[] bytes = origBitSet.toByteArray();
    ... and getting back the String ...

    Java Code:
    BitSet recovered = BitSet.valueOf(bytes);
    
    System.out.println("BitSet length: " + recovered.length());
    System.out.println("BitSet size: " + recovered.size()); // This is not what we want...
    
    StringBuilder recoveredStrBuilder = new StringBuilder(recovered.length());
    for (int i = 0; i < recovered.length() - 1; i++) { // length() - 1 to exclude the appended 1
    	recoveredStrBuilder.append((recovered.get(i) ? '1' : '0'));
    }
    
    String recoveredStr = recoveredStrBuilder.toString();
    AlexGraal likes this.

  15. #15
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    161
    Rep Power
    0

    Default Re: Converting strings to bits/bytes and vice versa

    Jim,
    Thanks for the feedback and the ideas. I see more or less what you mean now - though I wasn't thinking that that would be an issue. I was planning on turning the byte array back into a string for decompression anyway - as different characters have different length "codes" of bits, so it would be impossible to create a single type of data that would hold one character.

    Norm, I think I may have mentioned this in my original post -
    To keep things simple, I've been using a string to hold the 1s and 0s, so that debugging is simpler, and overall, everything is easier to write.
    But to reiterate, I've been working on the algorithm, and I am (obviously) much more familiar with strings than byte arrays and such, so I chose to go along the path of strings.

    Now, I can change it to work in any other way once I am familiar with it, but from my preliminary tests, a 10,000 character input string takes under a second to fully compress AND decompress with this system of strings (with only one step missing - conversion from string to bits and vice versa), so I'm not seeing any major problems yet. The speed will be further increased once I fix my decompression class to be liner and not exponential time.


    Jashburn,

    Thank you very much. That is a very detailed answer that I'm going to take a look at. Just from a glance, it seems to be exactly what I need. I'll go ahead and read up on some of the things you used.
    Thanks
    Last edited by AlexGraal; 02-23-2014 at 05:44 PM.

  16. #16
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,377
    Rep Power
    5

    Default Re: Converting strings to bits/bytes and vice versa

    However I think this doesn't actually convert the series of 0s and 1s into the equivalent bytes, but instead the series is converted into the bytes representation of the BigInteger.
    The internal representation of BigInteger is backed by an int array. The BigInteger value is as though you simply concatenated those ints back to back.

    Here is a comparison of bytes for both long and BigInteger for the same bit string. The byte arrays are the same.
    Java Code:
    import java.math.BigInteger;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    public class BigIntegervsLong {
       public static void main(String[] args) {
          String bitString = "110110101011100011010010101011111111";
          // show bytes of long
          long v = Long.parseLong(bitString, 2);
          List<Byte> b = new ArrayList<>();
          while (v != 0){
             b.add(0,(byte)(v&0xFF));
             v >>>=8;
          }
          System.out.println(b.toString());
          
          // show bytes of BigInteger
          BigInteger bv = new BigInteger(bitString,2);
          System.out.println(Arrays.toString(bv.toByteArray()));
       }
    }
    The fact that some of these are negative just means the sign bit of the byte value is set (which one would expect for some random selection of 8 bit fields in a large number).

    Regards,
    Jim
    Last edited by jim829; 02-23-2014 at 08:38 PM. Reason: added imports
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  17. #17
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Converting strings to bits/bytes and vice versa

    Cool, thanks, Jim. Look like BigInteger is the way to go!

  18. #18
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    161
    Rep Power
    0

    Default Re: Converting strings to bits/bytes and vice versa

    Thanks Jim - that helped a lot.

    Any chance you can annotate it for me as well so I can figure out what's happening? What's going on with the "v >>>=8;" part?

  19. #19
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,377
    Rep Power
    5

    Default Re: Converting strings to bits/bytes and vice versa

    >> is a signed right shift. The sign bit will be preserved thru the first and subsequent shifts.
    If it is 0, then 0 bits will be added in from the left. Similar behavior is
    true for 1 bits.

    >>> is an unsigned right shift. The sign bit will be shifted naturally the first time. Then subsequent
    right shifts will be filed in with 0 bits on the left.

    There is no <<< for obvious reasons.

    My code above just shifts v to the right 8 bits.
    it could also have been written:
    Java Code:
    v = v >>> 8;
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 4
    Last Post: 01-15-2013, 10:23 AM
  2. Replies: 5
    Last Post: 04-16-2009, 06:30 PM
  3. converting image to binary code n vice versa
    By nupurashi in forum Advanced Java
    Replies: 4
    Last Post: 04-01-2009, 07:27 PM
  4. Postfix into prefix and vice versa
    By sfe23 in forum New To Java
    Replies: 9
    Last Post: 02-19-2009, 10:37 PM
  5. Converting to ASCII and vice-versa
    By pheonix in forum New To Java
    Replies: 2
    Last Post: 09-09-2008, 04:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •