sprintf()在内存的编码转化问题

yjzl1911 2008-04-14 04:00:03
系统在用sprintf()将一个整型数据转换成字符串的过程中,是否就是把整型数据转化成BCD码存放在内存里,然后在需要向传输给显示器时再转化成ASCII码输出呢?
这只是我的猜测,对这比较感兴趣,希望知道的说一下阿,在此谢过了
...全文
199 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
yjzl1911 2008-04-14
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 steedhorse 的回复:]
个人认为完全可以直接将整数转换成字符串,而不需要先转换成BCD。
[/Quote]
我现在也认为是这样的。诚如Treazy所说,BCD码对于看惯十进制的人来说才是重要的,而对于计算机,转换成BCD码却是毫无必要的。
当然,若有协议约定就另当别论了。
晨星 2008-04-14
  • 打赏
  • 举报
回复
个人认为完全可以直接将整数转换成字符串,而不需要先转换成BCD。
就呆在云上 2008-04-14
  • 打赏
  • 举报
回复
34楼的说的有理
这个里面有一个格式化的问题,就当成printf()用,只是这个玩意存入了一个指定的字符串里面,而不是打印到终端
还有程序设计的时候这个函数一般是在内核里面实现的,我想应该没有人动不动就把什么东西转化为BCD什么的吧,就是用我们常用的ascii码,即便是0101这么表示也是通过ascii格式或者用基本数据类型进行的。
sirdan 2008-04-14
  • 打赏
  • 举报
回复
[Quote=引用楼主 yjzl1911 的帖子:]
系统在用sprintf()将一个整型数据转换成字符串的过程中,是否就是把整型数据转化成BCD码存放在内存里,然后在需要向传输给显示器时再转化成ASCII码输出呢?
这只是我的猜测,对这比较感兴趣,希望知道的说一下阿,在此谢过了
[/Quote]

要么是BCD要么是ASCII,与操作系统有关系。
二者其实都是整数,转换两次毫无意义。
Treazy 2008-04-14
  • 打赏
  • 举报
回复
BCD码只是一种数据格式

在内存中存放的数据永远都是0,1

但是对于看惯10进制的人来说,BCD显得很重要,这个楼主你无须太作关心

当你在做数据通讯时,如果要求一方提供BCD码的数据格式,那就需要进行转换,具体格式就是双方定的协议去决定了
Treazy 2008-04-14
  • 打赏
  • 举报
回复
sprintf就是格式化数据到字符串

至于如果要将整型转换成字符串到buf中,那只要format的参数设置正确那就可以了

那在buf中存放的数据是什么格式的,你可以认为是BCD码,但这里应该不存在这个概念

只是字符的ascii码而已

liufangbj 2008-04-14
  • 打赏
  • 举报
回复
学习
baihacker 2008-04-14
  • 打赏
  • 举报
回复

/***
*printf.c - print formatted
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
* defines printf() - print formatted data
*
*******************************************************************************/

#include <cruntime.h>
#include <stdio.h>
#include <dbgint.h>
#include <stdarg.h>
#include <file2.h>
#include <internal.h>
#include <mtdll.h>
#include <stddef.h>
#include <process.h>

/***
*int printf(format, ...) - print formatted data
*
*Purpose:
* Prints formatted data on stdout using the format string to
* format data and getting as many arguments as called for
* Uses temporary buffering to improve efficiency.
* _output does the real work here
*
*Entry:
* char *format - format string to control data format/number of arguments
* followed by list of arguments, number and type controlled by
* format string
*
*Exit:
* returns number of characters printed
*
*Exceptions:
*
*******************************************************************************/

int __cdecl printf (
const char *format,
...
)
/*
* stdout 'PRINT', 'F'ormatted
*/
{
va_list arglist;
int buffing;
int retval;

_VALIDATE_RETURN( (format != NULL), EINVAL, -1);

va_start(arglist, format);

_lock_str2(1, stdout);
__try {

buffing = _stbuf(stdout);

retval = _output_l(stdout,format,NULL,arglist);

_ftbuf(buffing, stdout);

}
__finally {
_unlock_str2(1, stdout);
}

return(retval);
}

int __cdecl _printf_l (
const char *format,
_locale_t plocinfo,
...
)
{
va_list arglist;

va_start(arglist, plocinfo);

return _vprintf_l(format, plocinfo, arglist);
}


int __cdecl _printf_s_l (
const char *format,
_locale_t plocinfo,
...
)
{
va_list arglist;

va_start(arglist, plocinfo);

return _vprintf_s_l(format, plocinfo, arglist);
}

int __cdecl printf_s (
const char *format,
...
)
{
va_list arglist;

va_start(arglist, format);

return _vprintf_s_l(format, NULL, arglist);
}

int __cdecl _printf_p_l (
const char *format,
_locale_t plocinfo,
...
)
{
va_list arglist;

va_start(arglist, plocinfo);

return _vprintf_p_l(format, plocinfo, arglist);
}

int __cdecl _printf_p (
const char *format,
...
)
{
va_list arglist;

va_start(arglist, format);

return _vprintf_p_l(format, NULL, arglist);
}

static UINT_PTR __enable_percent_n = 0;

/***
*int _set_printf_count_output(int)
*
*Purpose:
* Enables or disables %n format specifier for printf family functions
*
*Internals:
* __enable_percent_n is set to (__security_cookie|1) for security reasons;
* if set to a static value, an attacker could first modify __enable_percent_n
* and then provide a malicious %n specifier. The cookie is ORed with 1
* because a zero cookie is a possibility.
******************************************************************************/
int __cdecl _set_printf_count_output(int value)
{
int old = (__enable_percent_n == (__security_cookie | 1));
__enable_percent_n = (value ? (__security_cookie | 1) : 0);
return old;
}

/***
*int _get_printf_count_output()
*
*Purpose:
* Checks whether %n format specifier for printf family functions is enabled
******************************************************************************/
int __cdecl _get_printf_count_output()
{
return ( __enable_percent_n == (__security_cookie | 1));
}

70,026

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧