Click here to Skip to main content
15,884,176 members
Articles / Programming Languages / C++
Article

TCX Unit Conversion Library

Rate me:
Please Sign up or sign in to vote.
4.60/5 (5 votes)
1 Dec 1999 83.3K   1.7K   39   4
  • Download demo project - 18 Kb
  • Download source files - 7 Kb
  • Sample Image - TcxUnitConverter.gif

    This library offers functions to convert values between units of measurement. It's very simple to use. For example, to convert between mph (miles per hour) to m/s (meters per second), just use the following code:

    double dValue = 1.0;
    TcxConvertUnit( dValue, _T("mph"), _T("m/s") );
    // <dValue> is now in "m/s"

    The units are represented as strings, as the intention of the library is to afford unit entrance support at the UI level. In fact, the Demo Application that accompanies this article is a very simple (yet powerful) unit conversion utility.

    The table of supported units is currently hard coded. You can add your own new units just following the instructions in the source file (TcxUnitConverter.cpp).

    The unit interpreter accepts all the multiplication prefixes of the International System of Units (see the Appendices section).

    The unit interpreter accepts also linear unit expressions. For example, you can convert from acre (Acres) to mi^2 (square miles), or from N (Newtons, the unit of strength in the International System of Units) to m*kg/s^2 (meter times kilogram per square second).

    The following are samples of valid unit expressions (they are all units of strength).

  • m*kg/s^2
  • m*kg*s^-2
  • m/kg^-1*s*s
  • kg*s^-2/m^-1

    There can be only one division ('/') symbol in the expression, and all right terms are considered as part of the denominator (e.g. m/s^2 equals m/s*s). Parenthesis are not used. Exponents, when applicable, must be positive or negative literal integers.

    This source code is provided "as is" without express or implied warranty.
    Use it at your own risk!

    While I have made every effort to remove any undesirable "bugs", I cannot be held responsible if it causes any damage or loss of time, data, or Martian probes.


    Known Limitations

    The unit converter can only perform the following type of conversion:

    Unit1 = K * Unit2;  // K is a floating point constant
    

    This is valid for the greatest multitude of the common day units we can think of, but for temperature units.

    Temperature units as Kelvin, Celsius and Fahrenheits all demand conversion formulas in the following form:

    Unit1 = K * Unit2 + K0;  // K and K0 are floating point constants
    

    The library will still convert between these units, but, in this case, the value is supposed to be a temperature variation rather than an absolute temperature.


    Things to do

  • Implement UI Controls that automatically make use of these features.
  • Implement a more flexible Unit Converter so sets of units can be "loaded" and combined in runtime.
  • Buy a Ferrari.


    Using the Demo Application

    In the From/Value field, enter the source value you want to convert. In the From/Unit field, type the unit of the source value, or choose one from the dropdown list. In the To/Unit field, type the target unit or choose one from the dropdown list. The result will be displayed in the To/Result just-in-time. You can select & copy from this field.

    To enter unit expression in either the From/Unit or the To/Unit fields, you must type them. The dropdown lists show only sigle units.


    Appendices


    Table 1: Multiplication prefixes of the International System of Units

    PrefixNameConstant
    YYotta1e+24
    ZZetta1e+21
    EExa1e+18
    PPeta1e+15
    TTera1e+12
    GGiga1e+9
    MMega1e+6
    k or Kkilo1e+3
    h or Hhecto1e+2
    DDeka1e+1
    ddeci1e-1
    ccent1e-2
    mmili1e-3
    u or µmicro1e-6
    nnano1e-9
    ppico1e-12
    ffemto1e-15
    aatto1e-18
    zzepto1e-21
    yyocto1e-24


    Table 2: Basic Units

    AbbrTag
    AAmpere
    KKelvin
    cdCandela
    gGram
    mMeter
    molMole
    rRadian
    sSecond
    srSteradian


    Table 3: Derived Units

    AbbrTagDefinition
    Bqbecquerel1 [l/s]
    BtuInternationalTableBtu1055.05585262 [Kg*m^2/s^2]
    CCoulomb1 [A*s]
    CiCurie3.7*1010 [l/s]
    FFarad1 [A^2*s^4/Kg*m^2]
    FdyFaraday96487 [A*s]
    GyGray1 [m^2/s^2]
    HHenry1 [Kg*m^2/A^2*s^2]
    HzHertz1 [s^-1]
    JJoule1 [Kg*m^2/s^2]
    KnotNauticalMilesPerHour463.0/900.0 [m/s]
    NNewton1 [Kg*m/s^2]
    OhmOhm1 [Kg*m^2/A^2*s^3]
    OzUKUKFluidOunce2.8413075e-5 [m^3]
    PPoise0.1 [Kg/m*s]
    PaPascal1 [Kg/m*s]
    PdlPoundal0.13825495376 [Kg*m/s^2]
    PicaPica1.0/72.0 [in]
    RRoentgen0.000258 [A*s/Kg]
    SSiemens1 [A^2*s^3/Kg*m^2]
    StStokes0.0001 [m^2/s]
    SvSievert1 [m^2/s^2]
    TTesla1 [Kg/A*s^2]
    UUnifiedAtomicMass1.6605402e-27 [Kg]
    VVolt1 [Kg*m^2/A*s^2]
    WWatt1 [Kg*m^2/s^3]
    WbWeber1 [Kg*m^2/A*s^2]
    acreAcre4046.87260987 [m^2]
    arcminArcMinute2.9088820866e-4 [r]
    arcsArcSecond4.848136111e-6 [r]
    atmAttmosphere101325 [Kg/m*s^2]
    auAstronomicalUnit1.495979*1011 [m]
    bBarn1e-28 [m^2]
    barBar100000 [Kg/m*s^2]
    bblBarrel0.158987294928 [m^3]
    buBushel0.03523907 [m^3]
    cLightSpeed299792458 [m/s]
    calCalorie4.1868 [Kg*m^2/s^2]
    chainChain20.1168402337 [m]
    ctCarat0.0002 [Kg]
    cuUSCup2.365882365e-4 [m^3]
    dday86400 [s]
    dynDyne0.00001 [Kg*m/s^2]
    eVElectronVolt1.60217733e-19 [Kg*m^2/s^2]
    ergErg0.0000001 [Kg*m^2/s^2]
    fathFathom1.82880365761 [m]
    fbmBoardFoot0.002359737216 [m^3]
    fcFootcandle10.7639104167 [cd*sr/m^2]
    fermiFermi1e-15 [m]
    flamFootlambert3.42625909964 [cd/m^2]
    ftInternationalFoot0.3048 [m]
    ftUSSurveyFoot0.304800609601 [m]
    gaStandardFreefall9.80665 [m/s^2]
    galUSGallon0.003785411784 [m^3]
    galCCanadianGallon0.00454609 [m^3]
    galUKUKGallon0.004546092 [m^3]
    gfGramForce0.00980665 [Kg*m/s^2]
    gradGrade1.57079632679e-2 [r]
    grainGrain0.00006479891 [Kg]
    hHour3600 [s]
    haHectare10000 [m^2]
    hphorsepower745.699871582 [Kg*m^2/s^2]
    inInch0.0254 [m]
    inH2OInchesOfWater248.84 [Kg/m*s^2]
    inHgInchesOfMercury3386.38815789 [Kg/m*s^2]
    kipKilopoundForce4448.22161526 [Kg*m/s^2]
    kphKilometersPerHour5.0/18.0 [m/s]
    lLiter0.001 [m^3]
    lamLambert3183.09886184 [cd/m^2]
    lbAvoirdupoisPound0.45359267 [Kg]
    lbfPoundForce4.44822161526 [Kg*m/s^2]
    lbtTroyPound0.3732417216 [Kg]
    lmLumen1 [cd*sr]
    lxLux1 [cd*sr/m^2]
    lyrLightYear9.46052840488*1015 [m]
    mhoMho1 [A^2*s^2/Kg*m^2]
    miInternationalMile1609.344 [m]
    miUSUSStatuteMile1609.34721869 [m]
    milMil0.0000254 [m]
    minMinute60 [s]
    mmHgMilimeterOfMercury133.322368421 [Kg/m*s^2]
    mphMilesPerHour0.44704 [m/s]
    nmiNauticalMile1852 [m]
    ozOunce0.028349523125 [Kg]
    ozflUSFluidOunce2.95735295625e-5 [m^3]
    oztTroyOunce0.0311034768 [Kg]
    pcParsec3.08567818585106 [m]
    phPhot10000 [cd*sr/m^2]
    pkPeck0.0088097675 [m^3]
    psiPoundsPerSquareInch6894.75729317 [Kg/m*s^2]
    ptPint0.000473176473 [m^3]
    qtQuart0.000946352946 [m^3]
    radRad0.01 [m^2/s^2]
    rdRod5.02921005842 [m]
    remRem0.01 [m^2/s^2]
    sbStilb10000 [cd/m^2]
    slugSlug14.5939029372 [Kg]
    stStere1 [m^3]
    tMetricTon1000 [Kg]
    tbspTablespoon1.47867647813e-5 [m^3]
    thermEECTherm105506000 [Kg*m^2/s^2]
    tonShortTon907.18474 [Kg]
    tonUKUKLongTon1016.0469088 [Kg]
    torrTorr133.322368421 [Kg/m^2]
    tspTeaspoon4.92892159375e-6 [m^3]
    ydInternationalYard0.9144 [m]
    yrYear31556925.9747 [s]
    °Degree1.74532925199e-2 [r]
    °CDegreeCelsius1.0 [K]
    °FDegreesFahrenheit1.0/1.8 [K]
    °RDegreesRankine1.0/1.8 [K]
    µMicron1e-6 [m]
    ÅAngstrom1e-10 [m]
  • License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    Written By
    United States United States
    This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

    Comments and Discussions

     
    GeneralFails on pressure conversion Pin
    derraketenmann28-Dec-08 3:39
    derraketenmann28-Dec-08 3:39 
    Questionis the zip file corrupted. Pin
    wroscoe10-Dec-07 7:21
    wroscoe10-Dec-07 7:21 
    GeneralPerfect, thanks Pin
    Obliterator10-Oct-01 4:27
    Obliterator10-Oct-01 4:27 
    GeneralOCX Pin
    torulf12-Apr-00 4:29
    torulf12-Apr-00 4:29 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.