Flasky
V2EX  ›  问与答

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

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