• 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 == '')
            
    return -1;
        
    int result = 0;
        
    while(*str != '')
        {
            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 == '')
            
    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 == '')
            
    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

    个人学习笔记,欢迎拍砖!---by hellogiser

    Author: hellogiser
    Warning: 本文版权归作者和博客园共有,欢迎转载,但请保留此段声明,且在文章页面明显位置给出原文连接。Thanks!
    Me: 如果觉得本文对你有帮助的话,那么【推荐】给大家吧,希望今后能够为大家带来更好的技术文章!敬请【关注】
  • 相关阅读:
    UML学习
    A problem has been encountered while loading the setup components. VS2008
    C#获取资源中的图片文件
    [网络技巧]一个网线头来测试网络程序
    通过传递"窗体的名字"来实例化窗体
    [解决方案] Flex TypeError: Error #1007: 尝试实例化的函数不是构造函数。
    Flex编译器的一些参数
    ArcGIS 9.3安装流程(包括Desktop和Server)
    Oracle Wrong solution [整理中]
    What's DOM?(1)
  • 原文地址:https://www.cnblogs.com/hellogiser/p/16-to-3-or-k.html
Copyright © 2020-2023  润新知