Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

String to 2 bytes

String to 2 bytes

String to 2 bytes

I am writing a modified udp server, and i almost have it working the way I want, I am only missing the ability to take a string like "8367" and have 2 bytes 32 175 returned
the string is the port # I obtained from the udp server if that helps.
Here is what I have at the moment


#include <stdio.h>
#include <errno.h>
#include <sys/socket.h>
#include <resolv.h>

#define DEFAULT_PORT    1235

int main(int count, char *strings[])
{    int sd;
    int fport;
    int i;
    int port=DEFAULT_PORT;
    struct sockaddr_in addr;
    char buffer[1024];
    char sbuffer[1024];
    char o1[5], o2[5], o3[5], o4[5];  //set string rep of each octet
    int io1, io2, io3, io4;        //set int value for each octet

    if ( count != 2 )
        printf("usage: %s <port>\n...Using default port (%d).\n", strings[0], port);
        port = atoi(strings[1]);
    sd = socket(PF_INET, SOCK_DGRAM, 0);
    bzero(&addr, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    addr.sin_addr.s_addr = INADDR_ANY;
    if ( bind(sd, (struct sockaddr*)&addr, sizeof(addr)) != 0 )
    while (1)
    {    int bytes, addr_len=sizeof(addr);

        for (i = 0; i < 12; i++)
            sbuffer[i] = 0;

        bytes = recvfrom(sd, buffer, sizeof(buffer), 0, (struct sockaddr*)&addr, &addr_len);
        printf("msg from %s:%d (%d bytes)\n", inet_ntoa(addr.sin_addr),
                        ntohs(addr.sin_port), bytes);

        sscanf(inet_ntoa(addr.sin_addr), "%4[^.].%4[^.].%4[^.].%4[^.]", o1, o2, o3, o4);

        fport = ntohs(addr.sin_port);

        //int of each octet
        io1 = atoi(o1);
        io2 = atoi(o2);
        io3 = atoi(o3);
        io4 = atoi(o4);

        printf("From IP:%s %s %s %s: %d\n", o1, o2, o3, o4, fport);

        //this is what we received in hex
        printf("%2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x\n",
            buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], buffer[5],
            buffer[6], buffer[7], buffer[8], buffer[9], buffer[10], buffer[11]);
        //set send buffer to stuff I want
        for (i = 4; i < 12; i++)
        //this is the ip
        sbuffer[8] = io1;
        sbuffer[9] = io2;
        sbuffer[10] = io3;
        sbuffer[11] = io4;

                //port ???
        sbuffer[12] = fport;
            sbuffer[13] = fport;

        printf("%2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x\n",
            sbuffer[0], sbuffer[1], sbuffer[2], sbuffer[3], sbuffer[4], sbuffer[5],
            sbuffer[6], sbuffer[7], sbuffer[8], sbuffer[9], sbuffer[10], sbuffer[11],
            sbuffer[12], sbuffer[13]);
        sendto(sd, sbuffer, bytes, 0, (struct sockaddr*)&addr, sizeof(addr));

RE: String to 2 bytes

If you need to convert the string "8367" into the unsigned short 8367, you've almost got it already:


port = atoi(strings[1]);
just change the datatype of port from int to unsigned short, then cast the result to unsigned short:


port = (unsigned short)atoi(strings[1]);
Although you should add some error handling to check if atoi() failed to convert the string and if the number is larger than USHRT_MAX...

RE: String to 2 bytes

I'm not quite sure how I get 2 bytes back with that.
I have to send back the bytes to the client.  Will the above give me "8367" = 32 for the upper byte and 175 for the lower byte?

RE: String to 2 bytes

Yes.  A short = 2 bytes (usually) and an int = 4 bytes (usually).

RE: String to 2 bytes

Ok, thanx, now I got it.

That worked, but what I did instead was take the original int value of fport and did my magic.
how I did it was


int uport; //upper order byte - will be no decimal value
int lport; //lower order byte

uport = fport /256; //set up the upper byte
lport = fport - (uport * 256); //set up the lower byte
//now i have the bytes separated to send back

RE: String to 2 bytes

This should work too:


unsigned short port = 8367;
char portBytes[2] = (char*)port;


unsigned short port = 8367;
char LowByte = port | 0xFF;
char HighByte = (port >> 8) | 0xFF;
a union would work too.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close