CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: conversion help

  1. #1
    Join Date
    Jun 2002
    Location
    Spain
    Posts
    135

    conversion help

    I have the following piece of code:

    *************

    puerto = getenv("PUERTO");

    my_addr.sin_port = (short int)*puerto;

    ***************


    where getenv is a function that returns a pointer to a char, and my_addr.sin_port is an unsinged short int.
    As you can see, the issue here is the conversion from
    char* to unsigned short int.
    I get no compiler error with the above code, but I'm not sure it'll work in run-time.
    Even if I remove the cast conversion (short int), I get no compiler error, which seems suspicious.

    What is the best way to perform this conversion?

    Thanks a lot.

    -- naradaji

  2. #2
    Join Date
    Jul 2002
    Posts
    2,543
    my_addr.sin_port = atoi(puerto);

  3. #3
    Join Date
    Jun 2002
    Location
    Spain
    Posts
    135
    What the fact that what I need is a short int, not an int?

    Thanks

  4. #4
    Join Date
    Jun 2002
    Location
    Spain
    Posts
    135
    I mean, atoi converts to int, not to short int, right?

  5. #5
    Join Date
    Nov 2001
    Location
    Beyond Juslibol
    Posts
    1,688
    if you are converting from char tounsigned short int:

    In my opinion I don't see any problem if char is >=0. I don't know how would be the conversion if char is <0, probably will return the maximum unsigned short int.

    if you are converting from char to short int I don't see any problem.

    It's not clear... What type is my_addr.sin_port ?

  6. #6
    Join Date
    Jun 2002
    Location
    Spain
    Posts
    135
    it's unsigned short int

  7. #7
    Join Date
    Nov 2001
    Location
    Beyond Juslibol
    Posts
    1,688
    In my opinion the problem is when *puerto<0 because you can not convert signed to unsigned.

  8. #8
    Join Date
    Jun 2002
    Location
    Spain
    Posts
    135
    Okay. *puerto will never be >0, so I guess there's no problem.
    Thanks a whole lot!

  9. #9
    Join Date
    Aug 2001
    Location
    Stockholm, Sweden
    Posts
    1,664
    Sorry, but you're all wrong!!!

    Wrong byte-order is the problem (assuming Intel/AMD PC platform).

    The socket API (winsocket or BSD sockets) uses network-byte order (which happens to be big endian). On Intel (which is little endian) platforms, you must convert values like short:s and int:s. And to be really portable, you should always use the network-to-host and host-to-network conversion routines provided by the socket API.

    The conversion function in this case is host-to-netwrok-short: ntons(). Like this (together with atoi):
    Code:
    portnoStr = getenv("PORTNO");
    my_addr.sin_port = htons(atoi(portnoStr));
    That's it. Good luck.

  10. #10
    Join Date
    Jun 2002
    Location
    Spain
    Posts
    135
    The platform is really AIX.
    Will byte order be a problem, then?

    Thanks.

    -- naradaji

  11. #11
    Join Date
    Aug 2001
    Location
    Stockholm, Sweden
    Posts
    1,664
    Originally posted by naradaji
    The platform is really AIX.
    Will byte order be a problem, then?

    Thanks.

    -- naradaji

    I don't know if AIX big or little endian, but as I said in my previous post: Do always use the socket API conversion routines.

    Do like I showed you in my sample code. That will always be platform independent.

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