如何在Visual C ++中将字节数组转换为十六进制字符串? [英] How to convert Byte Array to hex string in visual c++?

查看:89
本文介绍了如何在Visual C ++中将字节数组转换为十六进制字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

方法声明如下:

//some.h
void TDES_Decryption(BYTE *Data, BYTE *Key, BYTE *InitalVector, int Length);

我正在从以下代码中调用此方法:

I am calling this method from the following code:

//some.c
extern "C" __declspec(dllexport) bool _cdecl OnDecryption(LPCTSTR stringKSN, LPCTSTR BDK){
    TDES_Decryption(m_Track1Buffer, m_cryptoKey, init_vector, len);
    return m_Track1Buffer;
}

m_Track1Buffer的数据类型为BYTE m_Track1Buffer[1000]; 现在我想对上述方法进行一些更改,即想返回String in hex而不是Byte.我应该如何将此m_Track1buffer转换为Hex string

Where as data type of m_Track1Buffer is BYTE m_Track1Buffer[1000]; Now i want to make some changes in above method i.e. want to return the String in hex instead of Byte. How should i convert this m_Track1buffer to Hex string

推荐答案

正如您提到的c ++一样,这是一个答案. Iomanip用于将十六进制形式的整数存储到stringstream中.

As you have mentioned c++, here is an answer. Iomanip is used to store ints in hex form into stringstream.

#include <sstream>
#include <iomanip>

std::string hexStr(BYTE *data, int len)
{
     std::stringstream ss;
     ss << std::hex;

     for( int i(0) ; i < len; ++i )
         ss << std::setw(2) << std::setfill('0') << (int)data[i];

     return ss.str();
}

这篇关于如何在Visual C ++中将字节数组转换为十六进制字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆