Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

數字轉二進位的函數是什麼丫

1 view
Skip to first unread message

福幸是愛被

unread,
Mar 10, 2002, 12:49:45 PM3/10/02
to
如題

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

學習中

unread,
Mar 10, 2002, 1:58:06 PM3/10/02
to
==> lovewe...@bbs.cs.ntust.edu.tw (福幸是愛被) 提到:
: 如題

: C語言中將數字轉為二進位第的函數是什麼丫
: 如 8 ---> 1000
: 謝謝

我是沒見過有二進位的轉換函數拉

但是你可以利用長除法的方式將數字表示成二進位阿

將欲轉換的數 除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

福幸是愛被

unread,
Mar 10, 2002, 3:41:45 PM3/10/02
to
【 在 oisi...@bbs.mgt.ncu.edu.tw (學習中) 的大作中提到: 】
: ==> lovewe...@bbs.cs.ntust.edu.tw (福幸是愛被) 提到:

: : 如題
: : C語言中將數字轉為二進位第的函數是什麼丫
: : 如 8 ---> 1000
: : 謝謝
: 我是沒見過有二進位的轉換函數拉
: 但是你可以利用長除法的方式將數字表示成二進位阿
: 將欲轉換的數 除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會很簡單的拉

這是我寫的啦......很差....新手
#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

掛站僵屍........

unread,
Mar 10, 2002, 6:29:14 PM3/10/02
to
※ 引述《lovewe...@bbs.cs.ntust.edu.tw (福幸是愛被)》之銘言:

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

....

unread,
Mar 11, 2002, 12:21:34 AM3/11/02
to
> > 一 result因為並不知道input可能會用到幾個位元,所以必須設一個很大的值
> > 二 8 --> 1000 但我得到的result其實是相反的,result=0001
> > 三 若有二個連續的數值,"8","4",我希望能得到1000 100 且照順序,不知道要如何丫
> > 希望大家提點新手.....^^y
> 用函式遞迴的的特性來作(如果只是要印的話)
> 這樣就不不用陣列儲存...輸出值序列也能正確........
嗯...不用自己寫陣列...
好像其實呼叫一個Subroutine
程式本身就是用陣列來做儲存..
--
http://www.taconet.com.tw/poser/rayer.jpg
--
[1;32m※ Origin: [33m交大資工鳳凰城資訊站 [37m<bbs.csie.nctu.edu.tw> [m
[1;31m◆ From: [36mpc9.tem.nctu.edu.tw [m

Blues

unread,
Mar 11, 2002, 12:55:47 AM3/11/02
to
※ 引述《lovewe...@bbs.cs.ntust.edu.tw (福幸是愛被)》之銘言:

> 如題
> C語言中將數字轉為二進位第的函數是什麼丫
> 如 8 ---> 1000
> 謝謝
#include <stdlib.h>
#include <stdio.h>

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

Blues

unread,
Mar 11, 2002, 12:57:10 AM3/11/02
to
※ 引述《lovewe...@bbs.cs.ntust.edu.tw (福幸是愛被)》之銘言:
> 如題
> C語言中將數字轉為二進位第的函數是什麼丫
> 如 8 ---> 1000
> 謝謝
我順便把msdn裡的給po出來好了....
_itoa, _i64toa, _ui64toa, _itow, _i64tow, _ui64tow
Convert an integer to a string.

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

福幸是愛被

unread,
Mar 12, 2002, 1:55:36 PM3/12/02
to
有沒有辦法輸入ABCD....

然後將相對應的ASCII碼變為101010111的模式丫

如 ABC

變成1000001 1000010 1000011三個連接

然後依順序將1,0以"數字"的格式輸出<----您是用字串表示且一次只能一個字元

謝囉.......


【 在 SHBK...@bbs.kimo.com.tw (Blues) 的大作中提到: 】
: ※ 引述《lovewe...@bbs.cs.ntust.edu.tw (福幸是愛被)》之銘言:


--

sonny

unread,
Mar 13, 2002, 11:48:45 AM3/13/02
to
※ 引述《SHBK...@bbs.kimo.com.tw (Blues)》之銘言:

> ※ 引述《lovewe...@bbs.cs.ntust.edu.tw (福幸是愛被)》之銘言:
> > 如題
> > C語言中將數字轉為二進位第的函數是什麼丫
> > 如 8 ---> 1000
> > 謝謝
> #include <stdlib.h>
> #include <stdio.h>
> 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

請問一個題外話,
如何檢查輸入的值是不是 int 呢...
謝謝

--
----== Posted via Openfind 網路論壇 ==-----
http://bbs.openfind.com.tw/ 提供免費的登載文章及查詢服務
※來源:208.57.*


0 new messages