Next Generation Emulation banner
1 - 5 of 5 Posts

· Registered
Joined
·
61 Posts
Discussion Starter · #1 ·
I going to feel like an idiot if someone posts a solution to this, but I've been searching for the last two hours with no luck. I have an input file that contains a series of bits, 8 bits per line. I need to read in each line and then store that value as a byte. So if I read in the string "01010010" in need to parse it and produce the number 82 and store is in a byte. I'll keep looking but any ideas would be appreciated.
 

· Premium Member
Joined
·
5,284 Posts
OK. Since I never actually coded in Java I'll just tell you the method you can use to achieve the desired result.

You need to declare a byte variable and make a loop that does 8 cycles.
In each cycle one char of the string is read and if it's "1" it adds (2^(7-current cycle))* to the byte var.

*Cycle is counted from 0-7, 8 cycles total.
 

· Registered
Joined
·
61 Posts
Discussion Starter · #4 ·
OK. Since I never actually coded in Java I'll just tell you the method you can use to achieve the desired result.

You need to declare a byte variable and make a loop that does 8 cycles.
In each cycle one char of the string is read and if it's "1" it adds (2^(7-current cycle))* to the byte var.

*Cycle is counted from 0-7, 8 cycles total.
Not as elegant as I was hoping but it definitely works. Thanks!
 

· Registered
Joined
·
643 Posts
Not as elegant as I was hoping but it definitely works. Thanks!
Code:
    String number = "01010010"; //the bin way
        byte numberByte = (byte) Integer.parseInt(number, 2); //so mode 2
        System.out.println(numberByte);
Prints : 82

Integer.parseInt(number, numberMode);
So you can pass the string "DEADC0DE" (hexa) and gets the int from it.
Integer.parseInt("DEADC0DE", 16); will return the number in decimal way.
 
1 - 5 of 5 Posts
This is an older thread, you may not receive a response, and could be reviving an old thread. Please consider creating a new thread.
Top