string_utils.cc (893B)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 // 5 // String manipulation functions used in the RLZ library. 6 7 #include "rlz/lib/string_utils.h" 8 9 namespace rlz_lib { 10 11 bool BytesToString(const unsigned char* data, 12 int data_len, 13 std::string* string) { 14 if (!string) 15 return false; 16 17 string->clear(); 18 if (data_len < 1 || !data) 19 return false; 20 21 static const char kHex[] = "0123456789ABCDEF"; 22 23 // Fix the buffer size to begin with to avoid repeated re-allocation. 24 string->resize(data_len * 2); 25 int index = data_len; 26 while (index--) { 27 string->at(2 * index) = kHex[data[index] >> 4]; // high digit 28 string->at(2 * index + 1) = kHex[data[index] & 0x0F]; // low digit 29 } 30 31 return true; 32 } 33 34 } // namespace rlz_lib