1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20/*!
21 *
22 * \file support/hexdump.cc
23 * \brief Print hex representation of BLOBs.
24 */
25
26#include "hexdump.h"
27
28namespace tvm {
29namespace support {
30
31void HexDump(const std::string& s, std::ostream& os) {
32 os << std::hex << std::setfill('0') << std::right;
33
34 int addr_width = 4;
35 for (size_t addr_bytes = s.size() >> 16; addr_bytes != 0; addr_bytes >>= 4) {
36 addr_width++;
37 }
38
39 for (size_t cursor = 0; cursor < s.size(); cursor += 0x10) {
40 os << std::setw(addr_width) << cursor;
41 size_t row_end = cursor + 0x10;
42 if (row_end > s.size()) {
43 row_end = s.size();
44 }
45
46 os << " ";
47 for (size_t j = cursor; j < row_end; j++) {
48 os << " " << std::setw(2) << (unsigned int)(s[j] & 0xff);
49 }
50
51 for (size_t j = row_end; j < cursor + 0x10; j++) {
52 os << " ";
53 }
54
55 os << std::setw(1) << " ";
56 for (size_t j = cursor; j < row_end; j++) {
57 os << (isprint(s[j]) ? s[j] : '.');
58 }
59 os << std::endl;
60 }
61}
62
63} // namespace support
64} // namespace tvm
65