Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;小字符到十六进制函数?_C++_Hex_Md5_Valueconverter - Fatal编程技术网

C++ C++;小字符到十六进制函数?

C++ C++;小字符到十六进制函数?,c++,hex,md5,valueconverter,C++,Hex,Md5,Valueconverter,我试图找到一种非常轻量级的方法来对char或md5one进行十六进制,下面有两个例子 char example[0x34]; char example_final[0x200]; sprintf(example, "%s", "4E964DCA5996E48827F62A4B7CCDF045"); 下面是使用两个PHP函数的示例 MD5: 十六进制: 这是最轻量级的方法。我想用一个较大的字符数组来表示它的十六进制值,而不是对每个字符进行循环,但不确定。bin2hex的一个示例如下所示: #in

我试图找到一种非常轻量级的方法来对char或md5one进行十六进制,下面有两个例子

char example[0x34];
char example_final[0x200];
sprintf(example, "%s", "4E964DCA5996E48827F62A4B7CCDF045");
下面是使用两个PHP函数的示例

MD5:

十六进制:


这是最轻量级的方法。我想用一个较大的字符数组来表示它的十六进制值,而不是对每个字符进行循环,但不确定。
bin2hex
的一个示例如下所示:

#include <string>
#include <iostream>


std::string bin2hex(const std::string& input)
{
    std::string res;
    const char hex[] = "0123456789ABCDEF";
    for(auto sc : input)
    {
        unsigned char c = static_cast<unsigned char>(sc);
        res += hex[c >> 4];
        res += hex[c & 0xf];
    }

    return res;
}


int main()
{
    std::string example = "A string";

    std::cout << "bin2hex of " << example << " gives " << bin2hex(example) << std::endl;
}
#包括
#包括
标准::字符串bin2hex(常量标准::字符串和输入)
{
std::string res;
常量字符十六进制[]=“0123456789ABCDEF”;
用于(自动sc:输入)
{
无符号字符c=静态_转换(sc);
res+=hex[c>>4];
res+=hex[c&0xf];
}
返回res;
}
int main()
{
std::string example=“一个字符串”;

std::coutbin2hex的示例如下所示:

#include <string>
#include <iostream>


std::string bin2hex(const std::string& input)
{
    std::string res;
    const char hex[] = "0123456789ABCDEF";
    for(auto sc : input)
    {
        unsigned char c = static_cast<unsigned char>(sc);
        res += hex[c >> 4];
        res += hex[c & 0xf];
    }

    return res;
}


int main()
{
    std::string example = "A string";

    std::cout << "bin2hex of " << example << " gives " << bin2hex(example) << std::endl;
}
#包括
#包括
标准::字符串bin2hex(常量标准::字符串和输入)
{
std::string res;
常量字符十六进制[]=“0123456789ABCDEF”;
用于(自动sc:输入)
{
无符号字符c=静态_转换(sc);
res+=hex[c>>4];
res+=hex[c&0xf];
}
返回res;
}
int main()
{
std::string example=“一个字符串”;

std::cout一个
char
数组的“十六进制”值不会很大:它将有256个条目。当您这样做时,请注意
char
可能会被签名,您最好将数组索引为
static\u cast(c)
。顺便问一下,您尝试对功能进行编码的地方在哪里…?不,因为我没有编写该数组,lol。
char
数组的“十六进制”值不会太大:它将有256个条目。当您这样做时,请注意
char
可能会被签名,您最好将数组索引为
static\u cast(c)
。顺便问一下,你想把功能编码到哪里去…?不,因为我没有写那个数组,哈哈。到底“for(auto-sc:input)”是什么意思?我对标准cOut也一无所知,尽量避免使用字符串,因为这是一个xbox360应用程序。
for(auto-sc:input)
是一种说法“迭代输入,给我(inti=0;i值”你也把这个标记为C++,所以我假设使用<代码> STD::String 很好——毕竟它是在1998以来的第一个标准中的C++标准,一般来说,它比你自己的字符串处理效率低。但是如果<代码> RES/COD>是一个(通过?)
char数组
,你可以在循环之前做一些类似于
inti=0;
的事情,
res[i*2]=hex[c>>4];res[i*2+1]=hex[c&15];
然后在循环之后做
res[i*2]=0;
。究竟“是为了什么(auto sc:input)“我也不了解标准cOut,尽量避免使用字符串,因为这是一个xbox360应用程序。
for(auto-sc:input)
是一种表示“迭代输入,给我sc中的值”的方式——
for(int I=0;I STD::String 很好——毕竟它是在1998以来的第一个标准中的C++标准,一般来说,它比你自己的字符串处理效率低。但是如果<代码> RES/COD>是一个(通过?)
char数组
,在循环之前执行类似于
inti=0;
的操作,
res[i*2]=hex[c>>4];res[i*2+1]=hex[c&15];
然后在循环之后执行
res[i*2]=0;
#include <string>
#include <iostream>


std::string bin2hex(const std::string& input)
{
    std::string res;
    const char hex[] = "0123456789ABCDEF";
    for(auto sc : input)
    {
        unsigned char c = static_cast<unsigned char>(sc);
        res += hex[c >> 4];
        res += hex[c & 0xf];
    }

    return res;
}


int main()
{
    std::string example = "A string";

    std::cout << "bin2hex of " << example << " gives " << bin2hex(example) << std::endl;
}