CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Mar 2004
    Posts
    119

    Simple conversion problem

    OK this is probably the most basic C++ question, but I can't seem to figure it out so I thought I'd ask.

    I'm using a 3rd party supplied structure to read data from a file. Some of the members of this structure hold a time value (i.e. hour, minute, second) as a BYTE. Their sample code shows how parse the values as a string using...

    sprintf("%2.2X:%2.2X:%2.2X", hour, minute, second);

    and that works perfectly. However when I try to use the values in a calculation they're not the same. And even when I use integer values in the sprintf like so...

    sprintf("%2.2i:%2.2i:%2.2i", hour, minute, second);

    the numbers are wrong. So I'm assuming that the value is stored as a hex value, and that's what's causing the problem. However I'm still pretty green when it comes to C++ and I can't figure out a way to convert this hex value to a proper int so I can use it in a calculation.

    Could anyone point me in the right direction?

    Thanks,
    Dan

  2. #2
    Join Date
    May 2004
    Location
    45,000FT Above Nevada
    Posts
    1,539

    Re: Simple conversion problem

    You can convert each item (hour,min,sec) to a string with sprintf, then convert them to an int or long using atoi(), or atol(). Or if you need to use a different BASE number you can use strtol().

    Code:
    char *hex= "FF";
    int num = static_case<int>(strtol(hex,NULL,16));
    Last edited by Vanaj; March 3rd, 2007 at 12:43 PM.
    Jim
    ATP BE400 CE500 (C550B-SPW) CE560XL MU300 CFI CFII

    "The speed of non working code is irrelevant"... Of course that is just my opinion, I could be wrong.

    "Nothing in the world can take the place of persistence. Talent will not; nothing is more common than unsuccessful men with talent. Genius will not; unrewarded genius is almost a proverb. Education will not; the world is full of educated derelicts. Persistence and determination are omnipotent. The slogan 'press on' has solved and always will solve the problems of the human race."...Calvin Coolidge 30th President of the USA.

  3. #3
    Join Date
    Dec 2005
    Location
    Prague, Czech Republic
    Posts
    208

    Re: Simple conversion problem

    If i understand the coding correctly, the proper way to convert is
    Code:
    x = ((num / 16) * 10) + (num % 16)

  4. #4
    Join Date
    Mar 2004
    Posts
    119

    Re: Simple conversion problem

    That worked, thank you.

    Jim, your solution worked as well but I thought converting it to a string first then back to an int was kind of inefficient.

    Thank you both for the help.

    Dan

  5. #5
    Join Date
    May 2004
    Location
    45,000FT Above Nevada
    Posts
    1,539

    Re: Simple conversion problem

    Quote Originally Posted by Dan203
    That worked, thank you.

    Jim, your solution worked as well but I thought converting it to a string first then back to an int was kind of inefficient.

    Thank you both for the help.

    Dan
    From your code it looked like you were converting it to a string anyway, that is why I did suggest that..
    Jim
    ATP BE400 CE500 (C550B-SPW) CE560XL MU300 CFI CFII

    "The speed of non working code is irrelevant"... Of course that is just my opinion, I could be wrong.

    "Nothing in the world can take the place of persistence. Talent will not; nothing is more common than unsuccessful men with talent. Genius will not; unrewarded genius is almost a proverb. Education will not; the world is full of educated derelicts. Persistence and determination are omnipotent. The slogan 'press on' has solved and always will solve the problems of the human race."...Calvin Coolidge 30th President of the USA.

  6. #6
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Simple conversion problem

    Quote Originally Posted by Dan203
    That worked, thank you.
    WHAT??? What worked? What kind of data is returned to you? What do you mean "as a BYTE"? In a BYTE? How is it encoded? Just a value? You obviously can't have a two-digit value represented as text in one char...
    So, if it is a value - it's neither hex nor decimal. It could NOT possibly be formatted correctly with %X spec - you'd get "0A" instead of "10".
    I can't imagine what the formula "x = ((num / 16) * 10) + (num % 16)" is supposed to do to a "num"... "11" will be "11", and "17" will become "11"?
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  7. #7
    Join Date
    Dec 2005
    Location
    Prague, Czech Republic
    Posts
    208

    Re: Simple conversion problem

    VladimirF> i have no idea what the 3rd party struct the OP's using is intended for, but from the code sample he posted it is clear that it is decimal number encoded in hexa in a way so it looks like decimal - eg. 11 is 0x11(=17), 23 is 0x23(=35) and so on.

  8. #8
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Simple conversion problem

    Quote Originally Posted by JohnyDog
    VladimirF> i have no idea what the 3rd party struct the OP's using is intended for, but from the code sample he posted it is clear that it is decimal number encoded in hexa in a way so it looks like decimal - eg. 11 is 0x11(=17), 23 is 0x23(=35) and so on.
    I *REALLY* doubt that. Lets wait for the OP to clear it up...
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  9. #9
    Join Date
    Dec 2005
    Location
    Prague, Czech Republic
    Posts
    208

    Re: Simple conversion problem

    Sure, lets wait Anyway,from the code he got from the documentation shows printing the time as hexadecimal (%X), yet it gives correct (decimal time) results. And he says my formula's working I remember using various hacks when i was writing code for microcontrollers and had to deal with base-10 numbers ...

  10. #10
    Join Date
    Mar 2004
    Posts
    119

    Re: Simple conversion problem

    Johny is right. The structure I'm using holds data about a DVD from an IFO file. The value is read directly from the IFO and stored as a BYTE variable within the structure. The thing I was saying about the string is that the only place this variable is used in the sample code provided is in a string, so I had no point of reference as to how to convert it to an int that I could use in an actual calculation. Specifically what I was trying to do was figure out the length of a DVD chapter in DirectShow units. However when I had one that was say 30 minutes long it would always get converted to 48, which obviously screwed up what I was trying to do. Johny's code properly converted it to 30 so I could multiply it out like I needed.

    Dan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured