16进制字符串转换为3进制(扩展至K进制)

【本文链接】

http://www.cnblogs.com/hellogiser/p/16-to-3-or-k.html

【题目】

写代码把16进制表示的串转换为3进制表示的串。例如x=”5”,则返回:”12”;又例如:x=”F”,则返回”120”

【代码】

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
 
/*
    version: 1.0
    author: hellogiser
    blog: http://www.cnblogs.com/hellogiser
    date: 2014/9/22
*/


#include   "stdafx.h"
#include  <iostream>
#include  <stack>
using   namespace  std;

int  getvalue( char  c)
{
    
if  (c >=  '0'  && c <=  '9' )
        
return  c -  '0' ;
    
else   if  (c >=  'a'  && c <=  'f' )
        
return  c -  'a'  +  10 ;
    
else   if  (c >=  'A'  && c <=  'F' )
        
return  c -  'A'  +  10 ;
    
return   0 ;
}

// "f" ===>15
int  GetString16Value( char  *str)
{
    
if (str ==  NULL  || *str ==  '\0' )
        
return  - 1 ;
    
int  result =  0 ;
    
while (*str !=  '\0' )
    {
        result = result * 
16  + getvalue(*str);
        str ++;
    }
    
return  result;
}

void  output(stack< int > &s)
{
    
while  (!s.empty())
    {
        cout << s.top();
        s.pop();
    }
    cout << endl;
}

// f --->15 --->120
/*
15     n%3   n/3
        0     5
        2     1
        1     0
*/

void  String16to3( char  *str)
{
    
if (str ==  NULL  || *str ==  '\0' )
        
return ;
    
int  value16 = GetString16Value(str);
    stack<
int > result;
    
int  t;
    
// do while(value16)      // if value16=0,then result push 0
     do
    {
        t = value16 % 
3 ;
        result.push(t); 
// save t
        value16 /=  3 ;
    }
    
while (value16);
    
// reverse output, so here we use stack to implement this function
     // 021 ===>120
    output(result);
}


// 16 to K
void  String16toK( char  *str,  unsigned   int  K)
{
    
if (str ==  NULL  || *str ==  '\0' )
        
return ;
    
if  (K ==  0  || K >  10 )
        
return ;

    
int  value16 = GetString16Value(str);
    stack<
int > result;
    
int  t;
    
do
    {
        t = value16 % K;
        result.push(t); 
// save t
        value16 /= K;
    }
    
while (value16);
    
// reverse output, so here we use stack to implement this function
    output(result);
}

void  test_base( char  *str)
{
    String16to3(str);
    String16toK(str, 
3 );
}

void  test_case0()
{
    
char  *str =  NULL ;
    test_base(str);
}

void  test_case1()
{
    
char  str[] =  "" ;
    test_base(str);
}

void  test_case2()
{
    
char  str[] =  "0" ;
    test_base(str);
}

void  test_case3()
{
    
char  str[] =  "5" ;
    test_base(str);
}

void  test_case4()
{
    
char  str[] =  "F" ;
    test_base(str);
}

void  test_main()
{
    test_case0(); 
//
    test_case1();  //
    test_case2();  // 0
    test_case3();  // 12
    test_case4();  // 120
}

int  main()
{
    test_main();
    
return   0 ;
}

【参考】

http://www.cnblogs.com/sooner/p/3280050.html

转载于:https://www.cnblogs.com/hellogiser/p/16-to-3-or-k.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值