如何将字符串中的数字转换为汇编中的任何基数? [英] How can I convert a number in a string to any base in assembly?

查看:122
本文介绍了如何将字符串中的数字转换为汇编中的任何基数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将字符串中包含的数字从任何基数转换为任何其他基数?

How can I convert a number contained in a string from any base to any other base?

碱基可以是任意值,即:2、16、10、4、8、9.

Bases can be anything i.e.: 2, 16, 10, 4, 8, 9.

我希望用户输入基本号码.用户将输入输出基准(要转换为的基准).用户将输入他想要转换的数字.

I'm expecting the user to enter the base number. The user will enter the output base (the base to be converted to). The user will enter the number he wants to convert.

预想: 我将输入库和输出库保存在变量中.然后,我将他输入的数字保存在字符串中(因为他可以输入任何类型的数字(十六进制,二进制,以5为底的..).

Pre thoughts: I will save the input base and the output base in variables. Then I'll save the number he enters in a string (because he could enter any kind of number (hex, binary, base-5..).

我只是想弄清楚如何将该字符串转换为数字,以便可以将其转换为输出基数.

I'm just trying to figure out how to convert that string to a number so i can convert it to the output base.

有什么想法吗?

推荐答案

要从字符串转换为整数,您需要查看

To convert from a string to an integer, you'll need to take a look at an ASCII table.

反复遍历字符串中的每个字符,直到打到末尾为止,然后根据字符的含义和范围(从"0"到"9",从"a"到"f","A"到'F',您需要减去范围内底部字符的值并添加任何适当的数量.然后将其添加到累加器值中,您应该设置为go.我想您还需要检查任何暗示该值位于哪个基数的值(例如,我希望十六进制值以"0x"为前缀).

iterate through each character in your string until you hit the end, and based off of what the character is and which range it's in: '0' to '9', 'a' to 'f', 'A' to 'F', you'll need to subtract the value of the bottom character in it's range and add any appropriate amount. then add that to an accumulator value and you should be set to go. i guess you'll also need to check for any values that hint at what base the value is in (for instance, i'd expect hex values to be prefixed by "0x").

因此,例如,如果看到"1",则需要减去"0".如果看到'a',则需要减去'a'并加上0x0a.

So for instance, if you see a '1', you'll need to subtract off '0'. if you see an 'a', you'll need to subtract off 'a' and add 0x0a.

这篇关于如何将字符串中的数字转换为汇编中的任何基数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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