C++ Code Examples for convert bytes

8 C++ code examples are found related to "convert bytes". These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Project: softwarecontainer   Author: Pelagicore   File: cgroupsparser.cpp    License: GNU Lesser General Public License v2.1 7 votes vote down vote up
std::string CGroupsParser::convertToBytes(const std::string settingValue, const int multiply)
{
    long long limit_in_bytes = std::strtoll(settingValue.c_str(), NULL, 10);
    if (limit_in_bytes ==  std::numeric_limits<long long>::max() ||
        limit_in_bytes ==  std::numeric_limits<long long>::min()) {
        if (errno == ERANGE) {
            std::string errMessage = settingValue + " is out of range";
            log_error() << errMessage;
            throw LimitRangeError(errMessage);
        }
    }

    if (limit_in_bytes > llrint(std::numeric_limits<long long>::max() / multiply)) {
        std::string errMessage = settingValue + " * " + std::to_string(multiply) + " is out of range";
        log_error() << errMessage;
        throw LimitRangeError(errMessage);
    }

    limit_in_bytes *= multiply;
    return std::to_string(limit_in_bytes);
} 
Example 2
Project: MEC   Author: TheTechnobear   File: juce_AudioDataConverters.cpp    License: GNU General Public License v3.0 7 votes vote down vote up
void AudioDataConverters::convertFloatToInt16LE (const float* source, void* dest, int numSamples, const int destBytesPerSample)
{
    const double maxVal = (double) 0x7fff;
    char* intData = static_cast<char*> (dest);

    if (dest != (void*) source || destBytesPerSample <= 4)
    {
        for (int i = 0; i < numSamples; ++i)
        {
            *(uint16*) intData = ByteOrder::swapIfBigEndian ((uint16) (short) roundToInt (jlimit (-maxVal, maxVal, maxVal * source[i])));
            intData += destBytesPerSample;
        }
    }
    else
    { 
Example 3
Project: xvc   Author: divideon   File: resample.cc    License: GNU Lesser General Public License v2.1 7 votes vote down vote up
void Resampler::ConvertFrom(const PictureFormat &src_format,
                            const uint8_t *src_bytes, YuvPicture *out_pic) {
  const int num_components = util::GetNumComponents(out_pic->GetChromaFormat());
  const YuvComponent luma = YuvComponent::kY;
  assert(out_pic->GetChromaFormat() == src_format.chroma_format);
  if (out_pic->GetWidth(luma) == 0 ||
      out_pic->GetHeight(luma) == 0) {
    return;
  }
  if (out_pic->GetWidth(luma) == src_format.width &&
      out_pic->GetHeight(luma) == src_format.height) {
    for (int c = 0; c < num_components; c++) {
      const YuvComponent comp = static_cast<YuvComponent>(c);
      const ptrdiff_t src_stride = (src_format.bitdepth == 8 ? 1 : 2) *
        util::ScaleSizeX(src_format.width, src_format.chroma_format, comp);
      src_bytes = CopyFromBytesFast(comp, src_bytes, src_stride,
                                    src_format.bitdepth, out_pic);
    }
  } else if 
Example 4
Project: WebRTC-APM-for-Android   Author: dengzikun   File: file_utils.cc    License: Apache License 2.0 6 votes vote down vote up
int ConvertDoubleToByteArray(double value, uint8_t out_bytes[8]) {
  if (!out_bytes) {
    return -1;
  }

  uint64_t binary_value = bit_cast<uint64_t>(value);
  for (size_t i = 0; i < 8; ++i) {
    out_bytes[i] = binary_value;
    binary_value >>= 8;
  }

  return 0;
} 
Example 5
Project: czys   Author: kxtry   File: aiffproperties.cpp    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
static double ConvertFromIeeeExtended(const TagLib::uchar *bytes)
{
  double f;
  int expon;
  unsigned long hiMant, loMant;

  expon  = ((bytes[0] & 0x7F) << 8) | (bytes[1] & 0xFF);

  hiMant = ((unsigned long)(bytes[2] & 0xFF) << 24) |
           ((unsigned long)(bytes[3] & 0xFF) << 16) |
           ((unsigned long)(bytes[4] & 0xFF) << 8)  |
           ((unsigned long)(bytes[5] & 0xFF));

  loMant = ((unsigned long)(bytes[6] & 0xFF) << 24) |
           ((unsigned long)(bytes[7] & 0xFF) << 16) |
           ((unsigned long)(bytes[8] & 0xFF) << 8)  |
           ((unsigned long)(bytes[9] & 0xFF));

  if (expon == 0 && hiMant == 0 && loMant == 0)
    f = 0;
  else {
    if 
Example 6
Project: WebRTC-APM-for-Android   Author: dengzikun   File: file_utils.cc    License: Apache License 2.0 5 votes vote down vote up
int ConvertFloatToByteArray(float value, uint8_t out_bytes[4]) {
  if (!out_bytes) {
    return -1;
  }

  uint32_t binary_value = bit_cast<uint32_t>(value);
  for (size_t i = 0; i < 4; ++i) {
    out_bytes[i] = binary_value;
    binary_value >>= 8;
  }

  return 0;
} 
Example 7
Project: DeepSpeech   Author: mozilla   File: kenlm_benchmark_main.cc    License: Mozilla Public License 2.0 5 votes vote down vote up
void ConvertToBytes(const Model &model, int fd_in) {
  util::FilePiece in(fd_in);
  util::FileStream out(1);
  Width width;
  StringPiece word;
  const Width end_sentence = (Width)model.GetVocabulary().EndSentence();
  while (true) {
    while (in.ReadWordSameLine(word)) {
      width = (Width)model.GetVocabulary().Index(word);
      out.write(&width, sizeof(Width));
    }
    if (!in.ReadLineOrEOF(word)) break;
    out.write(&end_sentence, sizeof(Width));
  }
} 
Example 8
Project: oboe   Author: google   File: Utilities.cpp    License: Apache License 2.0 5 votes vote down vote up
int32_t convertFormatToSizeInBytes(AudioFormat format) {
    int32_t size = 0;
    switch (format) {
        case AudioFormat::I16:
            size = sizeof(int16_t);
            break;
        case AudioFormat::Float:
            size = sizeof(float);
            break;
        default:
            break;
    }
    return size;
}