Converting string to int

I cannot convert this string to integer.
int tempI = Integer.parseInt("fdcedfbd", 16);
it returns a numberFormatException
but if I do this then there will be no problem.
int tempI = 0xfdcedfbd
Can anybody help me with this? Thank you.

It sounds like the real problem here is that someone is giving you an 8-byte unsigned value represented as a 16-character hexadecimal string, and they want you to convert that into the actual 8 bytes. Are they going to do bit manipulation on it or something?
If you successfully convert it into a long and they then try to do math with it, they'll run into the sign/overflow problems everyone else has been talking about. If they just want to look at the individual bits then it would make sense to do the conversion into a long.
You could do something like this:
  private static final String highOrderDigits = "89abcdef";
  public long hexToLong(String hex) {
    // Only process exactly 16-digit strings.
    if (hex.length() != 16) {
      throw new IllegalArgumentException("Expect 16-digit hex value");
    boolean overflow = false;
    // Check for most significant digit > 7, which would
    // cause problem for parseLong.
    int highOrderIndex = highOrderDigits.indexOf(hex.charAt(0));
    if (highOrderIndex > -1) {
      // Clear most-significant bit and set overflow flag.
      overflow = true;
      hex = "" + highOrderIndex + hex.substring(1);
    long result = Long.parseLong(hex, 16);
    if (overflow) {
      // Manually set most significant bit in result.
      result |= 0x8000000000000000L;
    return result;
  }I more or less tested this and it looks to work.

Similar Messages

  • How to convert String to int in JSP?

    Hi,
    I set a session attribute in Servlet and want use it in JSP, How can I convert it to int or Integer?
    the line in my code doesn't work:
    int quantity=(int)session.getAttribute("vehiclequantity") ;
    Thanks in advance.
    Wolf
    +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    <HTML>
    <HEAD>
    <TITLE>Using the for Statement</TITLE>
    </HEAD>
    <BODY>
    <H1>Using the for Statement</H1>
    <%=session.getAttribute("vehiclequantity") %>;
    <%
    int loopIndex;
    int quantity=(int)session.getAttribute("vehiclequantity") ;
    out.println(quantity);
    for (loopIndex = 1; loopIndex <=2; loopIndex++) {
    out.println("This is iteration number "
    + loopIndex + "<BR>");
    %>
    </BODY>
    </HTML>

    Learning how to read errors and understand them will help you solve your problems quicker by yourself... So lets take a look at the error and classes involved...
    The error says:
    "Cannor Resolve Symbol: method valueOf(java.lang.Object) in the class java.lang.Integer" and gives you line line where the error occurs: Integer quantity = Integer.valueOf(session.getAttribute("vehiclequantity"));
    Now, if we look at the API for the Integer we notice that are are only two valueOf methods: valueOf(java.lang.String s) and valueOf(java.lang.String s, int radix). Not valueOf(java.lang.Object) method.
    Now we look at the getAttribute(java.lang.String name) method of HttpSession we see that the method returns a java.lang.Object. Now, you know you put a java.lang.String into that attribute, but the get method returns an Object. This is because you could have put any object in there, an Integer, a String, or some other class instance. But you know it is a String, so you can cast the returned value to a String, so that you will be calling the valueOf(java.lang.String s) method of Integer with the Object returned from the HtttpSession's getAttribute(java.lang.String name) method:
    Integer quantity = Integer.valueOf((String)session.getAttribute("vehiclequantity"));

  • Exception thrown trying to convert string to int

    Hey Guys,
    i imported a csv file into my app which i divide using the split() method and put into an array then I divided the array into variables;
    fruit = piece[0];
    price = piece[1];
    unit = piece[2];Now since price has to be a double i converted it into a double;
    price2 = Double.parseDouble(price);which worked grand
    but when i try to convert unit to an integer
    unit2 = Integer.parseInt(unit);i got error
    Exception in thread "main" java.lang.NumberFormatException: For input string: "3
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    so i tried removing white spaces as follows
    target = unit.replace(" ", "");which worked and displayed grand, then i tried converting to int again but the same exception was thrown, can anyone help me with this problem, thanks
    mark

    sorry for the messed up format previously.
    Like you said the unit.replaceAll(" ", ""); did not do the trick, when displayed as a string it looked like 3 was on its own but still did not convert to int. I assume line break or something existed in the csv file like you mentioned earlier as unit was the last word in the csv file.
    when i used unit.trim(); and tried to convert to integer it worked perfectly.
    thanks all for your help,
    Mark

  • Combo Box, Converting string to int

    The user is able to select the month, year etc for a TimerTask using a combo box.
    The problem I have is converting say, "January" to an integer 0 for use to set in a calendar for the date.
    The combo box is populated with every month but i cannot link it with the int variable "month" where 0= january, 1- feb etc for use in the TimerTask.
    In other words, when the user selects "January" in the combo box i want the integer value for the variable "month" to equal 0, for "Febuary", month to equal 1, "March", month equal 2 etc etc.
    Any help would be excellent!!

    if January is the first item in the box, February is the second, all the way to December:
    month = monthComboBox().getSelectedIndex();

  • Help trying to convert string to int

    Hey im trying to input a string in an applet then turn it into a integer so i can use it any ideas?

    long val = Long.parseLong(string);
    if(val > Integer.MAX_VALUE || val <
    Integer.MIN_VALUE) {
    throw new Error("I don't like you :p");
    int intVal = (int) val;No real need to use parseLong and check explicitly the limits. The check for MIN and MAX is performed implicitely inside Integer.parseInt(string, 10), which is called by both new Integer(string).intValue() and Integer.parseInt(string) . When the check fails, it throws a NumberFormatException.
    The only difference between post#1 and post#2 is that the latter it stores the int value in the internal int data member after the call to Integer.parseInt(string, 10), hence the intValue() subsequent call.

  • Converting string number int o percentage

    Hi. I have a textfield in which it accepts numbers (2.5 or 2.35 or 32.52)
    How am I going to convert it to double and in percentage format( .0025 or .0035 or .003252)
    Thanks
    Please help

    > and just converting it back to string again. I made
    the division but converting it back to string.
    what function do i need to use?
    In Java they're called "methods".
    http://java.sun.com/developer/JDCTechTips/2004/tt1005.html#2
    http://java.sun.com/docs/books/tutorial/i18n/format/decimalFormat.html
    ~

  • Problem converting string to int

    I want to display records of flights from my database with the things that i get from a form...One of them is the number of passengers...the code is shown below;
    " SELECT * FROM flights WHERE kalkis LIKE ? AND inis LIKE ? AND DepartureDate LIKE ? AND NumPass >= ? ";
    Everything comes up right except the NumPass value...i get the ? value by saying;
    sorgulama.setInt(4,Integer.parseInt(request.getParameter("NumPass")));
    But it won't display the values that are >=...Displays nothing...
    Also the NumPass value is "int" type in the database...
    Any idea to solve this...kinda urgent ..

    Log the parameters being used in your application.
    Run the query directly against the database using the parameters your app is using, verify you find something.

  • Converting String to int again...

    telno.addActionListener(this);
    public void actionPerformed(ActionEvent e){
    int x, p, y;
    if(e.getSource()==telno){
    x = Integer.parceInt(telno.getText());
         p = x/10000;
              y = x%10000;
    there's an error 'cannot resolve symbol' when I try to compile.
    Help please, anyone!
    Thanks

    Hi,
    parseInt() and not parceInt() I think.
    Hope it helps
    S�bastien

  • Converting String type to inte type

    Can any one tell me how I can convert an array of Strings into an array of integers??

    it's simple ..... it's like this...
       // some code goes here....
       String num[]={"23", "342", "21", "1"};
        int n[]= new int[num.length];
        for(int i=0; i<num.length; i++)
             n=Integer.parseInt(num[i]); // << -- convert string to int then copied it to int[]
    // rest of your code....

  • Javascript, string to int

    Dear Sir:
    I try to convert string to int in flash javascript:
    temp="120";
    temp2="140";
    temp3=int ( temp );
    but I got a message "int is not a function", can somebody tell me how to do this?
    also, what if I want convert number back to string?
    thanks a lot!

    use:
    temp="120";
    temp2="140";
    temp3=Number ( temp );
    // to convert to a string, use:
    var temp4:String=String(temp3);
    // or
    var temp5:String=temp3.toString();

  • Leading Zeroes are lost when convert from string to int

    What I'm trying to do is simple yet the solution has seemed difficult to find.
    I have a system that requires 4 digit numbers as "requisitionNo". The system uses JSPs and accepts the 4 digit number that the user inputs (fyi - duplicate handling is already managed). The input number (rNumber) is of STRING type and in the action (using struts) is converted to an int:
    int requisitionNo = Integer.parseInt(rNumber);At that very line the issue is that when the user inputs a number with leading zeros such as: "0001" the 3 leading zeros are chopped off in the INT conversion. The application validation kicks in and says: "A 4 digit number is required" which is by design. The work around has been that the user has been putting in number that start with 9's or something like that, but this isn't how the system was intended to be used.
    How do I keep the leading zeroes from being lost instead of saving a number "1" to the database how do I keep it saving "0001" to the database? Would I just change everything to STRING on down to the database? or is there another number type that I can be using that will not chop off the leading zeroes? Please provide short code references or examples to be more helpful.

    Yeah, I have to agree here that leading zeroes make no sense. I figured that out when I started to look into this problem. The only requirement that exists is that the user wants it to be a 4 digit number due to some other requirement they have themselves.
    So what I'm gathering from what I've read in the responses thus far is that I should change the validation a bit to look at the STRING for the 4 required digits (which are really 4 characters; maybe I should add CLIENT side numeric validation; currently its doing server side numeric/integer validation; or maybe change up the server side validation instead???) and if they are ALL GOOD then let the application save the int type as it wants to. IE: Let it save "0001" as just "1" and when I come back to DISPLAY this saved number to the user I should append the string of "000" in front of the 1 for display purposes only? Am I understanding everyone correctly?

  • String to Int and Int to String

    How can I convert a string to Int & and an Int to String ?
    Say I've
    String abc="234"
    and I want the "int" value of "abc", how do I do it ?
    Pl. help.

    String.valueOf(int) takes an int and returns a string.
    Integer.parseInt(str) takes a string, returns an int.
    For all the others long, double, hex etc. RTFM :)

  • String to int conversion

    can n e body tell me how can i convert a String to an int

    always remember to use the try/catch block with thins that throw exceptions
    String gg;
    int in;
    try{  
    gg = "30";
       in = Integer.parseInt(gg);
    catch(NumberFormatException e){ 
      e.printStackTrace();
    }Ant

  • How to convert string to an integer in SQL Server 2008

    Hi All,
    How to convert string to an integer in sql server,
    Input : string str="1,2,3,5"
    Output would be : 1,2,3,5
    Thanks in advance.
    Regards,
    Sunil

    No, you cannot convert to INT and get 1,2,3 BUT you can get
    1
    2
    3
    Is it ok?
    CREATE FUNCTION [dbo].[SplitString]
             @str VARCHAR(MAX)
        RETURNS @ret TABLE (token VARCHAR(MAX))
         AS
         BEGIN
        DECLARE @x XML 
        SET @x = '<t>' + REPLACE(@str, ',', '</t><t>') + '</t>'
        INSERT INTO @ret
            SELECT x.i.value('.', 'VARCHAR(MAX)') AS token
            FROM @x.nodes('//t') x(i)
        RETURN
       END
    ----Usage
    SELECT * FROM SplitString ('1,2,3')
    Best Regards,Uri Dimant SQL Server MVP,
    http://sqlblog.com/blogs/uri_dimant/
    MS SQL optimization: MS SQL Development and Optimization
    MS SQL Consulting:
    Large scale of database and data cleansing
    Remote DBA Services:
    Improves MS SQL Database Performance
    SQL Server Integration Services:
    Business Intelligence

  • Parsing from string to int

    in my C# project I have an array type of String. the array gets numbers from type int.
    int numCell;
    string[] arr = new string[numCell];
    then i use it in a function like this:
    sum += int.Parse(arr[i]);
    and it works!
    but, when i dothe same thing in WPF it tells me:
    Input string was not in a correct format.
    what shouuld i do?

    Why did you mark your own question as answer? Please unmark it.
    >>if in C# it worked why wouldn't it work in wpf?
    As I have already mentioned, it is because of the actual value of arr[i]. It has nothing to do with Windows Forms or WPF but it is possible that your WPF application has put a different value in the array than your Windows Forms Application did. And if this
    specific value is not a valid number the parsing will always fail. So you should check the values that put into the array and probably use the int.TryParse method to do the parsing:
    int sum;
    if (!int.TryParse(arr[i], out sum))
    //the value of arr[i] was NOT a valid number...
    This will however not change the fact that for example the value "D1" cannot be converted to an int using the int.Parse or int.TryParse method.
    Please remember to close your threads by marking helpful posts as answer and please start a new thread if you have a new question.

Maybe you are looking for

  • How to change data in an Email

    In BW BPS (v3.5), how can you add details to the email(s) that are generated?

  • Can't set alarm, type of alarm isn't even visible

    My alarms worked a month ago and now I can't even set one because the pulldown menu is grayed out. And the option to select what type of alarm I want isn't showing up at all. I just downloaded and reinstalled 1.5.5 but no help. Any ideas? Sherry

  • Programmatically bulk upload of files via restful API

    Hi, If I am building a custom client application and need to bulk upload say 10 files.. what restful API does sharepoint include to accomplish that. A couple of more questions on the above: 1. Is there any limitation to the size of the file that I ca

  • Firefox installed but not working even after importing settings from IE

    I've just downloaded firefox and installed with settings imported from IE which is working very fine.but i couldn't get firefox worked.it keeps on saying 503 error,but IE and chrome are working excellent at the same time.i prefer firefox than those f

  • Domain Migration :from single domain to multiple domain.

    Hi , We have an urgent requirement for the domain migration The scenario is currently we are using MS AD as LDAP server to store portal users and thier credentials. Lets say we have current domain name as : ad.abc.com we are planning to migrate from