/*
 * Copyright (c) 2026 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#ifndef INTERNAL_HEX_UTIL_H
#define INTERNAL_HEX_UTIL_H

#include <cstdint>
#include <iomanip>
#include <sstream>
#include <string>
#include <string_view>
#include <vector>

namespace dev {

// 字节序列转 hex 文本,形态对齐 Lua 的 utils.to_hex:无 "0x" 前缀、大写、字节间无分隔符。
// 器件/总线日志统一走这里,避免各模块自写格式化导致同一字段在两侧(C++/Lua)形态漂移。
inline std::string to_hex(std::string_view bytes)
{
    if (bytes.empty()) {
        return {};
    }

    std::ostringstream oss;
    oss << std::hex << std::uppercase << std::setfill('0');
    for (size_t i = 0; i < bytes.size(); ++i) {
        oss << std::setw(2) << static_cast<unsigned>(static_cast<unsigned char>(bytes[i]));
    }
    return oss.str();
}

inline std::string to_hex(const std::vector<uint8_t>& bytes)
{
    if (bytes.empty()) {
        return {};
    }
    return to_hex(std::string_view(reinterpret_cast<const char*>(bytes.data()), bytes.size()));
}

} // namespace dev

#endif // INTERNAL_HEX_UTIL_H