I have binary string xl and xr
xl = "11110101011100001001011010011100"
xr = "01100101111100011000011000101011"
I want to add xl and xr and convert this binary string to 8 character (1 character = 8 bit). Can you help me to get this code?
String Chippertext = "";
char nextChar;
for(int i = 0; i <= chippertext.length()-8; i += 8) //this is a little tricky. we want [0, 7], [9, 16], etc
{
nextChar = (char)Integer.parseInt(chippertext.substring(i, i+8), 2);
Chippertext += nextChar;
}
Thasil.setText(Chippertext);
I have already try this code but the character doesn't same with the character on ascii table.
You can't use chars to represent the data of a byte array because there are values that are not equivalent to a "char".
The best solution is to print each byte value as decimal, hexadecimal or binary value.
Related
My problem is that I've to write a code where a MAC direction must be written via keyboard and then, the code must recognize it to mount a "Magic Packet".
Let me show some code so you can understand better my problem and maybe reach some solution.
static String realMAC[] = new String [6];
static String mac; //that's the written string
private DatagramPacket buildWOLPacket(InetAddress address,String MAC) {
String splittedMAC[] = new String[12];
final int SIZE = 6 + 6 * 16; // See above for this magic number
byte data[] = new byte[SIZE];
for (int i = 0; i < 6; i++) {
data[i] = (byte) 0xff;
}
//Method where each character written
//is stored into a char array (realMAC[])
for (int i = 0 ; i<12; i++) {
String replacedMAC = MAC.replace(":", ""); //Here I delete all the : from the MAC adress introduced via keyboard.
if (i == 12)
splittedMAC[i] = replacedMAC.substring(i,(i));
else splittedMAC[i] = replacedMAC.substring(i,(i+1));}
And now the piece of code that is giving me problems
//All the (byte) 0x00 and so on are examples of a MAC direction and how to convert it if it is predefined
data[6 * i + 0] = Byte.parseByte(realMAC[0]); //(byte) 0x00;
data[6 * i + 1] = Byte.parseByte(realMAC[1]); //(byte) 0x1A;
data[6 * i + 2] = Byte.parseByte(realMAC[2]); //(byte) 0x09;
data[6 * i + 3] = Byte.parseByte(realMAC[3]); //(byte) 0x07;
data[6 * i + 4] = Byte.parseByte(realMAC[4]); //(byte) 0x8c;
data[6 * i + 5] = Byte.parseByte(realMAC[5]); //(byte) 0xe9;
My problem comes when converting the realMAC[] into bytes because of the x and letters from (0x8c, for example) because parseByte only accepts ints and ascii code. How could I tranform that string in hex form into bits?
Thank you so much.
Byte also has a .parseByte() method taking a radix as an argument. However that won't account for the initial 0x so you have to use substring(). Do:
Byte.parseByte(realMac[0].substring(2), 16)
//etc etc
More generally, all "numeric integral" classes (Integer, Short, Long) have such a method.
Byte.decode accepts a String that may begin with 0x or 0X. (It may also parse a decimal or octal number depending on what the string looks like.)
I have the following code in Java:
int hex = 0x63;
The decimal value of 6316 is equal to 9910. I would like to convert this hex value to a decimal that is equal to 6310.
the hex value is 0x63, I want to have a decimal value as 63 based on the hex value
This is called a Binary-coded decimal (BCD) representation. There are many ways to convert a number from BCD to decimal. Perhaps the simplest one is to print it as hex, and then parse it back as a decimal:
int hex = 0x63;
int dec = Integer.valueOf(Integer.toHexString(hex), 10);
Demo on ideone.
you can use this:
int hex = 0x63;
int decimalValue = Integer.parseInt(hex+"", 10);
System.out.println("Decimal value is :" + decimalValue);
try this:
int hex = 0x63F2FC;//just an example (containg letters)
String hexStr = Integer.toHexString(hex);
String decimal = "";
char[] tmp = hexStr.toCharArray();
for (int i = 0; i < tmp.length ; i++)
decimal += (Character.isDigit(tmp[i])?tmp[i]+"":"");
int[] LETTERS = {0x69F99}
I want to convert every single hex digit to binary, for example the 1st hex digit from the 1st hex string (6):
String hex = Integer.toHexString(LETTERS[0]);
String binary = Integer.toBinaryString(hex.charAt(0));
System.out.println(binary);
OUTPUT:110110
If I do this Integer.toBinaryString(6) the output will be 110, but I want something with 4 digits, is it possible?
I'd just pad the string as appropriate with your favorite library or a utility function.
With Guava:
String binary = Strings.padStart(Integer.toBinaryString(hex.charAt(0)), 4, '0'));
Another option would be to simply fill a character buffer and render it as a string, which is essentially what the OpenJDK implementation does:
public static String intToBin(int num) {
char[] buf = new char[4];
buf[0] = num & 0x1;
buf[1] = num & 0x2;
buf[2] = num & 0x4;
buf[3] = num & 0x8;
return new String(buf);
}
You have no string here - just an array with one int so you essentially try to convert this integer into nibbles and this can be done this way:
int num = 0x12345678;
String bin32 = String.format("%32s", Integer.toBinaryString(num)).replace(" ", "0");
System.out.printf("all 32bits=[%s]\n", bin32);
for(int nibble = 0; nibble < 32; nibble += 4)
{
System.out.printf("nibble[%d]=[%s]\n", nibble, bin32.subSequence(nibble, nibble+4));
}
gives:
all 32bits=[00010010001101000101011001111000]
nibble[0]=[0001] ie hex digit 1 as bin
nibble[4]=[0010] ie hex digit 2 as bin
nibble[8]=[0011]
nibble[12]=[0100]
nibble[16]=[0101]
nibble[20]=[0110]
nibble[24]=[0111]
nibble[28]=[1000] ie hex digit 8 as bin
First of all i receive a hex color code from a parameter 'col'. I then convert this value to the binary equivalent and then need to flip all the bits and convert it back to the hex value. Then the hex value needs to be padded out to 6 characters.
public String invertColor(String col)
{
String inverted = col;
int i = Integer.parseInt(inverted, 16);
String bin = Integer.toBinaryString(i);
System.out.println(bin);
int binary = Integer.parseInt(bin,2);
System.out.println(binary);
return inverted;
}
This is the code i have so far, i have been racking my brains all morning and just cannot seem to get a working solution. Any help at all would be appreciated.
Thanks
Use the bitwise not operator, ~.
int flipped = ~i;
Are we counting all the 0's preceding the binary representation in 32 bits, or do we only take the binary representation with no preceding 0's? Because that makes a difference when flipping. If it's the former, you can just use the operator ~.
int flip = ~i;
But if it's the second one, there's a little more work to do.
public String invertColor(String col)
{
String inverted = col;
int i = Integer.parseInt(inverted, 16);
String bin = Integer.toBinaryString(i);
String flipped = "";
for (int j = 0; j < bin.length(); j++) {
if (bin.charAt(j) == '0') flipped += "1";
else flipped += "0";
}
int k = Integer.parseInt(flipped, 2);
inverted = Integer.toHexString(k);
return inverted;
}
This should work. Basically this code builds a string by concatenating 1 if the current character is 0, and 0 otherwise. Then k is the integer represented by the flipped string, and inverted is the hex value.
I need to change a integer value into 2-digit hex value in Java.Is there any way for this.
Thanks
My biggest number will be 63 and smallest will be 0.
I want a leading zero for small values.
String.format("%02X", value);
If you use X instead of x as suggested by aristar, then you don't need to use .toUpperCase().
Integer.toHexString(42);
Javadoc: http://docs.oracle.com/javase/6/docs/api/java/lang/Integer.html#toHexString(int)
Note that this may give you more than 2 digits, however! (An Integer is 4 bytes, so you could potentially get back 8 characters.)
Here's a bit of a hack to get your padding, as long as you are absolutely sure that you're only dealing with single-byte values (255 or less):
Integer.toHexString(0x100 | 42).substring(1)
Many more (and better) solutions at Left padding integers (non-decimal format) with zeros in Java.
String.format("%02X", (0xFF & value));
Use Integer.toHexString(). Dont forget to pad with a leading zero if you only end up with one digit. If your integer is greater than 255 you'll get more than 2 digits.
StringBuilder sb = new StringBuilder();
sb.append(Integer.toHexString(myInt));
if (sb.length() < 2) {
sb.insert(0, '0'); // pad with leading zero if needed
}
String hex = sb.toString();
If you just need to print them try this:
for(int a = 0; a < 255; a++){
if( a % 16 == 0){
System.out.println();
}
System.out.printf("%02x ", a);
}
i use this to get a string representing the equivalent hex value of an integer separated by space for every byte
EX : hex val of 260 in 4 bytes = 00 00 01 04
public static String getHexValString(Integer val, int bytePercision){
StringBuilder sb = new StringBuilder();
sb.append(Integer.toHexString(val));
while(sb.length() < bytePercision*2){
sb.insert(0,'0');// pad with leading zero
}
int l = sb.length(); // total string length before spaces
int r = l/2; //num of rquired iterations
for (int i=1; i < r; i++){
int x = l-(2*i); //space postion
sb.insert(x, ' ');
}
return sb.toString().toUpperCase();
}
public static void main(String []args){
System.out.println("hex val of 260 in 4 bytes = " + getHexValString(260,4));
}
According to GabrielOshiro, If you want format integer to length 8, try this
String.format("0x%08X", 20) //print 0x00000014