Skip to content

Instantly share code, notes, and snippets.

@indefinit
Last active May 5, 2017 21:57
Show Gist options
  • Save indefinit/1fa9dda78653c8a57a24 to your computer and use it in GitHub Desktop.
Save indefinit/1fa9dda78653c8a57a24 to your computer and use it in GitHub Desktop.
Cinder: buffer data type conversions and processing
string bufferToString( const cinder::Buffer& buffer )
{
string s( static_cast<const char*>( buffer.getData() ) );
if ( s.length() > buffer.getDataSize() ) {
s.resize( buffer.getDataSize() );
}
return s;
}
uint8_t* bufferToUint8(const cinder::Buffer& buffer)
{
uint8_t* pBuffer = reinterpret_cast<uint8_t*>( mBuffer.getData() );
return pBuffer;
}
uint32_t* bufferToUint32(const cinder::Buffer& buffer)
{
uint32_t* pBuffer = reinterpret_cast<uint32_t*>( mBuffer.getData() );
return pBuffer;
}
cinder::Buffer stringToBuffer( const string& value )
{
cinder::Buffer buf( value.size() );
buf.copyFrom( (char*)&value[ 0 ], value.size() );
return buf;
}
void processBuffer(const cinder::Buffer& buffer)
{
size_t numElements = mBuffer.getDataSize() / sizeof(uint8_t);
uint8_t* pBuffer = reinterpret_cast<uint8_t*>( mBuffer.getData() );
for(size_t i=0; i<numElements; ++i) {
uint8_t element = pBuffer[i]; // do something with each element
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment