C ++将字符串转换为十六进制 [英] c++ convert string to hex

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

问题描述

可能重复:
C ++将十六进制字符串转换为有符号整数

Possible Duplicate:
C++ convert hex string to signed integer

我已经在Google上进行了搜索,但没有找到任何帮助. 所以这是我的问题: 我的所有字符串都包含十六进制代码,例如:string s1 ="5f0066" 我想将此字符串转换为十六进制. 我需要将字符串与我在二进制文件中读取的十六进制代码进行比较.

I allready searched on google but didn't find any help. So heres my problem: I have strings that allready contains hex code e.g.: string s1 = "5f0066" and i want to convert this string to hex. I need to compare the string with hex code i've read in a binary file.

(只需要基本概念如何转换字符串) 5楼 最好的问候和感谢.

(Just need the basic idea how to convert the string) 5f Best regards and thanks.

以无符号字符格式获取二进制文件信息.所以它的0x5f ... 想要使用相同格式的字符串

Got the binary file info in the format of unsigned char. SO its 0x5f... WOuld like to have teh string in the same format

推荐答案

使用 std:stoi 为(仅在C ++ 11中):

Use std:stoi as (in C++11 only):

std::string s = "5f0066";
int num = std::stoi(s, 0, 16);

在线演示

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

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