把 n 用m 进制显示

作者在 2010-07-03 00:15:20 发布以下内容
#include <stdio.h>
// #include <WATLIB.H> 测试表明lib库不通用
//#include <vcLIB.H>

typedef  char BOOL ;
typedef  unsigned char UCHAR8 ;

#define  NUM  10

void PN(int n,int m)  /* 把 n 用m 进制显示 */
{
    char temp[10]={0};
    int i=0;
    do {
        temp[i] = n%m;
        i++;
        n /= m;
    }while(n>0);
    for(i -= 1;i>=0;i--){
        printf("%x",temp[i]);
    }
}

int main()
{
    int i;
    int date[NUM];

    for (i=0;i<NUM ;i++)
    {
        date[i] = i;
        PN(date[i],2);printf("\t");
    }
    printf("\n");
    for (i=0;i<NUM ;i++)
    {
        date[i] ^= (date[i]>>1); // 转成BCD Gray 码
        PN(date[i],2);printf("\t");
    }
    printf("\n");
    
    for (i=0;i<NUM ;i++)
    {
        int temp = date[i];// 转成BCD Gray 码
        while (temp>0)
        {
            temp >>= 1;
            date[i] ^= temp;
        }
        PN(date[i],2);printf("\t");
    }

    return 0;
}
编码 | 阅读 1343 次
文章评论,共1条
vfdff(作者)
2010-07-09 01:02
1
// test.cpp : Defines the entry point for the console application.<br />
// JPEG2000 中小波参数浮点系数转定点<br />
<br />
#include &quot;stdafx.h&quot;<br />
#include &lt;iostream&gt;<br />
#include &lt;stdio.h&gt;<br />
using namespace std;<br />
<br />
union data {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //unsigned long ul;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; __int64 ul;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; struct {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; signed&nbsp;&nbsp;l ;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; signed&nbsp;&nbsp;h ;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }inner0;<br />
#define&nbsp;&nbsp;HHH&nbsp; &nbsp; &nbsp; &nbsp; inner0.h<br />
#define&nbsp;&nbsp;LLL&nbsp; &nbsp; &nbsp; &nbsp; inner0.l<br />
}test; <br />
<br />
//--0=&quot;1001101001111101&quot;;-1.586134342 <br />
//--1=&quot;1111110010011100&quot;;-0.052980118<br />
//--2=&quot;0011100010000001&quot;; 0.882911076<br />
//--3=&quot;0001110001100010&quot;; 0.443506852<br />
//--4=&quot;0011011110101011&quot;;&nbsp; &nbsp; &nbsp; &nbsp; <br />
//--5=&quot;0100100110010011&quot;; 1.149604398 <br />
//<br />
//constant cst0 : std_logic_vector(15 downto 0):=&quot;1001101001111101&quot;; --16 <br />
//constant cst1 : std_logic_vector(13 downto 0):=&quot;11111100100111&quot;;&nbsp; &nbsp;--14<br />
//constant cst2 : std_logic_vector(14 downto 0):=&quot;011100010000001&quot;;&nbsp;&nbsp;--15 <br />
//constant cst3 : std_logic_vector(13 downto 0):=&quot;01110001100010&quot;;&nbsp; &nbsp;--14<br />
//constant cst4 : std_logic_vector(15 downto 0):=&quot;0011011110101011&quot;; --16<br />
//constant cst5 : std_logic_vector(15 downto 0):=&quot;0100100110010011&quot;; --16<br />
<br />
int main()<br />
{<br />
&nbsp; &nbsp; &nbsp; &nbsp; double cst0 = -1.586134342 ;<br />
&nbsp; &nbsp; int num0 = (int)(cst0 *(1&lt;&lt;14));<br />
&nbsp; &nbsp; &nbsp; &nbsp; double cst1 = -0.052980118 ;<br />
&nbsp; &nbsp; &nbsp; &nbsp; int num1 = (int)(cst1 *(1&lt;&lt;12));<br />
&nbsp; &nbsp; &nbsp; &nbsp; double cst2 = 0.882911076 ;<br />
&nbsp; &nbsp; &nbsp; &nbsp; int num2 = (int)(cst2 *(1&lt;&lt;14));<br />
&nbsp; &nbsp; &nbsp; &nbsp; double cst3 = 0.443506852 ;<br />
&nbsp; &nbsp; &nbsp; &nbsp; int num3 = (int)(cst3 *(1&lt;&lt;14));<br />
&nbsp; &nbsp; &nbsp; &nbsp; double cst4 = 1/1.149604398 ;<br />
&nbsp; &nbsp; &nbsp; &nbsp; int num4 = (int)(cst4 *(1&lt;&lt;14));<br />
&nbsp; &nbsp; &nbsp; &nbsp; double cst5 = 1.149604398 ;<br />
&nbsp; &nbsp; &nbsp; &nbsp; int num5 = (int)(cst5 *(1&lt;&lt;14));<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; //union data test;<br />
&nbsp; &nbsp; &nbsp; &nbsp; test.ul = 0xfff5123f5678;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; //printf(&quot;%12xl,%x,%x,%d\n\n&quot;,test.ul,test.H,test.L,sizeof(union data));<br />
&nbsp; &nbsp; &nbsp; &nbsp; printf(&quot;%d,%d,%d\n\n&quot;,sizeof(long),sizeof(unsigned long),sizeof(__int64));<br />
&nbsp; &nbsp; &nbsp; &nbsp; printf(&quot;%12x,%x,%x,%x\n\n&quot;,0xfff5123f5678,test.HHH,test.LLL,sizeof(long));<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; printf(&quot;%X\n&quot;,num0);<br />
&nbsp; &nbsp; &nbsp; &nbsp; printf(&quot;%x\n&quot;,num1);<br />
&nbsp; &nbsp; &nbsp; &nbsp; printf(&quot;%x\n&quot;,num2);<br />
&nbsp; &nbsp; &nbsp; &nbsp; printf(&quot;%x\n&quot;,num3);<br />
&nbsp; &nbsp; &nbsp; &nbsp; printf(&quot;%x\n&quot;,num4);<br />
&nbsp; &nbsp; &nbsp; &nbsp; printf(&quot;%x\n&quot;,num5);<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; printf(&quot;%d\n&quot;,num0);<br />
&nbsp; &nbsp; &nbsp; &nbsp; printf(&quot;%d\n&quot;,num1);<br />
&nbsp; &nbsp; &nbsp; &nbsp; printf(&quot;%d\n&quot;,num2);<br />
&nbsp; &nbsp; &nbsp; &nbsp; printf(&quot;%d\n&quot;,num3);<br />
&nbsp; &nbsp; &nbsp; &nbsp; printf(&quot;%d\n&quot;,num4);<br />
&nbsp; &nbsp; &nbsp; &nbsp; printf(&quot;%d\n&quot;,num5);<br />
&nbsp; &nbsp; &nbsp; &nbsp; return 0;<br />
}
游客请输入验证码
浏览1941175次