CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jun 1999
    Location
    NY
    Posts
    28

    Simple conversion question

    How would I convert an unsigned short to an INT?
    How would I convert a string to an unsigned short?

    I don't have VC++, but I figured somone could help me here.. I have borland C++ 4.52.. Is there a predefined function for this? Is their possibly an API function for this conversion??

    Please help - Anything would be greatly appreciated


    Illusioned

  2. #2
    Join Date
    Jun 1999
    Posts
    319

    Re: Simple conversion question

    Hope this helps you.
    unsigned short nYourShort;
    int nmyInt = (int)nYourShort;
    For the string. A string is actually a list of caracters(unsigned short or whatever for Unicode). How do you want to translate a list into a double byte value. Maebe i don't well understand you.
    Let me know if this help you,
    Best regards,
    Faby


  3. #3
    Join Date
    Jun 1999
    Posts
    319

    Re: Simple conversion question

    Sorry for the previous. I think i understand your message Try with "atoi" function.
    Regards,
    Faby


  4. #4
    Join Date
    Jun 1999
    Location
    NY
    Posts
    28

    Re: Simple conversion question

    Im mainly interested in the conversion of the unsigned short to an int. I've used the method you've desribed already [ myInt = (int)myShort ]and I get a protection fault.. Any other clues?

    Illusioned

  5. #5
    Join Date
    Jun 1999
    Posts
    319

    Re: Simple conversion question

    It's something wrong with your code. This should be working. There are no API fuctions whitch make this conversion. Or maebe is a Borland error. Did you tryed to rebuild all?
    Regards,
    Faby


  6. #6
    Join Date
    Jun 1999
    Location
    NY
    Posts
    28

    Re: Simple conversion question

    Yes, and I've found it was not your conversion that was causing the error it is the next few lines... What Im trying to do is get the separate parts of the system time from the GetSystemTime API.. heres a peice of the code.. any Ideas?

    SYSTEMTIME CurTime;
    GetSystemTime(&CurTime);
    char *TempStr;
    char *TimeCPTN;
    int myInt;
    myInt = (int)&CurTime.wHour;
    TempStr = itoa(myInt, TempStr, 10);
    strcat(TimeCPTN, TempStr); //<-- Causes the GPF
    strcat(TimeCPTN, ":");




    Also when I do the conversion myInt = (int)&CurTime.wHour; It returns a Giant number thats seems to change each 100'th of a second... Im so new at all this.. any Ideas?


    Illusioned

  7. #7
    Join Date
    Jun 1999
    Posts
    319

    Re: Simple conversion question

    This is an pointer
    myInt = (int)&CurTime.wHour;
    Try this :
    myInt = (int)CurTime.wHour;

    Faby



  8. #8
    Join Date
    Jun 1999
    Location
    NY
    Posts
    28

    Re: Simple conversion question

    That seems to have fixed the problem (: Now the only problem Im having is for hour it's returning 6 when it should be returning 2, but im sure I can figure that out my self.. Thanks alot, you were a great help

    Illusioned

  9. #9
    Join Date
    May 1999
    Posts
    318

    Re: Simple conversion question

    I read your code with little late but for me it seems to have a big problem.

    I think the reponse to prefer 'CurTime.wHour' than '&CurTime.wHour' is right but do not explain the GPF. Because not matter the value you give to myInt itoa and strcat works well.

    But one little question. Is your code exactly has described.
    Because for me TempStr and TimeCPTN point on nothing and you cannot use these variables with itoa or strcat.

    This will work better :


    SYSTEMTIME CurTime;
    GetSystemTime(&CurTime);
    char TempStr[20]; // 20 is an example here
    char TimeCPTN[20];
    int myInt;
    myInt = (int)CurTime.wHour;
    TempStr = itoa(myInt, TempStr, 20);
    strcat(TimeCPTN, TempStr);
    strcat(TimeCPTN, ":");




    Hop this help





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