Like Tree1Likes
  • 1 Post By laserlight

[C]Convert Char* to Unsigned Long

This is a discussion on [C]Convert Char* to Unsigned Long within the C Programming forums, part of the General Programming Boards category; Hello! How can I convert char* to unsigned long? When I try it with Code: atol() it gives wrong the ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    45

    [C]Convert Char* to Unsigned Long

    Hello!

    How can I convert char* to unsigned long?

    When I try it with
    Code:
    atol()
    it gives wrong the conversion.

    And
    Code:
    (unsigned)atol()
    gives the wrong conversion also.


    Code:
    char *test =  "3232235876";
    unsigned long numeric_ip =  atol(test) ;
    PS: It needs to be exactly the same value.

    Thanks a lot

    Greetz!
    Last edited by MaSSaSLaYeR; 11-17-2011 at 02:19 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,727
    Check out strtoul instead. It allows for error checking too.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    45
    Quote Originally Posted by laserlight View Post
    Check out strtoul instead. It allows for error checking too.
    Does that work under windows 2?

    I taught it only worked with Linux and isn't it C++?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,727
    Quote Originally Posted by MaSSaSLaYeR
    Does that work under windows 2?
    I do not know. Why are you using such an old version of Windows, anyway?

    Quote Originally Posted by MaSSaSLaYeR
    I taught it only worked with Linux and isn't it C++?
    I don't have my copy of C99 with me right now, but I would not teach that it only works with Linux since I believe that it is part of the C standard library.
    Tclausex likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    45
    Quote Originally Posted by laserlight View Post
    I do not know. Why are you using such an old version of Windows, anyway?


    I don't have my copy of C99 with me right now, but I would not teach that it only works with Linux since I believe that it is part of the C standard library.
    This worked thank you
    Code:
    (unsigned) strtoul(test,NULL,10);

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,727
    You're welcome, though I don't think you want the cast to unsigned as you are actually casting from unsigned long to unsigned int, only to assign to unsigned long.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Half your problem is the number itself... 3,232,235,876 is too big for a 32 bit integer.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    OK, but which half is it?

    A 4 byte unsigned int would be OK, but not a signed int:
    UINT_MAX: 4 294 967 295 * print with %u *

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,727
    Quote Originally Posted by CommonTater
    Half your problem is the number itself... 3,232,235,876 is too big for a 32 bit integer.
    It is in range of unsigned long though, which I think is the idea, otherwise conversion to long first would work.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Nov 2011
    Posts
    45
    I never had these kind of problems in VB.net =(

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by MaSSaSLaYeR View Post
    I never had these kind of problems in VB.net =(
    Because you were being babysat by .NET which hides considerable detail from a programmer. Now you are seeing all the "behind the scenes" stuff. It's not a problem, it's merely a higher level of exposure to the real work being done.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 10-06-2009, 09:37 AM
  2. Converting unsigned long array to unsigned char array
    By delvec28 in forum C Programming
    Replies: 2
    Last Post: 09-07-2009, 08:53 PM
  3. problem with char * and unsigned long
    By Mouse_103 in forum C++ Programming
    Replies: 4
    Last Post: 04-09-2006, 07:07 PM
  4. cannot convert from HBITMAP__* to unsigned long
    By Leeman_s in forum Windows Programming
    Replies: 1
    Last Post: 01-05-2003, 06:49 PM
  5. How do I insert a char in an unsigned long* v?
    By Robert in forum C++ Programming
    Replies: 17
    Last Post: 07-02-2002, 04:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21