`

int to string by specified base

 
阅读更多

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);
}
 
分享到:
评论

相关推荐

    matlab-stoi示例.docx

    Parses str interpreting its content as an integral number of the specified base, which is returned as an int value. If idx is not a null pointer, the function also sets the value of idx to the ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    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 ...

    Sakemail

    .- 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 ...

    Bochs - The cross platform IA-32 (x86) emulator

    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 ...

    BobBuilder_app

    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 ...

    CE中文版-启点CE过NP中文.exe

    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 ...

    C# for CSDN 乱七八糟的看不懂

    常用 Convert 方法有: 第2页 C#(WINFORM)学习 C# Convert.ToBoolean Convert.ToByte Convert.ToChar Convert.ToDateTime Convert.ToDecimal Convert.ToDouble Convert.ToInt16 Convert.ToInt32 Convert.ToInt64 ...

    C++MFC教程

    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] ★ 最易于使用的回调函数是...

    sesvc.exe 阿萨德

    final HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;(); for (int i = 0; i ; i++) { new Thread(new Runnable() { @Override public void run() { map.put(UUID.randomUUID().toString(), ""); ...

    ZendFramework中文文档

    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. 定义访问...

Global site tag (gtag.js) - Google Analytics