c++sdlwavuint8tuint

How to copy uint8* to vector<Uint8> correctly


I am trying to make a vector of Uint8 from Uint8*, but for reason some of the values are not same. Here is my code.

std::vector<Uint8> wav_vector = {}; 
Uint8* wav_buffer_;   
for (unsigned int i = 0; i < wav_length_; i++) {
  wav_vector.push_back(wav_buffer_[i]);
}

Then later I am trying to verify the values:

for (unsigned int i = 0; i < wav_length_; i++){
    if (wav_buffer_[i]!=wav_vector[i]){
      cout<<i<<endl;
      printf("Orignal Buffer %u\n", wav_buffer_[i]);
      printf("Vector Buffer %u\n", wav_vector[i]);

    }
  }

These are my sample outputs:

Index: 0
Orignal Buffer 208
Vector Buffer 40

Index: 1
Orignal Buffer 72
Vector Buffer 3

Index: 2
Orignal Buffer 111
Vector Buffer 183

Index: 3
Orignal Buffer 1
Vector Buffer 97

Index: 4
Orignal Buffer 0
Vector Buffer 79

Index: 5
Orignal Buffer 0
Vector Buffer 127

Index: 8
Orignal Buffer 120
Vector Buffer 40

Index: 9
Orignal Buffer 251
Vector Buffer 3

Index: 10
Orignal Buffer 182
Vector Buffer 183

Index: 16
Orignal Buffer 0
Vector Buffer 176

Index: 17
Orignal Buffer 0
Vector Buffer 200

Index: 18
Orignal Buffer 0
Vector Buffer 109

Index: 19
Orignal Buffer 0
Vector Buffer 1

Index: 24
Orignal Buffer 0
Vector Buffer 176

Index: 25
Orignal Buffer 0
Vector Buffer 200

Index: 26
Orignal Buffer 0
Vector Buffer 109

Index: 27
Orignal Buffer 0
Vector Buffer 1

Index: 32768
Orignal Buffer 16
Vector Buffer 120

Any help would be really appreciated.

Note: I tried this and it works. But I want to do using for loop as I have some other audio formats which will require bit shifting.

std::vector<Uint8> wav_vector(&wav_buffer_[0], &wav_buffer_[wav_length_]);

Solution

  • Looks like there is a constructor for std::vector that can copy from an iterator (or pointer).

    You could do something like:

    std::vector<uint8_t> my_vector(&wave_buffer[0], &wave_buffer[N]);