xref: /aosp_15_r20/external/gsc-utils/tpm_generated/hex.cc (revision 4f2df630800bdcf1d4f0decf95d8a1cb87344f5f)
1 // Copyright 2024 The ChromiumOS Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "hex.h"
6 
7 namespace trunks {
8 
HexEncode(const char * data,std::size_t len)9 std::string HexEncode(const char* data, std::size_t len) {
10   static constexpr char kHexChars[] = {'0', '1', '2', '3', '4', '5', '6', '7',
11                                        '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
12   std::string ret;
13   ret.reserve(len * 2);
14   for (int i = 0; i < len; i++) {
15     ret.push_back(kHexChars[data[i] >> 4]);
16     ret.push_back(kHexChars[data[i] & 0xf]);
17   }
18   return ret;
19 }
20 
21 }  // namespace trunks
22