Flasky
V2EX  ›  问与答

C 如何把一个十进制数转为 ASCII 数组

  •  
  •   Flasky · Apr 13, 2019 · 2851 views
    This topic created in 2591 days ago, the information mentioned may be changed or developed.

    比如:

       char ascii[2], dec = 45;
       decToAscii(dec, ascii);
    


       ascii[0] == "4"
       ascii[1] == "5"
    

    想了半天没想出来怎么写

    7 replies    2019-04-13 12:43:53 +08:00
    sol33
        1
    sol33  
       Apr 13, 2019   ❤️ 2
    sprintf() ??
    另外没有考虑字符串结束符,所以数组长度+1
    而且
    ascii[0] == '4'
    ascii[1] == '5'
    CBBing
        2
    CBBing  
       Apr 13, 2019 via Android
    先把十进制数拆开(比如 mod 10 ),然后加上字符'0',再存入字符数组
    Flasky
        3
    Flasky  
    OP
       Apr 13, 2019
    @sol33 靠,我想复杂了,忘记了 sprintf()这一函数,谢谢了
    Flasky
        4
    Flasky  
    OP
       Apr 13, 2019
    @CBBing 这个方法也不错
    CBBing
        5
    CBBing  
       Apr 13, 2019 via Android
    @Flasky 1 楼方法确实不错
    newtype0092
        6
    newtype0092  
       Apr 13, 2019   ❤️ 1
    你为何能把一个最简单的数字转字符串的问题描述的如此清新脱俗。。。
    ysc3839
        7
    ysc3839  
       Apr 13, 2019 via Android
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2935 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 06:07 · PVG 14:07 · LAX 23:07 · JFK 02:07
    ♥ Do have faith in what you're doing.