Convert any base number to decimal

Is there a way to convert any number of base 2-16 into decimal?
The numbers can be input using scanner
Anyone has any code for this?

Hey, guys this is what I got so far for this. But, of course there are issues
1. I'm not sure how to show "invalid" message if a character or floating point number is entered
2. I'm not sure about the code that will only allow the relevant numbers to be entered for each base.
E.g - If base = 2 then number == 1 || 0
This has to be repeated for all allowed base from 2-16
Please help!!!
import java.util.Scanner;
public class BaseConverter {
public static void main(String[] args) {
int base;
String number;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter base value (Integer between 2 to 16): ");
base = scanner.nextInt();
if (base < 2 || base > 16) {                     // how to get same invalid message for character or float (e.g. -12.2)
System.out.println("Invalid base. Please retry");
return;
System.out.print("Enter the number in base : ");
number = scanner.next();
if (scanner.hasNext()) {           // not sure how to validate in relation to entered base
} else {
System.out.println("Invalid number. Please retry");
return;
int decimalValue = 0;
int step = 0;
for (int i = number.length() - 1; i >= 0; i--) {
char c = number.charAt(i);
int cValue = Character.digit(c, base);
decimalValue = decimalValue + (cValue * (int) Math.pow(base, step));
step = step + 1;
System.out.println("Decimal value: " + decimalValue);
}

Similar Messages

  • Converting from BASE 64 to Decimal Base 10

    I have an requirement to convert a value from base 64 to decimal base 10
    for ex if i give b the resultant output should be 27
    as per the website -- http://convertxy.com/index.php/numberbases/
    I tried using the UTL_ENCODE.BASE64_ENCODE with UTL_RAW.CAST_TO_RAW
    select utl_raw.cast_to_varchar2(utl_encode.base64_encode(utl_raw.cast_to_raw('b'))) from dual
    but i am not getting the desired result
    Kindly guide me in this issue
    With Warm Regards
    SSR

    The base64 encoding for 27 is not b
    SELECT UTL_RAW.cast_to_varchar2 (
              UTL_ENCODE.base64_encode (
                 UTL_RAW.cast_to_raw (
                    '27')))
              base64
      FROM DUAL
    BASE64
    "Mjc="27 ist the position of b in
    ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
    which you need in the first step to convert base64 to decimal.
    select
    instr(
      'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
    ,'b'
    ) -1 d
    from dual
    d
    27
    vice versa
    select
    substr(
      'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
      ,27 + 1
      ,1
    ) d
    from dual
    D
    "b"You dont get a result with your approach because b is not a valid base64-encoding.
    Otherwise thsi should give a result
    SELECT UTL_RAW.cast_to_varchar2 (
              UTL_ENCODE.base64_decode (
                 UTL_RAW.cast_to_raw (
                    'b')))
              base64
      FROM DUAL
    BASE64
    but it doesnt, it does in the case below
    SELECT UTL_RAW.cast_to_varchar2 (
              UTL_ENCODE.base64_decode (
                 UTL_RAW.cast_to_raw (
                    'Mjc=')))
              base64
      FROM DUAL
    BASE64
    "27"Edited by: chris227 on 12.03.2013 07:36
    Edited by: chris227 on 12.03.2013 07:41
    Edited by: chris227 on 12.03.2013 07:46
    Edited by: chris227 on 12.03.2013 08:20

  • CONVERT BASE 36 TO DECIMAL

    Any Vi to CONVERT BASE 36 TO DECIMAL?

    Here's the explanation:
    The math behind the value is quite simpe: base^position, where the first position left of the decimal sign is 1.
    Ton
    Message Edited by TonP on 11-13-2008 08:21 AM
    Message Edited by TonP on 11-13-2008 08:21 AM
    Free Code Capture Tool! Version 2.1.3 with comments, web-upload, back-save and snippets!
    Nederlandse LabVIEW user groep www.lvug.nl
    My LabVIEW Ideas
    LabVIEW, programming like it should be!
    Attachments:
    Base 36_BD.png ‏8 KB

  • Decimal of SQL Server getting converted to Whole number in ORACLE

    Hi All,
    I am using ORACLESQL Developer tool to migrate database from SQL Server to ORACLE. The decimal datatype in Sql Server is not getting converted to its equivalent in ORACLE. Do anyone face the same problem? Its getting converted to WHOLE number.
    Also I am able to import data only from Excel sheet. Is therre any other option to import data using this tool?
    Also is the tool the best to use for migration? Please give me your suggestions pls since I am facing this conversion problem after import lakhs of data.
    I am using version 1.1.3
    Thanks in Advance,
    Srinivasan.T

    I have upgraded to the version mentioned by you. Even now it is not getting upgraded. I am exporting data using Excel sheet only. Is it possible for you to explain how to migrate data thru SQL * PLUS?
    Thanks,
    Srinivasan.T

  • Converting a number with decimal to word

    hi
    how to convert a number with decimal to words in oracle
    to_char(to_date(1311,'J'), 'JSP') giving error with number having decimal

    and very nls dependant ;-)nls-dependant because you used comma in the mask as a decimal delimiter?
    but instead of
    regexp_replace(n,'(\d*),(\d*)','\2')you can simply use:
    regexp_substr(n,'\d*$')but in case there is no fractional part it would return the whole number, but still you can avoid usind delimiter as:
    regexp_replace(n,'^\d*|(\d*)$|.','\1')PS
    of course it is not really serious, cause you can get the fractional part, without using regular expressions at all, namely: val-trunc(val)

  • Is there any FM convert char to number ?

    Hi guys,
    Is there any FM convert char to number ?
    what are they? and how can I find them ?
    thx in advance.

    Hi
    data: wl_char(3) type c value '123',
          wl_num type n .
          CALL FUNCTION 'MOVE_CHAR_TO_NUM'
               EXPORTING
                    CHR             = wl_char
               IMPORTING
                    NUM             = wl_num
               EXCEPTIONS
                    CONVT_NO_NUMBER = 1
                    CONVT_OVERFLOW  = 2
                    OTHERS          = 3.

  • Error in running a function to convert coordinates in degrees to decimal for EXCEL VBA

    For your information, I have 3 cross-posts regarding this question - and all I can said there is still no firm solution regarding this error.
    1) http://stackoverflow.com/questions/27634586/error-in-running-a-function-to-convert-coordinates-in-degrees-to-decimal-for-exc/27637367#27637367
    2) http://www.mrexcel.com/forum/excel-questions/826099-error-running-function-convert-coordinates-degrees-decimal-excel-visual-basic-applications.html#post4030377 
    3) http://www.excelguru.ca/forums/showthread.php?3909-Error-in-running-a-function-to-convert-coordinates-in-degrees-to-decimal-for-EXCEL-VB&p=16507#post16507
    and the story of the error is as below:
    Currently I am working on VBA excel to create a widget to verify coordinates whether it lies under the radius of ANOTHER predefined and pre-specified sets of coordinates.
    In the module, I want to convert the coordinates from degrees to decimal before doing the calculation - as the formula of the calculation only allow the decimal form of coordinates.
    However, each and every time I want to run the macros this error (Run-time error '5', invalid procedure call or argument) will appear. Then, the debug button will bring me to below line of coding:
    degrees = Val(Left(Degree_Deg, InStr(1, Degree_Deg, "°") - 1))
    For your information, the full function is as below:
    Function Convert_Decimal(Degree_Deg As String) As Double
    'source: http://support.microsoft.com/kb/213449
    Dim degrees As Double
    Dim minutes As Double
    Dim seconds As Double
    Degree_Deg = Replace(Degree_Deg, "~", "°")
    degrees = Val(Left(Degree_Deg, InStr(1, Degree_Deg, "°") - 1))
    minutes = Val(Mid(Degree_Deg, InStr(1, Degree_Deg, "°") + 2, _
    InStr(1, Degree_Deg, "'") - InStr(1, Degree_Deg, "°") - 2)) / 60
    seconds = Val(Mid(Degree_Deg, InStr(1, Degree_Deg, "'") + _
    2, Len(Degree_Deg) - InStr(1, Degree_Deg, "'") - 2)) / 3600
    Convert_Decimal = degrees + minutes + seconds
    End Function
    Thank you.
    Your kind assistance and attention in this matter are highly appreciated.
    Regards,
    Nina.

    You didn't give an example of your input string but try the following
    Sub test()
    Dim s As String
    s = "180° 30' 30.5""""" ' double quote for seconds
    Debug.Print Deg2Dec(s) ' 180.508472222222
    End Sub
    Function Deg2Dec(sAngle As String) As Double
    Dim mid1 As Long
    Dim mid2 As Long
    Dim degrees As Long
    Dim minutes As Long
    Dim seconds As Double ' or Long if only integer seconds
    sAngle = Replace(sAngle, " ", "")
    mid1 = InStr(sAngle, "°")
    mid2 = InStr(sAngle, "'")
    degrees = CLng(Left$(sAngle, mid1 - 1))
    minutes = CLng(Mid$(sAngle, mid1 + 1, mid2 - mid1 - 1))
    seconds = Val(Mid$(sAngle, mid2 + 1, 10)) ' change 10 to 2 if only integer seconds
    Deg2Dec = degrees + minutes / 60 + seconds / 3600
    End Function
    As written the function assumes values for each of deg/min/sec are included with unit indicators as given. Adapt for your needs.
    In passing, for any work with trig functions you will probably need to convert the degrees to radians.

  • Is there a way to convert skype online number cont...

    That was the general question, and here are my details:
    I have had a skype number for many years and it was great, around $5.5 a month to call all phones in North America from my laptop from any place in the world with internet. With time, I had a ccumulated a relatively large number of contacts (name of person or party & their phone number(s)). Now I decided that I finally need a regular "smart" cellphone with an AT &T 10c/minute and no data (that's the only way that I understand is not a rip-off, I'm counting on my really unlimited mobile hotspot to get wifi). Now, the company will have me purchase minutes every 30 days which doesn't look very bad considering that I don't have my wifi all the time.
    The problem I'm having is, how to transfer or convert the name/number list on skype to name/number list on my phone? so I can call the same numbers I used to call on slype with my AT&T minutes?
    Typing the numbers one-by-one to my phone may take days!! so, I figured out that there must be away to do it automatically, rather than manually.
    Any help is very appreciated

    Is there any answer skype people?????
    please!

  • Convert time to number and number to time

    I'm using SAP 4.0b
    I'm looking for function that converts time to number and number to time.
    Any suggestion?

    Hi,
    Can you give an example of what number you want to give to get what time and vice-versa?
    There can be a direct conversion between an integer and a time variable in ABAP.
    For example -
    data: number type i,
          time type syuzeit.
    number = 3600.
    move number to time.
    write time.
    The time will be output as "01:00:00". That is because 3600 seconds make up one hour.
    Regards,
    Anand Mandalika.

  • How to convert exponent to number format

    Hi,
    how to convert exponent to number format.My column is in number and the data is in exponent format .for example  data is 2.44705130197009E18 .when i do field name haveing this data like
    select * from table name where filed name =2.44705130197009E18 ,field name  is of number (20) data type .but i am not getting any data fectched .
    your advice is at most appriciated .
    Regards,
    Suja

    Example of what I was saying...
    SQL> create table myexp (name number);
    Table created.
    SQL> insert into myexp values (2447051301970090001);
    1 row created.
    SQL> select name from myexp;
          NAME
    2.4471E+18
    SQL> col name format 9999999999999999999999999999999999999
    SQL> select name from myexp;
                                      NAME
                       2447051301970090001
    SQL>
    The number you are seeing as an exponent is not just the number that is stored with lots of 0's on it, there's other information you are missing, so you need to change your format to see the true value.

  • To convert Old material Number (BISMT) to  new material number

    HI Folks,
                           I have the requirement is how to convert OLD material number to New material number.  Is  thier any  Global  exists is thier? Please let me know.
    Thanks,
    Vijay

    Hi,
           use MGA00001 in changing parameters u can change...
    Regards

  • Number to a binary base number

    How can I convert a numeric data to a binary base number?
    Ex.: A into 0001010
    Thank you in advance

    OK, only conversion to a string is needed to solve the problem. (Everything else is just a cosmetic feature of the indicator where you set the formatting. This does not change the underlying data.)
    Just use "format into string" with a format code of "%07b" (=show seven binary digits and pad with zeroes to the left).
    LabVIEW Champion . Do more with less code and in less time .

  • Is there any function module todelete decimal values

    hi all
    is there any function module todelete decimal values not converting charcter or numeric values i want to change my value with decimals to with out decimals.
    please give me suggetion.
    Thanks
    Ramana reddy

    hi dear
    you can use type P or I
    Data W_VAL type P.
    Data W_VAL type I.
    or assigned dictionary field like Mara-MATNR as getting your value
    Rewards if Useful.

  • Convert xsd:double to xsd:decimal

    Hi,
    I have an incoming field which is xsd:double and I need to convert the field into xsd:decimal
    For example:
    Incoming field - 5.13964E11
    Outgoing field - 513964000.00
    Any ideas how to do this ??
    Thanks
    Colin.

    Hi Colin,
    you can create a target data type in which you can take outgoing field as string variable.....
    then in msg mapping, make a small UDF in JAVA.......take your input field to it....
    in UDF, cast the input to double..........then convert it to decimal.....then cast it back to string.......then output this string.......
    then take output of UDF to target........so you will get outgoing field in decimal format as string............
    Then you can process your target XML in your target system.
    Thanks,
    Rajeev Gupta
    Message was edited by:
            RAJEEV GUPTA

  • Determine Number of Decimal Place using BigDecimal

    I was interested to have the following getNumberOfDecimalPlace function :
    System.out.println("0 = " + Utils.getNumberOfDecimalPlace(0)); // 0
    System.out.println("1.0 = " + Utils.getNumberOfDecimalPlace(1.0)); // 0
    System.out.println("1.01 = " + Utils.getNumberOfDecimalPlace(1.01)); // 2
    System.out.println("1.012 = " + Utils.getNumberOfDecimalPlace(1.012)); // 3
    System.out.println("0.01 = " + Utils.getNumberOfDecimalPlace(0.01)); // 2
    System.out.println("0.012 = " + Utils.getNumberOfDecimalPlace(0.012)); // 3
    I use the following code
        public static int getNumberOfDecimalPlace(double value) {
            final BigDecimal bigDecimal = new BigDecimal("" + value);
            final String s = bigDecimal.toPlainString();
            System.out.println(s);
            final int index = s.indexOf('.');
            if (index < 0) {
                return 0;
            return s.length() - 1 - index;
        }However, for case 0, 1.0, it doesn't work well. I expect, "0" as result. But they turned out to be "0.0" and "1.0". This will return "1" as result.
    0.0
    0 = 1
    1.0
    1.0 = 1
    1.01
    1.01 = 2
    1.012
    1.012 = 3
    0.01
    0.01 = 2
    0.012
    0.012 = 3
    Any solution?

    Please [don't cross-post!|http://stackoverflow.com/questions/2296110/determine-number-of-decimal-place-using-bigdecimal], it's considered rude. If you must do it, then at least link each post so that people can find out which answers you've already got in order to avoid duplicate work on our part.
    Please read [_How To Ask Questions The Smart Way_|http://www.catb.org/~esr/faqs/smart-questions.html].

Maybe you are looking for

  • Unable to load WSDL after moving to Air 2.6

    I have an AIR app that works fine in 2.5, but fails after moving to FB 4.5 & SDK 4.5.1 & Air 2.6! It fails to load a WSDL file if the host address is in a separate network. My box is in 10.0.1.202, and the following wsdls load fine: - http://localhos

  • My MacBook Pro 13" (2012) is running Windows 7 on Bootcamp and there is an issue with the headset I'm using.

    My MacBook Pro 13" (2012) is running Windows 7 on Bootcamp and there is an issue with the headset I'm using. I use a cheap Logitech Stereo Headset H130, but since there is only on audio plugin on the computer, I purchased a 3.5 mm 4-pin smartphone au

  • New Retina Macbook Pro - strange whining Sound

    Hi, i just received my new Macbook Pro Retina - brilliant machine. But suddenly it starts to whine - I can hear the whining while sitting in front of the notebook, so its pretty loud. The sound does appear when I move windows or enter mission control

  • Login failure (cannot modify header) PHP/MySQL

    I have a login form 100% Dreamweaver8 built using PHP/MySQL. After logging in it fails to goto the $MM_redirectLoginSuccess page (index.php). I did not have this issue prior to the 8.02 update. I designed it the same way as I had before the 8.02 upda

  • AirPort not recognizing network

    I have a wireless network set up in my house (A PC and Linksys router - WRT54GS. The PC is connected directly to the router using an Ethernet cable. I installed the Linksys router for my Mac) I purchased an Airport card for my G5 and AirPort is insta