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 8990503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:30:51+00:00 2026-06-15T22:30:51+00:00

I have char pAudioBuffer buffer which i got from function ffmpeg: int len =

  • 0

I have char pAudioBuffer buffer which i got from function ffmpeg:

int len = avcodec_decode_audio3(av_codec_context,
            (int16_t *) pAudioBuffer, &out_size, &packet);

I know that audio format is two bytes per sample, i need to convert every two bytes to short value, i have tried to use code snippet below, but i often got zero instead of short value:

int shortBufIndex = 0;
for (int i = 0; i < (out_size); i += 2) {
    char b1 = pAudioBuffer[i];
    char b2 = pAudioBuffer[i + 1];
    short sample = atoi(&b1) + atoi(&b2);
    shortBuffer[shortBufIndex] = sample;
    shortBufIndex++;
    LOGI("BUFFER_ITEM='%d'", sample);
}

What i’m doing wrong, how to convert every two bytes in char buffer to short and and back.

UPDATE:

system’s byte order is LITTLE_ENDIAN i have test it like this: Endianness of Android NDK

How can i convert every two bytes in buffer to sample of short type and back. Please can you provide any code sample.

UPDATE

I have tried to access to short as pairs, here is my fixed code, but it not work, i don’t hear any sound:

    int shortBufIndex = 0;
    for (int i = 0; i < (out_size); i += 2) {
        char * byte = (char *) pAudioBuffer[i];
        short * sample = byte;
        shortBuffer[shortBufIndex] = sample;
}

What i’m doing wrong?
I need conversion like this: byte array to short array and back again in java but in c.

  • 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-06-15T22:30:52+00:00Added an answer on June 15, 2026 at 10:30 pm

    The use of atoi() with the address of the variables is entirely mistaken and incorrect. That is for converting the ASCII representation of a value into a number. In your case, you don’t really have a buffer of “characters” but rather a buffer of byte values, which under the history of C is commonly implemented with the char type rather than something more recent and definitive such as int8_t.

    To convert between a character buffer and a short buffer, you first need to answer the critical question if the encoding of the character buffer is in the system’s native endian order, or opposite to it.

    If it is in native endian order, then in a language such as C all you have to do is cast the pointer type to a short, and access the data as pairs of bytes.

    However, if it is in opposite order, you will have to access the two bytes and combine them (shift + add) in opposite order from usual. There may be macros for doing this which you can use, though doing it by hand has the advantage of making the functionality fully evident.

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

Sidebar

Related Questions

I have char* data; which has a buffer dumped into it. I want to
I have a char data[len] populated from unzipped data that is read off of
I have a char * buffer that is filled by an API function. I
I have a function setPosition declared like: void setPosition(char ***grid, int a, int b)
fclose() is causing a segfault. I have : char buffer[L_tmpnam]; char *pipeName = tmpnam(buffer);
I have a char pointer which would be used to store a string. It
I have a char, for example 'A'. How I can get KeyValue after which
I have a char* buffer and I am interested in looking at the first
I have a char array which contains null characters at random locations. I tried
I have a char* name which is a string representation of the short I

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.