将输入解释为十六进制数字 [英] Interpreting input as hexadecimal numbers

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

问题描述

尝试将输入解释为十六进制时遇到麻烦.例如,如果给定输入40A2,并假设我有一个大小为2 16 的数组,我将如何访问以下示例?

I was having a bit of trouble trying to interpret my input as hexadecimal numbers. For example if given the input of 40A2, and assuming I have an array at size 216, how would I access the following example?

arr[0x40A2] //this is simply accessing the array at binary 0100 0000 1010 0010 
              which is at index 16546

对此我有点麻烦,因为当程序从用户那里接收输入时,数字以ASCII值的形式被解释,并且需要被解释为十六进制的值.

I'm just having a bit of trouble with this because as the program receives input from the user, numbers come in interpreted as ASCII values, and need to be interpreted as hexadecimal values.

推荐答案

您如何阅读输入内容? C ++标准输入流了解 std::hex修饰符:

How do you read the input? C++ standard input streams understand the std::hex modifier:

unsigned int index;
std::cin >> std::hex >> index;

这篇关于将输入解释为十六进制数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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