C語言中將數字轉為二進位第的函數是什麼丫
如 8 ---> 1000
謝謝
--
[1;5;31m# [0;1;36m^_^ [5;31m# [0;1;33m來源: [4;32m台灣科技大學銀河之旅 BBS [0;1;37m(140.118.9.47) [33m來自: [4;35m140.121.135.214 [0m
我是沒見過有二進位的轉換函數拉
但是你可以利用長除法的方式將數字表示成二進位阿
將欲轉換的數 除2 的餘數為第一位數 商為下次的被除數
一直除下去就可解出來拉
ex:18-->10010
18/2=9....0--->00000
(9)/2=4...1--->00010
(4)/2=2...0--->00000
(2)/2=1...0--->00000
(1)/2=0...1--->10000
所以合起來就是--->10010
用for會很簡單的拉
--
◎ [1;31m龍 [32m貓 [33m資 [34m訊 [35m天 [36m地 [0m( [1mbbs.mgt.ncu.edu.tw [0m)
◎[ [1;33;46moisi [0m]From: 140.115.226.106
這是我寫的啦......很差....新手
#include<stdio.h>
main()
{
int input=5; //要轉二進位之數值
int i=0;
int result[10]; //二進位值
int temp;
while ( input!=1)
{temp=input/2;
result[i]=input%2;
input=temp;
i++;}
result[i]=1;
for (int k=i;k>=0;k--)
printf("%d",result[k]);
}
我覺得有很多缺點...
一 result因為並不知道input可能會用到幾個位元,所以必須設一個很大的值
二 8 --> 1000 但我得到的result其實是相反的,result=0001
三 若有二個連續的數值,"8","4",我希望能得到1000 100 且照順序,不知道要如何丫
希望大家提點新手.....^^y
--
[1;33;44m [m
[1;33;44m [1;33;41m全像光學實驗室 Master Chang [1;33;44m [m
[1;33;44m [1;33;41m研究員兼工友兼老闆小跟班/_\ [1;33;44m [m
[1;33;44m [m
--
[1;32m※ Origin: [33m碩誠 Linux 資訊站 [37m<bbs.sayya.org> [m
[1;31m◆ From: [36m140.132.179.48 [m
void main( void )
{
char buffer[20];
int i;
printf("input a integer:");
scanf("%d",&i);
/* 轉成2進位,然後存到buffer陣列 */
_itoa( i, buffer, 2 );
printf( "String of integer %d (radix 2): %s\n", i, buffer );
}
################################################################
output:
input a integer:4356
String of integer 4356 (radix 2): 1000100000100
Press any key to continue
--
[0m [1;33m※ Origin: [36mYahoo!奇摩 大摩域 [37m<telnet://bbs.kimo.com.tw> [m
[1;35m◆ From: [1;32m61.220.133.210 [m
char *_itoa( int value, char *string, int radix );
char *_i64toa( __int64 value, char *string, int radix );
char * _ui64toa( unsigned _int64 value, char *string, int radix );
wchar_t * _itow( int value, wchar_t *string, int radix );
wchar_t * _i64tow( __int64 value, wchar_t *string, int radix );
wchar_t * _ui64tow( unsigned __int64 value, wchar_t *string, int radix );
Routine Required Header Compatibility
_itoa <stdlib.h> Win 95, Win NT
_i64toa <stdlib.h> Win 95, Win NT
_ui64toa <stdlib.h> Win 95, Win NT
_itow <stdlib.h> Win 95, Win NT
_i64tow <stdlib.h> Win 95, Win NT
_ui64tow <stdlib.h> Win 95, Win NT
For additional compatibility information, see Compatibility in the Introduction.
Libraries
LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version
Return Value
Each of these functions returns a pointer to string. There is no error return.
Parameters
value
Number to be converted
string
String result
radix
Base of value; must be in the range 2 – 36
Remarks
The _itoa, _i64toa, and _ui64toa function convert the digits of the given value argument to a null-terminated character string and stores the result (up to 33 bytes) in string. If radix equals 10 and value is negative, the first character o
f the stored string is the minus sign ( – ). _itow, _i64tow, and _ui64tow are wide-character versions of _itoa, _i64toa, and _ui64toa respectively.
Generic-Text Routine Mappings
TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined
_itot _itoa _itoa _itow
Example
/* ITOA.C: This program converts integers of various
* sizes to strings in various radixes.
*/
#include <stdlib.h>
#include <stdio.h>
void main( void )
{
char buffer[20];
int i = 3445;
long l = -344115L;
unsigned long ul = 1234567890UL;
_itoa( i, buffer, 10 );
printf( "String of integer %d (radix 10): %s\n", i, buffer );
_itoa( i, buffer, 16 );
printf( "String of integer %d (radix 16): 0x%s\n", i, buffer );
_itoa( i, buffer, 2 );
printf( "String of integer %d (radix 2): %s\n", i, buffer );
_ltoa( l, buffer, 16 );
printf( "String of long int %ld (radix 16): 0x%s\n", l,
buffer );
_ultoa( ul, buffer, 16 );
printf( "String of unsigned long %lu (radix 16): 0x%s\n", ul,
buffer );
}
Output
String of integer 3445 (radix 10): 3445
String of integer 3445 (radix 16): 0xd75
String of integer 3445 (radix 2): 110101110101
String of long int -344115 (radix 16): 0xfffabfcd
String of unsigned long 1234567890 (radix 16): 0x499602d2
然後將相對應的ASCII碼變為101010111的模式丫
如 ABC
變成1000001 1000010 1000011三個連接
然後依順序將1,0以"數字"的格式輸出<----您是用字串表示且一次只能一個字元
謝囉.......
【 在 SHBK...@bbs.kimo.com.tw (Blues) 的大作中提到: 】
: ※ 引述《lovewe...@bbs.cs.ntust.edu.tw (福幸是愛被)》之銘言:
--
請問一個題外話,
如何檢查輸入的值是不是 int 呢...
謝謝
--
----== Posted via Openfind 網路論壇 ==-----
http://bbs.openfind.com.tw/ 提供免費的登載文章及查詢服務
※來源:208.57.*