int to string by specified base
convert a int to string, by specified base, support 2 - 16,
code:
#include <stdio.h>
/*
* @author kuchaguangjie
* @date 2012-04-03 22:10
* @mail kuchaguangjie@gmail.com
*/
/**
* convert int to string of specified base,
* @param n
input number
* @param s
output string
* @param base
the base, between [2 - 16]
* @param maxwidth
the possible max width after convertion
* @ return
0 means ok, -1 means has problem
*/
int itob_base(int n, char *s, int base, int maxwidth) {
if(base>16 || base<2) {
printf("base should between 2~16\n");
return -1;
}
int quotients[maxwidth], bc=0, i;
while(n>=base) {
quotients[bc++] = n % base;
n /= base;
}
quotients[bc] = n;
for(i=0;bc>=0;i++,bc--) {
if(quotients[bc]<10) {
s[i] = quotients[bc] + '0';
} else if(quotients[bc]<16) {
s[i] = quotients[bc] + 'A' - 10;
}
}
s[i] = '\0';
return 0;
}
/**
* convert int to string of specified base, the possible max width is 32,
* @param n
input number
* @param s
output string
* @param base
the base, between [2 - 16]
*/
int itob(int n, char *s, int base) {
return itob_base(n, s, base, 32);
}
main() {
char s[33];
int n = 1000, base = 16;
itob(n, s, base);
printf("%s\n",s);
}
分享到:
相关推荐
These rules exist to keep the code base manageable while still allowing coders to use C++ language features productively. Style, also known as readability, is what we call the conventions that ...
.- Reduced the line sleep to 30 (tell me if this value doesn‘t work for you).27/4/981.7.0- Fixed a memory leak, thanks to Don Higgins.- Moved the string esErrorInFormatOfMsg to a property of SakPOP.-...
This specification was developed in response to a perceived need for a standardized programming inter-face to digitizing tablets, three dimensional position sensors, and other pointing devices by a ...
devices (currently only used to set the speed reported by device and to specify an alternative redolog file of USB MSD disk image modes) - hard drive - new disk image mode 'vvfat' - ported the ...
String Keys are UTF8 encoded and limited to 60 bytes if not specified otherwise (maximum is 255 chars). Support for long string Keys with the RaptorDBString class. Duplicate keys are stored as a WAH ...
Debugging: Added an option to chose if you wish to break on unexpected breakpoints, and if CE should break on unexpected breakpoints, or only on specified regions (like AA scripts) Disassembler: The ...
public string this[int index] { get { return items[index]; } set { items[index] = value; } } } ``` #### 15. 事件(Events) 事件实现了发布-订阅模式,允许对象通知其他对象发生的特定事件。 **示例...
- `int("42", base)`: Converts a string to an integer, using the specified base. - **Additional Math Functions:** - `import math`: Standard math functions such as `sin`, `cos`, etc. - `import cmath...
常用 Convert 方法有: 第2页 C#(WINFORM)学习 C# Convert.ToBoolean Convert.ToByte Convert.ToChar Convert.ToDateTime Convert.ToDecimal Convert.ToDouble Convert.ToInt16 Convert.ToInt32 Convert.ToInt64 ...
CRect( int l, int t, int r, int b ); 指明四个坐标 CRect( const RECT& srcRect ); 由RECT结构构造 CRect( LPCRECT lpSrcRect ); 由RECT结构构造 CRect( POINT point, SIZE size ); 有左上角坐标和尺寸构造 CRect...
this library provides general dynamic create way, can create object by multiple ways , even customized way.and base class can be specified by user. ◆ [callbackWorker.hpp] ★ 最易于使用的回调函数是...
final HashMap<String, String> map = new HashMap<String, String>(); for (int i = 0; i ; i++) { new Thread(new Runnable() { @Override public void run() { map.put(UUID.randomUUID().toString(), ""); ...
1. Introduction to Zend Framework 1.1. 概述 1.2. 安装 2. Zend_Acl 2.1. 简介 2.1.1. 关于资源(Resource) 2.1.2. 关于角色(Role) 2.1.3. 创建访问控制列表(ACL) 2.1.4. 注册角色(Role) 2.1.5. 定义访问...
由于MySQL对字符串类型的默认映射可能导致键长度超出限制,所以在定义`UserName`和`PassWord`时,需要使用`StringLength`和`MaxLength`数据注解来限制其长度,避免出现“Specified key was too long; max key length...