how convert a 2 digit decimal to byte with same digits? - java

I am working with hex and byte numbers and now face with a problem at converting a thing as bellow:
int i= 26;
byte b = 0x00;
I want to have :
b = 0x26; ( 0x26 must be in format "byte")
or for example for
i= 90;
i must have
Byte b = 0x90;
Can you give me a method which does my work?
Thank u...

You could use the Byte.parseByte(String, int) method to simply parse your "26" into a hex value used to create a byte primitive:
int number = 26;
String intString = Integer.toString(number);
byte b = Byte.parseByte(intString, 16);
The radix value of 16 is used because you want the byte to have a value which is equal to 0x26 and the 0x prefix means hexadecimal which is base 16.
If you want a Byte object you can use valueOf instead:
Byte b = Byte.valueOf(intString, 16);

Related

Convert hex string value to hex int

How can I convert the hexstring into the form of hexint below?
string hexstring = "0x67";
int hexint = 0x67;
Integer#decode can be used to convert hexadecmial string representation into its integer value:
Integer.decode("0x67");
This function automatically detects the correct base and will parse return the int 103 (0x67 = 6*16+7). If you want to manually specify a different base, see my other answer
If you only have a single byte, you can strip of the leading "0x" part and then parse as a base-16 number with Integer#parseInt:
Integer.parseInt("0x67".substring(2), 0x10);
Integer.parseInt("0x67".substring(2), 16);
0x10 is the hexadecimal representation of the decimal number 16.
String hexstring = "67";
int hexint = Integer.parseInt(hexstring, 16);
System.out.println(hexint); // 103 (decimal)
With Integer.parseInt(String s, int radix) you can parse a String to an int.
The radix is the base of that number system, in case of hex-values it is 16.

JAVA Byte Manipulation

I want to read a binary file and do some manipulation on each byte. I want to test that I am manipulating the bytes correctly. I want to set a byte variable1 to "00000000" and then another byte variable2 set at "00001111" and OR them newvariable = variable1|variable2, shift the newvariable << 4 bits and then print out the int value.
byte a = 00000000;
//Convert first oneByte to 4 bits and then xor with a;
byte b = 00001111;
byte c = (byte)(a|b);
c = c << 4;
System.out.println("byte= " + c + "\n");
I am not sure why I keep getting "incompatiable types:possible lossy conversion from byte to int"
You need to put a '0b' in front of those numbers to express binary constants. The number 00001111 is interpreted as a literal in octal, which is 585 in decimal. The max byte is 127 (since it's signed). Try 0b00001111 instead.
As literals, those will still be int, so depending on where you do the assignment, you may also need to explicitly cast down to byte.

Java Hex number increase by one

I have some auto generated ids represented as a HEX String. I want to find the next 1000 values. For instance, let's suppose I have the following string
String keyFrom = "536a11dae4b062cab536549d";
How can I get from a java code the following, into String?
536a11dae4b062cab536549e
536a11dae4b062cab536549f
536a11dae4b062cab53654a0
536a11dae4b062cab53654a1
536a11dae4b062cab53654a2 ... etc.
Use BigInteger as below
BigInteger decimal = new BigInteger("536a11dae4b062cab536549d",16);
for ( int i=0;i<1000;i++){
decimal = decimal.add(BigInteger.ONE);
System.out.println(decimal.toString(16));
}
Convert your String to BigInteger and increment it:
BigInteger bigInt = new BigInteger(hexString, 16);
for(int i = 0 ; i < 1000 ; ++i) {
// do something with bigInt...
System.out.println(bigInt.toString(16));
bigInt = bigInt.add(BigInteger.ONE);
}
EDIT: If your using hex strings longer than ~8 characters, use the solution using BigInteger above.
Use Integer#parseInt(String, 16) to parse the hex string into an integer, add one to it, and then use Integer#toHexString to turn it back to hexadecimal.
String hexString = "A953CF";
// 16 sepcifies the string to be in base 16, hexadecimal
int hexAsInt = Integer.parseInt(hexString, 16);
hexAsInt += 6; // Add 6
String newHexString = Integer.toHexString(hexAsInt);
System.out.println(newHexString);
--> A953D4
http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt(java.lang.String)
http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#toHexString(int)

Converting String type binary number to bit in java

I have a question about converting String type binary number to bit and write in the txt file.
For example we have String like "0101011" and want to convert to bit type "0101011"
then write in to the file on the disk.
I would like to know is there anyway to covert to string to bit..
i was searching on the web they suggest to use bitarray but i am not sure
thanks
Try this:
int value = Integer.parseInt("0101011", 2); // parse base 2
Then the bit pattern in value will correspond to the binary interpretation of the string "0101011". You can then write value out to a file as a byte (assuming the string is no more than 8 binary digits).
EDIT You could also use Byte.parseByte("0101011", 2);. However, byte values in Java are always signed. If you tried to parse an 8-bit value with the 8th bit set (like "10010110", which is 150 decimal), you would get a NumberFormatException because values above +127 do not fit in a byte. If you don't need to handle bit patterns greater than "01111111", then Byte.parseByte works just as well as Integer.parseInt.
Recall, though, that to write a byte to a file, you use OutputStream.write(int), which takes an int (not byte) value—even though it only writes one byte. Might as well go with an int value to start with.
You can try the below code to avoid overflows of the numbers.
long avoidOverflows = Long.parseLong("11000000000000000000000000000000", 2);
int thisShouldBeANegativeNumber = (int) avoidOverflows;
System.out.println("Currect value : " + avoidOverflows + " -> " + "Int value : " + thisShouldBeANegativeNumber);
you can see the output
Currect value : 3221225472 -> Int value : -1073741824
//Converting String to Bytes
bytes[] cipherText= new String("0101011").getBytes()
//Converting bytes to Bits and Convert to String
StringBuilder sb = new StringBuilder(cipherText.length * Byte.SIZE);
for( int i = 0; i < Byte.SIZE * cipherText .length; i++ )
sb.append((cipherText [i / Byte.SIZE] << i % Byte.SIZE & 0x80) == 0 ? '0' : '1');
//Byte code of input in Stirn form
System.out.println("Bytecode="+sb.toString()); // some binary data
//Convert Byte To characters
String bin = sb.toString();
StringBuilder b = new StringBuilder();
int len = bin.length();
int i = 0;
while (i + 8 <= len) {
char c = convert(bin.substring(i, i+8));
i+=8;
b.append(c);
}
//String format of Binary data
System.out.println(b.toString());

Increment a Hex value (JAVA)

can you increment a hex value in Java? i.e. "hex value" = "hex value"++
It depends how the hex value is stored. If you've got the hex value in a string, convert it to an Integer, increment and convert it back.
int value = Integer.parseInt(hex, 16);
value++;
String incHex = Integer.toHexString(value);
Short answer: yes. It's
myHexValue++;
Longer answer: It's likely your 'hex value' is stored as an integer. The business of converting it into a hexadecimal (as opposed to the usual decimal) string is done with
Integer.toHexString( myHexValue )
and from a hex string with
Integer.parseInt( someHexString, 16 );
M.
What do you mean with "hex value"? In what data type is your value stored?
Note that int/short/char/... don't care how your value is represented initially:
int i1 = 0x10;
int i2 = 16;
i1 and i2 will have the exact same content. Java (and most other languages as well) don't care about the notation of your constants/values.
Yes. All ints are binary anyway, so it doesn't matter how you declare them.
int hex = 0xff;
hex++; // hex is now 0x100, or 256
int hex2 = 255;
hex2++; // hex2 is now 256, or 0x100
The base of the number is purely a UI issue. Internally an integer is stored as binary. Only when you convert it to human representation do you choose a numeric base. So you're question really boils down to "how to increment an integer?".

Categories