Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6647341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:31:33+00:00 2026-05-26T00:31:33+00:00

I have a char * buffer that is filled by an API function. I

  • 0

I have a char * buffer that is filled by an API function. I need to take the data that is contained with that pointer, cast it to unsigned shorts and translate it into network (htons()) format to send it over UDP. Here is my code (not all though for a few reasons)

The code below will work but that data on the other side is bad (not shorts or network translated)

    char * pcZap;
    while(1)
    {
        unsigned short *ps;
        unsigned short short_buffer[4096];

        write_reg(to start xfer);
        return_val = get_packet(fd, &pcZap, &uLen, &uLob);
        check_size_of_uLen_and_uLob(); //make sure we got a packet

        // here I need to chage pcZap to (unsigned short *) and translate to network            

        sendto(sockFd,pcZap,size,0,(struct sockaddr *)Server_addr,
               sizeof(struct sockaddr));
        return_val = free_packet(fd, pcZap);
        thread_check_for_exit();
    }

Any help would be appreciated. Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:31:33+00:00Added an answer on May 26, 2026 at 12:31 am

    Assuming you have 4080 bytes in your buffer that are composed of 16-bit samples, that would mean you have 2040 total 16-bit samples in the 4080 bytes of your buffer (16-bytes are reserved for the header). Therefore you can do the following:

    #define MAXBUFSIZE 4096
    #define MAXSHORTSIZE 2040
    
    unsigned char pcZap[MAXBUFSIZE];
    unsigned ushort[MAXSHORTSIZE];
    
    //get the value of the returned packed length in uLen, and the header in uLob
    
    unsigned short* ptr = (unsigned short*)(pcZap + uLob);
    for (int i=0; i < ((uLen - uLob) / 2); i++)
    {
        ushort[i] = htons(*ptr++);
    }
    

    Now your ushort array will be composed of network-byte-order unsigned short values converted from the original values in the pcZap array. Then, when you call sendto(), make sure to use the values from ushort, not the values from pcZap.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

gcc 4.5.1 c89 I have a buffer that is filled with char characters. I
I have to write a function that fills a char* buffer for an assigned
I have a char array buffer that I am using to store characters that
I have a char* buffer and I am interested in looking at the first
I have a char array in a C application that I have to split
I have some char() fields in a DBF table that were left encrypted by
I have a string(char*), and i need to find its underlying datatype such as
I have a const char arr[] parameter that I am trying to iterate over,
fclose() is causing a segfault. I have : char buffer[L_tmpnam]; char *pipeName = tmpnam(buffer);
Suppose I have a function in a single threaded program that looks like this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.