[译]二进制,八进制,十进制和十六进制数系统之间的转换
By robot-v1.0
本文链接 https://www.kyfws.com/applications/conversion-between-binary-octal-decimal-hexadecima-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 3 分钟阅读 - 1414 个词 阅读量 0[译]二进制,八进制,十进制和十六进制数系统之间的转换
原文地址:https://www.codeproject.com/Articles/19050/Conversion-between-Binary-Octal-Decimal-Hexadecima
原文作者:asadbd
译文由本站 robot-v1.0 翻译
前言
Conversion between Binary, Octal, Decimal, & Hexadecimal number systems. C code for download.
二进制,八进制,十进制和十六进制数字系统之间的转换.下载的C代码.
介绍(Introduction)
您是否曾经需要在各种数字系统之间进行转换. C语言中没有库(内置)函数用于以二进制形式显示数字或将八进制数解析(读取)为等效的整数.在本文中,我将介绍为此目的而编写的一些函数.(Have you ever needed to convert between various number systems. There is no library (built-in) function in C for displaying a number in binary or parsing (reading) an octal number into it’s integer equivalent. In this article I am going to introduce some functions I have written for use with this purpose.)
背景(Background)
很多时候,我需要以二进制形式显示数字或将二进制字符串解析为等效字符串.同样对于其他数字系统,我发现没有标准的通用方法.因此,我决定写下自己的代码.这些实际上很简单.但是,当您不想浪费时间仅写下用于以二进制(或其他格式)显示数字的代码时,肯定可以节省时间.(Many time I needed to show a number in binary or parse a binary string into it’s equivalent string. Also for other number system I found no standard generalized way. So, I decided to write down my own codes. These are simple in fact. But can surely save time when you don’t want to waste time in just writing down the code for displaying a number in binary (or in other formats).)
使用代码(Using the code)
我已经编写了8个函数,用于从所有数字系统表示形式解析和格式化为其他形式. 4用于将4个数字系统字符串表示形式解析为它们的整数等效形式,4用于将字符串格式化为整数等效形式.任何组合都可以用于在一个数字系统的字符串表示形式到另一个数字系统之间的转换.(I have written 8 functions for parsing and formatting from and into all the number system representations into others. 4 for parsing 4 number system string representations into their integer equivalents and 4 for formatting strings from integer equivalents. Any combinations can be used for conversion between string representation of one number system to another.)
解析功能是:(The parsing functions are:-)
int parseBin(char* bin);
int parseOct(char* oct);
int parseDec(char* dec);
int parseHex(char* hex);
格式化功能是:(The formatting functions are:-)
char* formatBin(int bin);
char* formatOct(int oct);
char* formatDec(int dec);
char* formatHex(int hex);
我在这里详细讨论其中两个.(I am discussing two of them here in detail.)
int parseBin(char* bin)
将二进制字符串bin解析为等效的整数.要将二进制数字字符串" 1001"解析为等效的整数,(parses binary string bin into it’s integer equivalent. To parse the binary number string “1001” into it’s equivalent integer-)
printf("%d\n", parseBin("1001");
char* formatHex(int hex)
将整数十六进制格式化为十六进制字符串表示形式.要将数字255格式化为十六进制字符串表示形式,(formats integer hex into it’s hexadecimal string representation. To fromat the number 255 into it’s hexadecimal string representation-)
printf("%s\n", formatHex(255));
函数的任何组合都可以用于在各种数字系统的字符串表示形式之间进行可能的转换.(Any combination of the functions can be used for possible convertions between string representation of various number systems.)
printf(formatBin(parseHex("3f8"));
formatHex()
以大写形式输出.为了使输出为小写,还有另一个功能.(gives output in uppercase. To have the output in lower case there is another function;) formatHexL()
.(.)
兴趣点(Points of Interest)
我制作了一个可以在命令行中任何数字系统之间转换的应用程序.源包含在zip中.该应用程序名为conv_type.(I have made an application that can convert between any number system in the command line. The source is included in the zip. The application is named conv_type.)
例:(Example:)conv_type 0 3 1111(conv_type 0 3 1111)
输出:FF(Output: FF)
讨论:(Discussion:)conv_type input_system output_system编号(conv_type input_system output_system number)
0 =二进制(0 = binary)
1 =八进制(1 = octal)
2 =小数(2 = decimal)
3 =十六进制(3 = hexadecimal)
历史(History)
本文是第一版.(This article is in it’s first edition.)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
C C++ Windows Dev 新闻 翻译