十六进制转换成十进制[K&安培; R运动]

编程入门 行业动态 更新时间:2024-10-18 16:42:09
本文介绍了十六进制转换成十进制[K&安培; R运动]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我学习C和我想不通的K&放大器之一;ř练习,上市:

I'm learning C and I can't figure out one of the K&R exercises, the listing:

练习2-3,编写函数   htoi(S),它转换的字符串  十六进制数字(包括  可选的 0X 或 0X )转换成等值  整数值。允许的数字  是 0 到 9 , A 到˚F和 A   通过˚F。

Exercise 2-3, Write the function htoi(s), which converts a string of hexadecimal digits (including an optional 0x or 0X) into its equivalent integer value. The allowable digits are 0 through 9, a through f and A through F.

我想我需要在这里做一些递归,我只是想我不知道了很多关于编号的类型,他们的各种转换方法等。

I suppose I need to do some recursion here, I just guess that I don't know a lot about the numbering types, and their various conversion methodologies, etc.

可能有人给我如何更好地理解一些指点,我不是在找人牵我的手,而是直接向我正确理解的一种手段,所以我可以在最优雅的形式尽可能写,而不是与的printf(%X,技能);

Could someone give me some pointers on how best to understand it, I'm not looking for someone to hold my hand, but instead direct me to a means of proper understanding so I can write this in the most elegant form as possible, and not with printf("%x", skill);

推荐答案

递归是没有必要的。您只需向后循环在字符串(即在单位列开始),单数字的转换时间它的基数位置乘数总结。这是伪code和不处理可选0X preFIX(并且不检查溢出的可能性):

Recursion is not necessary. You simply need to loop backwards over the string (i.e. starting in the units column), summing the single digit conversion times it's radix position multiplier. This is pseudocode and does not handle the optional 0x prefix (and is not checking for possibility of overflow):

long total = 0; long multiplier = 1; for (int i = string.length - 1; i >= 0 i--) { digit = ConvertSingleHexDigittoInt(string[i]); total += digit * multiplier; multiplier *= 16; }

我已经离开了易于实现ConvertSingleHexDigittoInt()给你:)

I've left the easy implementation of ConvertSingleHexDigittoInt() to you :)

更多推荐

十六进制转换成十进制[K&安培; R运动]

本文发布于:2023-10-24 20:05:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524874.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:安培   转换成   十六进制   十进制

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!