请问:setlocale函数怎么用

leetow2003 2009-03-06 09:49:33
当我用cmd命令设置控制台的代码页为437后,请看:
void main()
{
unsigned char a1[]={0xa7,0xa8,0};
setlocale(LC_ALL,".936");
printf("%s\n",a1);
}
程序不能显示汉字,请问setlocale函数怎么设置代码页为汉字的?
...全文
1105 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
太乙 2009-03-06
  • 打赏
  • 举报
回复
/* setlocale example */
#include <stdio.h>
#include <time.h>
#include <locale.h>

int main ()
{
time_t rawtime;
struct tm * timeinfo;
char buffer [80];

struct lconv * lc;

time ( &rawtime );
timeinfo = localtime ( &rawtime );

int twice=0;

do {
printf ("Locale is: %s\n", setlocale(LC_ALL,NULL) );

strftime (buffer,80,"%c",timeinfo);
printf ("Date is: %s\n",buffer);

lc = localeconv ();
printf ("Currency symbol is: %s\n-\n",lc->currency_symbol);

setlocale (LC_ALL,"");
} while (!twice++);

return 0;
}
-----------------------------------------------
One of the possible outputs when the previous code is run is:
Locale is: CDate is: 01/15/07 13:33:47Currecy symbol is: -Locale is: English_United States.1252Date is: 1/15/07 1:33:47 PMCurrency symbol is: $-

xuguod20042576 2009-03-06
  • 打赏
  • 举报
回复
setlocale()函数


Definition and Usage
定义和用法
The setlocale() function sets locale information.
setlocale()函数的作用是:设置场景信息。

Locale information is language, monetary, time and other information specific for a geographical area.
本地信息可以是指定的某个区域的语言、货币、时间以及其它的信息。

This function returns the current locale settings, or FALSE on failure.
如果这个函数成功执行,将返回当前的场景属性;如果执行失败,将返回False。

Syntax
语法
setlocale(constant,location)



Parameter参数 Description描述
constant Required. Specifies what locale information should be set.
必要参数。指定需要设置的场景信息

Available constants:
可用的常量:

LC_ALL - All of the below
LC_ALL – 所有下属的常量
LC_COLLATE - Sort order
LC_COLLATE – 排列顺序
LC_CTYPE - Character classification and conversion (e.g. all characters should be lower or upper-case)
LC_CTYPE – 字符分类和转换(例如:将所有的字符转换成小写或大写形式)
LC_MESSAGES - System message formatting
LC_MESSAGES – 系统信息格式
LC_MONETARY - Monetary/currency formatting
LC_MONETARY – 货币 / 通货格式
LC_NUMERIC - Numeric formatting
LC_NUMERIC – 数值格式
LC_TIME - Date and time formatting
LC_TIME – 日期和时间格式

location Required. Specifies what country/region to set the locale information to. Can be a string or an array. If the location is an array, setlocale() will try each array element until it finds a valid language or region code. This is very useful if a region is known under different names on different systems.
必要参数。指定需要进行场景信息设置的国家或区域。它可以由一个字符串或一个数组组成。如果本地区域是一个数组,那么setlocale()函数将尝试每 个数组元素直到它从中获取有效的语言和区域代码信息为止。如果一个区域处于不同操作系统中的不同名称下,那么这个参数将非常有用。

Note: Here you can find language and region codes.
注意:你可以通过下面一个地址查询:语言信息和区域代码





--------------------------------------------------------------------------------

Tips and Notes
提示和注意点
Note: The setlocale() function changes the locale only for the current script.
注意:setlocale()函数仅改变当前脚本程序中的场景。

Tip: The locale information can be set to system default with setlocale(LC_ALL,NULL)
提示:可以使用setlocale(LC_ALL,NULL)函数将场景信息设置为系统默认值。

liuenhai211 2009-03-06
  • 打赏
  • 举报
回复
友情UP
leetow2003 2009-03-06
  • 打赏
  • 举报
回复
还是不行
  • 打赏
  • 举报
回复
up1楼.
neu_sunlei 2009-03-06
  • 打赏
  • 举报
回复
代码看的晕晕的
glorywu 2009-03-06
  • 打赏
  • 举报
回复
// C
#include <stdio.h>
#include <locale.h>
int main( void )
{
setlocale( LC_ALL, "chs" );
//setlocale( LC_ALL, "Chinese-simplified" );
//setlocale( LC_ALL, "ZHI" );
//setlocale( LC_ALL, ".936" );
wprintf( L"中国" );

return 0;
}

// C++
#include <iostream>
#include <locale>
using namespace std;
int main( void )
{
locale loc( "chs" );
//locale loc( "Chinese-simplified" );
//locale loc( "ZHI" );
//locale loc( ".936" );
wcout.imbue( loc );
std::wcout << L"中国" << endl;

return 0;
}

说明:别混合使用 setlocale 和 std::locale

69,382

社区成员

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

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