HEX TO integer

Hi,
I am wondering if you know how to convert
HEX to integer value in java?
Thanks

(you get the idea - drop that "String" bit, sorry)

Similar Messages

  • Converting Hex to Integer Value

    I'm trying to convert hex code to the corresponding integer, which can then be used to display a character.
    For example I have the code.
    int i = 0xE7;
    System.out.println(i);
    System.out.write(i)This produces the output as expected with the first output outputting 231, and the second line outputting the corresponding character.
    My problem comes when trying to make it so the hex code is variable.
    For example, I receive an output of 7
    This output is then the last digit of the hex code (so i check if needs to be converted to a, b etc.) then try and combine it with the prefix 0xE to create the hex code.
    I'm not quite sure how to do this bit as I cant carry out
    num = 7;
    hex = "0xE"+num;
    int i = hex;as the last line doesn't calculate due to hex being a string.
    Many thanks for your help.

    leesto wrote:
    The char array is currently being created using:
    char [] hexchar = "0123456789ABCDEF".toCharArray();
    Look this is OK, you need to do nothing but just the following:
    // I am using the very first example you provided
    // i is the index for the char
    String hex = "0E" + hexchar;
    Integer.parseInt(hex, 16);
    System.out.println(i);

  • Easier Way to convert Hex to Integer

    This problem is mainly to do with a hex string to integer conversion.  The device, connected via serial connection outputs hex in the format 0A00, when looking at it in hex.  The first two digits are the data, the last two are attached to seperate data points.  what I'm trying to do is to raster the last 2 digits off so that 2A00 becomes 2A and then convert the 2A into integer format (42).  I have come up with a method to do this, but it is very messy and occasionally results in data loss.  I have attached my vi (8.2).  The large integer being subtracted is a constant that continues to come up for reasons I am not aware of.
    another issue which I am coding through is the fact that sometimes a byte slips by, making 2A00 become 00BB or something of that format.  Is there a way to scan the string, determine whether the first two digits or the last two digits are the valid ones and then seperate the valid string?  In this code, it would be necessary to allow for the value 0000 to read 0.
    If there is confusion, here is some examples of incoming data and desired output
    0100 - 1
    0A00 - 10
    0005 -5
    000B - 11
    etc.
    Solved!
    Go to Solution.
    Attachments:
    geiger read.vi ‏28 KB

    Since you are reading 2 bytes, it looks like you should be dealing with a 16 bit number. Typecast to U16 rather than I32 like you are doing now. And all of that string manipulation make it look like you are getting a string of ASCII formatted characters like "0" "A" "1" "B" rather than a 2 byte string of ASCII characters 0A and 1B. Then you can split the U16 number and take the higher order byte.
    I can't explain why you would be having a byte slip by unless it has something to do with all that string manipulation you are trying to do.  But if it still happens, and you know one byte for the other is always zero.  Just take the high order byte and add it to the low order byte.  Since one is zero, it will have no effect on the other byte.
    Message Edited by Ravens Fan on 07-07-2009 01:54 PM
    Attachments:
    geiger%20read[1]_BD.png ‏3 KB

  • Convert hexa to integer

    Hello,
    I have a flat file in which I get a value in hexadecimal format.
    Can we convert it in integer format in ODI ? I did'nt find anything about that.
    Thanks in advance
    Marie

    What is your target technology ?
    There are ways to do it. One of the way is to create function
    You can create ODI function to convert Hexadecimal to Integer.
    For example if you are using Oracle as your staging or target technology .. you can use
    Oracle you can use
    SYS_OP_RAWTONUM('AA') or to_number('AA', 'xx').
    Other can be directly using features of target or staging technologies to get the desired output.

  • Int to Hex Progam

    Could someone help resolve this problem : I have created a program to change int to hex but it does not seem to woek ? What could be wrong ?
    import java.lang.String;
    import javax.swing.JOptionPane; // import class JOptionPane
    public class KcHexConverter {
    public static void main( String args[] )
         String value, s;// first string entered by user
         int ni=0;
              // read in first number from user as a string
              value = JOptionPane.showInputDialog( "Enter integer" );
    // convert numbers from type String to type Hex
              ni = Integer.parseInt(value, 16);
              s = Integer.toHexString(ni);
    // display the results
    JOptionPane.showMessageDialog(null, "The equvalent in Hexadecimal is: " +s, "Results", JOptionPane.PLAIN_MESSAGE );
    System.exit( 0 ); // terminate the program
    }

    Hi
    you need to change:
    ni = Integer.parseInt(value);//no need for a radix it is base 10
    and you should catch a possible NumberFormatException (sb enters a letter).

  • Retrieve the character from hex code

    I want to write the hex code / ASCII code of the English alphabets ( A - Z and a - z ) in a file and again want to read those codes (either ASCII or Hex code) from the file to retrieve the characters from them. How it is possible?
    For example, ASCII code of 'A' is 65 and hex code is 41.
    ASCII code of 'z' is 122 and hex code is 7A.
    Avijit

    Forgot that you need to convert them back:
            Character c = new Character('A');
            System.out.println("char : "+c);
            int ascii = (int) c.charValue();
            System.out.println("ascii: "+ascii+" => "+(char) ascii);
            String hex = Integer.toHexString(ascii);
            System.out.println("hex  : "+hex +" => "+(char) Integer.parseInt(hex, 16));

  • Help with binary to decimal, binary to hex, and hex to ascii or ascii to hex program

    I decided to do a program that will do binary to decimal, binary to hex, and hex to ascii for a project related to a java programming course, which only needs to perform features from chapters 1-6 and 8 of Tony Gaddis's book. The functions work fine as their own main programs out side of this combined effort, so can anyone help me determine why I get the following 41 errrors saying: class, interface, or enum expected as well as any other errors that may show up afterwards because I'm stumped. My flowcharts, which have to be revised after discovering that my previous function were logically incorrect after running them in their own main are attached below as the spec sheet.
    My code is as follows and I hope you don't mind the commented lines of unused code because I'm not sure where I want things and what I want at the moment:
    import java.util.Scanner;
    import java.io.*;
    import java.lang.*;
    public class BintoDectoHextoAscii
       public static void main(String[] args)throws IOException
          Scanner input = new Scanner(System.in);
          System.out.println("Enter a binary number: ");
          String binary = input.nextLine(); // store input from user
         if (binary == input.nextLine())
          //int i= Integer.parseInt(hex,2);
          //String hexString = Integer.toHexString(i);
          //System.out.println("Hexa decimal: " + hexString);
          //int finaldecimalvalue = binaryToDecimal(hexString);
          int finaldecimalvalue = binaryToDecimal(hexString);
         if (binary != input.nextLine())
          String hexInput; // The variable Bin Input declared as the datatype int to store the Binary value  
          // Create a Scanner object for keyboard input.
          //Scanner keyboard = new Scanner(System.in);
          // Get the number of binary files.
          System.out.print("Enter the Hex value: ");
          hexInput = keyboard.nextLine();
          System.out.println("Original String: "+ hexInput);
          //String hexEquivalent = asciiToHex(demoString);
          String hexEquivalent = asciiToHex(hexInput);
          //Hex value of original String
          System.out.println("Hex String: "+ hexEquivalent);
          String asciiEquivalent = hexToASCII(hexEquivalent);
          //ASCII value obtained from Hex value
          System.out.println("Ascii String: "+ asciiEquivalent);String finalhexOutput = HextoAsciiConverter(hexEquivalent);
         if (binary != input.nextLine() && hexInput != keyboard.nextLine())
             BufferedReader binInput = new BufferedReader(new InputStreamReader(System.in));
             System.out.println("Enter the Binary number:");
             String hex = binInput.readLine();
             //String finaldecimalvalue = binaryToDecimal(decimal);
             //long finalhexvalue = BinaryToHexadecimal(num);
             long finalhexvalue = BinaryToHexadecimal();
       public static String BinaryToHexadecimal(String hex)
          //public static void main(String[] args)throws IOException
             //BufferedReader bf= new BufferedReader(new InputStreamReader(System.in));
             //System.out.println("Enter the Binary number:");
             //String hex = binInput.readLine();
             long num = Long.parseLong(hex);
             long rem;
             while(num > 0)
             rem = num % 10;
             num = num / 10;
             if(rem != 0 && rem != 1)
             System.out.println("This is not a binary number.");
             System.out.println("Please try once again.");
             System.exit(0);
             int i= Integer.parseInt(hex,2);
             String hexString = Integer.toHexString(i);
             System.out.println("Hexa decimal: " + hexString);
          return num.tolong();
      //int i= Integer.parseInt(hex,2);
      //String hexString = Integer.toHexString(i);
      //System.out.println("Hexa decimal: " + hexString);
    //} // end BintoDectoHextoAsciil
       //public static String HexAsciiConverter(String hextInput)
          // Get the number of binary files.
          //System.out.print("Enter the Hex value: ");
          //hexInput = keyboard.nextLine();
          //System.out.println("Original String: "+ hexInput);
          //String hexEquivalent = asciiToHex(demoString);
          //String hexEquivalent = asciiToHex(hexInput);
          //Hex value of original String
          //System.out.println("Hex String: "+ hexEquivalent);
          //String asciiEquivalent = hexToASCII(hexEquivalent);
          //ASCII value obtained from Hex value
          //System.out.println("Ascii String: "+ asciiEquivalent);
       //} // End function  
       private static String asciiToHex(String asciiValue)
          char[] chars = asciiValue.toCharArray();
          StringBuffer hex = new StringBuffer();
          for (int i = 0; i < chars.length; i++)
             hex.append(Integer.toHexString((int) chars[i]));
          return hex.toString();
       private static String hexToASCII(String hexValue)
          StringBuilder output = new StringBuilder("");
          for (int i = 0; i < hexValue.length(); i += 2)
             String str = hexValue.substring(i, i + 2);
             output.append((char) Integer.parseInt(str, 16));
          return output.toString();
       public static String binaryToDecimal(String binary)
            //Scanner input = new Scanner(System.in);
            //System.out.println("Enter a binary number: ");
            //String binary = input.nextLine(); // store input from user
            int[] powers = new int[16]; // contains powers of 2
            int powersIndex = 0; // keep track of the index
            int decimal = 0; // will contain decimals
            boolean isCorrect = true; // flag if incorrect input
           // populate the powers array with powers of 2
            for(int i = 0; i < powers.length; i++)
                powers[i] = (int) Math.pow(2, i);
            for(int i = binary.length() - 1; i >= 0; i--)
                // if 1 add to decimal to calculate
                if(binary.charAt(i) == '1')
                    decimal = decimal + powers[powersIndex]; // calc the decimal
                else if(binary.charAt(i) != '0' & binary.charAt(i) != '1')
                    isCorrect = false; // flag the wrong input
                    break; // break from loop due to wrong input
                } // end else if
                // keeps track of which power we are on
                powersIndex++; // counts from zero up to combat the loop counting down to zero
            } // end for
            if(isCorrect) // print decimal output
                System.out.println(binary + " converted to base 10 is: " + decimal);
            else // print incorrect input message
                System.out.println("Wrong input! It is binary... 0 and 1's like.....!");
            return decimal.toint();
       } // end function
    The errors are as follows:
    ----jGRASP exec: javac BintoDectoHextoAscii.java
    BintoDectoHextoAscii.java:65: error: class, interface, or enum expected
       public static String BinaryToHexadecimal(String hex)
                     ^
    BintoDectoHextoAscii.java:73: error: class, interface, or enum expected
             long rem;
             ^
    BintoDectoHextoAscii.java:74: error: class, interface, or enum expected
             while(num > 0)
             ^
    BintoDectoHextoAscii.java:77: error: class, interface, or enum expected
             num = num / 10;
             ^
    BintoDectoHextoAscii.java:78: error: class, interface, or enum expected
             if(rem != 0 && rem != 1)
             ^
    BintoDectoHextoAscii.java:81: error: class, interface, or enum expected
             System.out.println("Please try once again.");
             ^
    BintoDectoHextoAscii.java:83: error: class, interface, or enum expected
             System.exit(0);
             ^
    BintoDectoHextoAscii.java:84: error: class, interface, or enum expected
             ^
    BintoDectoHextoAscii.java:87: error: class, interface, or enum expected
             String hexString = Integer.toHexString(i);
             ^
    BintoDectoHextoAscii.java:88: error: class, interface, or enum expected
             System.out.println("Hexa decimal: " + hexString);
             ^
    BintoDectoHextoAscii.java:90: error: class, interface, or enum expected
          return num.tolong();
          ^
    BintoDectoHextoAscii.java:91: error: class, interface, or enum expected
       ^
    BintoDectoHextoAscii.java:124: error: class, interface, or enum expected
          StringBuffer hex = new StringBuffer();
          ^
    BintoDectoHextoAscii.java:125: error: class, interface, or enum expected
          for (int i = 0; i < chars.length; i++)
          ^
    BintoDectoHextoAscii.java:125: error: class, interface, or enum expected
          for (int i = 0; i < chars.length; i++)
                          ^
    BintoDectoHextoAscii.java:125: error: class, interface, or enum expected
          for (int i = 0; i < chars.length; i++)
                                            ^
    BintoDectoHextoAscii.java:128: error: class, interface, or enum expected
          ^
    BintoDectoHextoAscii.java:130: error: class, interface, or enum expected
       ^
    BintoDectoHextoAscii.java:135: error: class, interface, or enum expected
          for (int i = 0; i < hexValue.length(); i += 2)
          ^
    BintoDectoHextoAscii.java:135: error: class, interface, or enum expected
          for (int i = 0; i < hexValue.length(); i += 2)
                          ^
    BintoDectoHextoAscii.java:135: error: class, interface, or enum expected
          for (int i = 0; i < hexValue.length(); i += 2)
                                                 ^
    BintoDectoHextoAscii.java:138: error: class, interface, or enum expected
             output.append((char) Integer.parseInt(str, 16));
             ^
    BintoDectoHextoAscii.java:139: error: class, interface, or enum expected
          ^
    BintoDectoHextoAscii.java:141: error: class, interface, or enum expected
       ^
    BintoDectoHextoAscii.java:144: error: class, interface, or enum expected
       public static String binaryToDecimal(String binary)
                     ^
    BintoDectoHextoAscii.java:150: error: class, interface, or enum expected
            int powersIndex = 0; // keep track of the index
            ^
    BintoDectoHextoAscii.java:151: error: class, interface, or enum expected
            int decimal = 0; // will contain decimals
            ^
    BintoDectoHextoAscii.java:152: error: class, interface, or enum expected
            boolean isCorrect = true; // flag if incorrect input
            ^
    BintoDectoHextoAscii.java:155: error: class, interface, or enum expected
            for(int i = 0; i < powers.length; i++)
            ^
    BintoDectoHextoAscii.java:155: error: class, interface, or enum expected
            for(int i = 0; i < powers.length; i++)
                           ^
    BintoDectoHextoAscii.java:155: error: class, interface, or enum expected
            for(int i = 0; i < powers.length; i++)
                                              ^
    BintoDectoHextoAscii.java:159: error: class, interface, or enum expected
            for(int i = binary.length() - 1; i >= 0; i--)
            ^
    BintoDectoHextoAscii.java:159: error: class, interface, or enum expected
            for(int i = binary.length() - 1; i >= 0; i--)
                                             ^
    BintoDectoHextoAscii.java:159: error: class, interface, or enum expected
            for(int i = binary.length() - 1; i >= 0; i--)
                                                     ^
    BintoDectoHextoAscii.java:166: error: class, interface, or enum expected
                else if(binary.charAt(i) != '0' & binary.charAt(i) != '1')
                ^
    BintoDectoHextoAscii.java:169: error: class, interface, or enum expected
                    break; // break from loop due to wrong input
                    ^
    BintoDectoHextoAscii.java:170: error: class, interface, or enum expected
                } // end else if
                ^
    BintoDectoHextoAscii.java:174: error: class, interface, or enum expected
            } // end for
            ^
    BintoDectoHextoAscii.java:180: error: class, interface, or enum expected
            else // print incorrect input message
            ^
    BintoDectoHextoAscii.java:185: error: class, interface, or enum expected
            return decimal.toint();
            ^
    BintoDectoHextoAscii.java:186: error: class, interface, or enum expected
       } // end function
       ^
    41 errors
    ----jGRASP wedge2: exit code for process is 1.
    ----jGRASP: operation complete.

    so can anyone help me determine why I get the following 41 errrors saying: class, interface, or enum expected as well as any other errors that may show up afterwards because I'm stumped.
    Yes - YOU CAN!
    My code is as follows and I hope you don't mind the commented lines of unused code because I'm not sure where I want things and what I want at the moment:
    Excellent! Commenting out code is EXACTLY how you troubleshoot problems like yours.
    Comment out sections of code until the problem goes away. Then start adding back ONE SECTION of code at a time until the problem occurs. When it does you have just FOUND the problem.
    If you do that you wind up with code that looks like this:
    import java.util.Scanner;
    import java.io.*;
    import java.lang.*;
    public class BintoDectoHextoAscii  {
          public static void main(String[] args)throws IOException      {
             Scanner input = new Scanner(System.in);
             System.out.println("Enter a binary number: ");
             String binary = input.nextLine(); // store input from user
                  public static String BinaryToHexadecimal(String hex)     {     } // end function        
    Notice ANYTHING UNUSUAL?
    You have a complete CLASS definition followed by a method definition.
    Methods have to be INSIDE the class - you can NOT define methods on their own.
    Write modular code.
    Write EMPTY methods - just the method signature and maybe a RETURN NULL if you need to return something.
    Then add calls to those empty methods.
    When everything compiles and runs find you can start adding code to the methods ONE METHOD AT A TIME.
    Test compile and run after you add the code for each method.

  • Java Label object not displaying ascii chars (128-159)

    I am having a problem getting ASCII characters between 128 and 159 to show up in a Label object which is inside a Panel which is in turn inside a Frame. There is other code in a different class that uses a Graphics object and drawString and Paint methods to display the same text, and that works. However, simply passing the text containing the above mentioned ascii characters like so:
    lb = new Label("€‚ƒ„…†‡");
    results in mostly empty boxes being displayed (there is other code that eventually displays the label in the Frame). The Euro sign comes through, as does the integral sign. Some others come through, and they are mostly modifed letters of some sort.
    I've tried coding a mock up simple Frame with a few labels. I've passed in literal strings, String objects derived from hex and integer byte arrays and a String object with the Cp1252 encoding passed in. None display the characters in question. However, in the same code, I pass a title string containing these characters into the Frame's constructor, and it appears with no problems.
    I just want to be able to handle all characters that may be entered into this application, and that may include these Cp1252 characters. Is there any way of using Label objects and getting these characters to display correctly?
    [Windows-1252 Chart|http://upload.wikimedia.org/wikipedia/commons/e/e7/Windows-1252.svg]

    I am having a problem getting ASCII characters between 128 and 159 to show up in a Label object which is inside a Panel which is in turn inside a Frame. There is other code in a different class that uses a Graphics object and drawString and Paint methods to display the same text, and that works. However, simply passing the text containing the above mentioned ascii characters like so:
    lb = new Label("€‚ƒ„…†‡");
    results in mostly empty boxes being displayed (there is other code that eventually displays the label in the Frame). The Euro sign comes through, as does the integral sign. Some others come through, and they are mostly modifed letters of some sort.
    I've tried coding a mock up simple Frame with a few labels. I've passed in literal strings, String objects derived from hex and integer byte arrays and a String object with the Cp1252 encoding passed in. None display the characters in question. However, in the same code, I pass a title string containing these characters into the Frame's constructor, and it appears with no problems.
    I just want to be able to handle all characters that may be entered into this application, and that may include these Cp1252 characters. Is there any way of using Label objects and getting these characters to display correctly?
    [Windows-1252 Chart|http://upload.wikimedia.org/wikipedia/commons/e/e7/Windows-1252.svg]

  • Split TextArea

    Im wanting to create a TextArea that will show ASCII on the left, and the Hex values on the right.
    Im not sure where to start. Ive looked into the JTextArea source and noticed it doesnt have a paintComponent method. So obviosly text components render differently?
    I thought of modifying the JTable, but I wont be dealing with rows.
    I was then thinking of just implementing my own paintComponent for the text area, but I think I should learn how text components are drawn first.
    Or, to save time, has anyone seen an implementation of this already?
    Any ideas on where to start?

    import java.awt.*;
    import java.io.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.text.*;
    public class ShowText extends JFrame{
        public ShowText(){
            setTitle("Show Text");
            setDefaultCloseOperation( EXIT_ON_CLOSE );
            try{
                readText();
            }catch(IOException ex){
                ex.printStackTrace();
            textPane = new JTextPane();
            textPane.setFont( new Font("monospaced", 0, 14) );
            JScrollPane scrollPane = new JScrollPane( textPane );
            getContentPane().add( scrollPane );
            showText(40);
            setScreenState(false);   //true = full screen
        private void showText(int textWidth){
            StringBuffer sb = new StringBuffer(1000);
            for ( int j=0; j<text.length(); j=j+textWidth ){
                int end = text.length();
                int end_2 = j+textWidth;
                if( end_2 > end ){
                    text.append( blanks.substring( 0, end_2 - end ) );
                String str = text.substring(j, end_2);
                sb.append(str);
                sb.append("\t");
                sb.append(asciiToHex(str));
                sb.append("\n");
            setTabs( textPane, textWidth+5 );
            textPane.setText( sb.toString() );
        public void setTabs( final JTextPane textPane, int textWidth){
            FontMetrics fm = textPane.getFontMetrics( textPane.getFont() );
            int charWidth = fm.charWidth( 'W' );
            TabStop[] tabs = new TabStop[1];
            tabs[0] = new TabStop( textWidth * charWidth );
            TabSet tabSet = new TabSet(tabs);
            SimpleAttributeSet attributes = new SimpleAttributeSet();
            StyleConstants.setTabSet(attributes, tabSet);
            int length = textPane.getDocument().getLength();
            textPane.getStyledDocument().setParagraphAttributes(0, length, attributes, false);
        private void readText()throws IOException{
            BufferedReader reader = new BufferedReader(new FileReader("MyText.txt"));
            text = new StringBuffer(1000);
            for( String str = reader.readLine(); str != null; str = reader.readLine() ){
                text.append(str);
        private String asciiToHex(String ascii){
            StringBuilder hex = new StringBuilder();
            for (int i=0; i < ascii.length(); i++) {
                hex.append(Integer.toHexString(ascii.charAt(i)));
            return hex.toString();
        private void setScreenState(boolean full){
            setExtendedState(JFrame.MAXIMIZED_BOTH);
            if(full){
                setResizable(false);
                setVisible(true);
            }else{
                setVisible(true);
                setResizable(false);
        public static void main(final String[] args){
            new ShowText();
        private JTextPane textPane;
        private StringBuffer text;
        private final String blanks = "                                                  ";
    }

  • Convert data from AS/400

    When I make a query to AS/400 table, the character field appear in Hexa presentation, How can I translate that to human readable format?
    Thanks

    You can retrieve data from your database, and then perform some convertions between datacodifications.
    You can cast an int to a char to a String:
    int vInt = 50;
    char vChar = (char)vInt;
    String vString = ""+vChar;
    System.out.println("int=" + vInt + ", char=" + vChar + ", str=" + vString);
    // output: int=50, char=2, str=2
    All think that your stoed data are in the form of strings like "0x56" for hex 56, Integer.decode() are useful here.

  • Why unicode symbol (copyright) in textfield to drawString not interpreted?

    I have a textfield that I can type stuff into and have printed on a JPanel. However, when I type in some unicode, it's printed as the code not what it means. Why is this? how do I fix this?
    NOTE: to try this sample program, you have to type "\u00a9" (without the quotes) into the text box and then click the button.
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class TestSymbol extends JPanel
         private static JTextField field;
         public void paintComponent(Graphics g)
              //This prints a copyright symbol
              g.drawString( "\u00a9 Something", 50, 50 );
              //This prints the unicode
              g.drawString( field.getText(), 50, 100 );
         public static void main(String[] args)
              JFrame f = new JFrame();
              f.setSize( 500, 500 );
              JPanel p = new JPanel();
              p.setLayout( new FlowLayout() );
              //The panel that prints everything
              final TestSymbol t = new TestSymbol();
              setExactSize( t, 200, 200 );
              p.add( t );
              //The text field
              field = new JTextField( "", 10 );
              field.setSize( 100, 25 );
              p.add( field );
              JButton button = new JButton( "click" );
              button.addActionListener( new ActionListener()
                   public void actionPerformed(ActionEvent e)
                        t.repaint();
              p.add( button );
              f.getContentPane().add( p );
              f.setVisible( true );
         private static void setExactSize(JComponent component, int width, int height)
              Dimension dim = new Dimension( width, height );
              component.setPreferredSize( dim );
              component.setMaximumSize( dim );
              component.setMinimumSize( dim );
              component.setSize( width, height );
    }

    sabre150 wrote:
    Unicode representation as String literals are parsed by the compiler. Unicode representation entered by the user has to be parsed by your program and should substitute the unicode string for the appropriate unicode value. See reply #1 of http://forums.sun.com/thread.jspa?threadID=5226995&tstart=28049 .
    Edited by: sabre150 on May 14, 2009 5:26 PMThanks! I just figured that out too.
    This isn't the best, but it properly converts all unicode within a string from a text field (you can add it to my previous program):
         private static String UNICODE_START      = "\\u";
         private static int UNICODE_LENGTH      = 6;
         private static String convertUnicode(String base)
              //Unicode unicodePos
              int unicodePos = 0;
              int currSpot = 0;
              //String length
              int length = base.length();
              //Our return string
              StringBuffer buffer = new StringBuffer( length );
              //Loop and convert
              while ( ( unicodePos = base.indexOf( UNICODE_START, unicodePos ) ) != -1 )
                   //The unicode string
                   String unicode = base.substring( unicodePos, unicodePos + 6 );
                   //Is actually a unicode character
                   if ( isUnicode( unicode ) )
                        //Put everything up to this into the buffer
                        if ( currSpot < unicodePos ) buffer.append( base.substring( currSpot, unicodePos ) );
                        //Now append our new character
                        buffer.append( ( char ) Integer.parseInt( unicode.substring( 2 ), 16 ) );
                        //Move to end of this unicode
                        unicodePos += UNICODE_LENGTH - 1;
                   //Move forward
                   unicodePos++;
                   currSpot = unicodePos;
              //Put in everything left
              if ( currSpot < length )
                   buffer.append( base.substring( currSpot ) );
              return buffer.toString();
         private static boolean isUnicode(String unicode)
              //Check length
              if ( unicode.length() != UNICODE_LENGTH ) return false;
              //Make sure it starts with unicode start
              if ( unicode.indexOf( UNICODE_START ) != 0 ) return false;
              try
                   //Can only have hex symbols
                   Integer.parseInt( unicode.substring( 2 ), 16 );
              catch (NumberFormatException e)
                   return false;
              return true;
         }

  • Finding out what a character is

    Hi
    I have a string coming from a database with a uk pound sign in it. All my JSPs and Tomcat are operating in UTF-8 encoding and therefore the pound signs are coming out as squares in my JSP.
    When I copy and paste one of these squares from the webpage into Dreamweaver it turns into a question mark. Now, I know there have been similar posts to this but none of those will help me at the moment.
    I had this problem with question marks appearing for all kinds of symbold like dashes and apostophes and I corrected this by setting all my encoding to UTF-8.
    So I figure I can fix this by just doing the following;
    out.println(contentString.replace(THE_SQUARE, '\u00A3'));Where THE_SQUARE is whatever the actual char value for the square I am getting. So how can I work out what this square's value is so I can do this replace call?
    Thanks!

    It sounds like the wrong characterset is being used to read the database. One of the deficiencies of jdbc seems to be the inability to specificy a characterset for getString and setString methods.
    Your best bet is probably to write a little program to read one of your database strings and print it out in hex. (use Integer.toHexString()).

  • Parsing machine code

    I have a need for a program that can parse a 4-digit hexadecimal code and take action on it. The first and last digits are frequently the most important, but all of the digits can be varied. I have several methods in mind, but what is the fastest or most efficient way in java to operate on the digits individually?

    You could do something similar to this.
    public class HexToInt {
         public static void main(String args[]){
              String hex = "A7FE"; //Hexadecimal number
              int count = hex.length();
              String[] c = new String[count];
              for(int i = 0; i < count; i++){
                   c[i] = String.valueOf(hex.charAt(i)); //Grab single hex character
                   System.out.println("Hex digit at element " + i + " is " + convert(c)); //Convert character to integer
         public static int convert(String hex){
              return Integer.valueOf(hex, 16);
    }I'm not exactly sure if this was the kind of action you were looking for, but this is just a simple program that converts a single hexadecimal character from a string and prints it out as an integer.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

  • Java reading and writing binary

    Java does not seem to like reading in binary values from 128 to 160. I have a binary file containing values from 0 to 255 inclusive. The output (in hex) is as follows:
    00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f 20ac fffd 201a 192 201e 2026 2020 2021 2c6 2030 160 2039 152 fffd 17d fffd fffd 2018 2019 201c 201d 2022 2013 2014 2dc 2122 161 203a 153 fffd 17e 178 a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf c0 c1 c2 c3 c4 c5 c6 c7 c8 c9 ca cb cc cd ce cf d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 da db dc dd de df e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 ea eb ec ed ee ef f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb fc fd fe ff This is my code:
    while ((temp = in.read()) != -1) {
                //convert the bits to hex
                hexString = Integer.toHexString(temp);
                // add the 0 if hex representation is just one char
                if(hexString.length() == 1){
                    hexString = "0" + hexString;
                // print out the value
                System.out.println(hexString + " ");
            }The object 'in' is a BufferedReader.
    Why is this happening and how can I fix it?
    Thank you.

    Works for me:
    import java.io.*;
    public class BinaryIO {
        public static void main(String[] args) throws IOException {
            File file = new File("temp.dat");
            write(file);
            read(file);
        static void write(File file) throws IOException {
            FileOutputStream out = new FileOutputStream(file);
            try {
                for(int i=0; i<256; ++i) {
                    out.write(i);
            } finally {
                out.close();
        static void read(File file) throws IOException {
            FileInputStream in = new FileInputStream(file);
            try {
                for(int b; (b = in.read()) != -1; ) {
                    System.out.format("%02x%n", b);
            } finally {
                in.close();
    }

  • Hex String of format "01:02:03:04:05:06" to Integer?

    Yes, that is a Mac Address,
    does anyone know if there is some "comfortable" conversion routine in Cocoa for converting Hex numbers from String to int?
    The ":" could be stripped, of course, they are only delimiters for easier reading...
    Many thanks for any suggestions!
    Cheers,
    Thomas

    After you strip the colons from the string try something like:
    NSScanner *scan = [NSScanner scannerWithString:hexValue];
    unsigned val;
    [scan scanHexInt:&val];
    'val' will now contain the integer value of the string. 'hexValue' is the hex string. You might need to prepend "0x" to the start of the string too, after removing the colons.

Maybe you are looking for

  • Upgraded to 10.6 and now it wont lauch

    Im running Windows XP. Im not sure which version of Itunes I was running (havent updated in a while) and when I booted it before I would get a little box that would come up saying something about windows and some devices may not work blah blah, you j

  • Win_API_Environment.Get_Net_Connection????

    Hi, I need to know what does the function Win_API_Environment.Get_Net_Connection(string, boolean) and what returns. Thank!

  • ESS / MSS Calendar

    Hi , In ECC 5.0, what is needed for Setup and/or Dynpro for a employee in ESS, to maintain a calendar ( which will feed the team calendar of the Manager ) ? Is Working Time needed and a lot of IMG ? or else, ... Also, what is needed for the Manager t

  • Marking "goods movement completed" automatically during iw41 in PM

    Hi all, I have a problem during iw41. I asign material/components in component tab in order, and use t-code IW41 for confirmation, in this I confirm operation then click on goods movement icon, "completed" field is getting ticked automatically in thi

  • Voreinstellungen

    Einen schönen Nachmittag wünsche ich! Ich habe mir Adobe Reader XI auf meinem Yoga 2 installiert. Allerdings möchte ich per default nicht den Touch Modus aktiviert haben und die Werkzeugleiste ist riesengroß,also falsch skaliert. Wenn ich das in den