How to Convert hex String into integer inorder to perform bitwise operation ?

edited January 2017 in Questions about Code
String val =Integer.toHexString(-10263709);
int value = Integer.parseInt(val,2);
System.out.println(value);

Answers

  • I want to find the most significant bit

  • One way to do it is (other elegant ways exists)

        String val="0xa";//10
    
       //next I am removing the "0x" in "0xa" for unhex() to work properly
        String res[] = split(val, 'x');
        if (res.length>=2 ) {
          val=res[1];
        } else
          val="0";
    
        int n=unhex(val);
        println(val+" is "+n);
    

    To find the most significant bit.... you need to elaborate more. The least significant is the one to the right in binary representation. The most significant will depend in your data structure. For example, a 32 bit integer's most significant bit would be bit 32. If you store a byte into an integer, would its most significant bit be bit number 8? So it depends on the data you are working on. Why do you need this info and how are you using it?

    Kf

  • Actually what i am trying to do is bit phase slicing . by getting the most ,least and middle significant bit of the pixels of the image . And it will be very useful in case of steganography in order to hide any other image

  • Goto yes i know those function but the main problem is they take int as an argument since it is showing the number format exception on changing to integer so can't put String into it ;(

  • greyscale again, or packed ARGB value?

    given a 32 bit int

    int msb = (input >> 31) & 0x1;
    int midsb = (input >> 15) & 0x1; // or 16 depending on what you call 'middle'
    int lsb = input & 0x1;
    
  • using the ARGB value like this is probably not what you want though.

  • hex(value); give the 0xff or something like this and they all are string so we cannot do the shift operation in strings.

  • edited January 2017

    but https://processing.org/reference/unhex_.html

    which was mentioned in the very first answer up there

    NB this doesn't like any 0x or 0X at the start of that string so remove it if it's there

Sign In or Register to comment.