V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
Flasky
V2EX  ›  问与答

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

  •  
  •   Flasky · 2019-04-13 11:55:41 +08:00 · 1972 次点击
    这是一个创建于 1811 天前的主题,其中的信息可能已经有所发展或是发生改变。

    比如:

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


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

    想了半天没想出来怎么写

    7 条回复    2019-04-13 12:43:53 +08:00
    sol33
        1
    sol33  
       2019-04-13 11:58:53 +08:00   ❤️ 2
    sprintf() ??
    另外没有考虑字符串结束符,所以数组长度+1
    而且
    ascii[0] == '4'
    ascii[1] == '5'
    CBBing
        2
    CBBing  
       2019-04-13 12:02:29 +08:00 via Android
    先把十进制数拆开(比如 mod 10 ),然后加上字符'0',再存入字符数组
    Flasky
        3
    Flasky  
    OP
       2019-04-13 12:04:06 +08:00
    @sol33 靠,我想复杂了,忘记了 sprintf()这一函数,谢谢了
    Flasky
        4
    Flasky  
    OP
       2019-04-13 12:05:29 +08:00
    @CBBing 这个方法也不错
    CBBing
        5
    CBBing  
       2019-04-13 12:25:59 +08:00 via Android
    @Flasky 1 楼方法确实不错
    newtype0092
        6
    newtype0092  
       2019-04-13 12:34:38 +08:00   ❤️ 1
    你为何能把一个最简单的数字转字符串的问题描述的如此清新脱俗。。。
    ysc3839
        7
    ysc3839  
       2019-04-13 12:43:53 +08:00 via Android
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1045 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 19:17 · PVG 03:17 · LAX 12:17 · JFK 15:17
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.