请问怎样输出16进制数

asplh1234 2010-10-17 06:09:46
请问怎样把字符串转成16进制并且输出
使用printf(“%x”,str);可以显示 但是怎样把这个值赋给一个变量,因为我想把转换后16进制的的值写到文件里面
...全文
1622 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
fibbery 2010-10-17
  • 打赏
  • 举报
回复
char s[] = "abcde";  
fp = fopen( "c:\\fprintf.txt", "w" );
int i=0;
for(;s[i]!='\0';++i)//更正一下结束条件判断
fprintf( fp, "%x", s[i]);
fclose(fp);
fibbery 2010-10-17
  • 打赏
  • 举报
回复

char s[] = "abcde";
fp = fopen( "c:\\fprintf.txt", "w" );
int i=0;
for(;s[i]!=NULL;++i)
fprintf( fp, "%x", s[i]);
fclose(fp);
asplh1234 2010-10-17
  • 打赏
  • 举报
回复

char s[] = "abcde";



fp = fopen( "c:\\fprintf.txt", "w" );

fprintf( fp, "%x", s);


fclose( stream );

这样写怎么输出12ff78?
小魔菇 2010-10-17
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 asplh1234 的回复:]

sprintf(s,"%x","a");

请问s要定义成什么类型

定义各种类型都出现该内存不能为written

能不能给个具体的例子
[/Quote]

你不是要写到文件中去吗?
用fprintf哈

你的问题
定义一下即可 char str[16] = {0}; 数组长度你自己定
lgch123456 2010-10-17
  • 打赏
  • 举报
回复
在linux中可以这样直接实现,直接运行程序,定向输出到一个文件即可:
如: a.out > XXXXX
就是将结果输出到XXXX中
asplh1234 2010-10-17
  • 打赏
  • 举报
回复
sprintf(s,"%x","a");

请问s要定义成什么类型

定义各种类型都出现该内存不能为written

能不能给个具体的例子
小魔菇 2010-10-17
  • 打赏
  • 举报
回复
fibbery 你在做爪子哦
刷屏所
fibbery 2010-10-17
  • 打赏
  • 举报
回复
Flag
Meaning
Default


Left align the result within the given field width.
Right align.

+
Prefix the output value with a sign (+ or –) if the output value is of a signed type.
Sign appears only for negative signed values (–).

0
If width is prefixed with 0, zeros are added until the minimum width is reached. If 0 and – appear, the 0 is ignored. If 0 is specified with an integer format (i, u, x, X, o, d) and a precision specification is also present (for example, %04.d), the 0 is ignored.
No padding.

blank (' ')
Prefix the output value with a blank if the output value is signed and positive; the blank is ignored if both the blank and + flags appear.
No blank appears.

#
When used with the o, x, or X format, the # flag prefixes any nonzero output value with 0, 0x, or 0X, respectively.
No blank appears.


When used with the e, E, f, a or A format, the # flag forces the output value to contain a decimal point in all cases.
Decimal point appears only if digits follow it.


When used with the g or G format, the # flag forces the output value to contain a decimal point in all cases and prevents the truncation of trailing zeros.

Ignored when used with c, d, i, u, or s.
Decimal point appears only if digits follow it. Trailing zeros are truncated.

fibbery 2010-10-17
  • 打赏
  • 举报
回复
不贴了,你还是自己看MSDN吧!
格式串,你可以参考printf函数的参考页。
fibbery 2010-10-17
  • 打赏
  • 举报
回复
Format Specification Fields: printf and wprintf Functions
This topic describes the syntax for format specifications fields, used in printf, wprintf and related functions. More secured versions of these functions are available, see printf_s, _printf_s_l, wprintf_s, _wprintf_s_l. For details on the individual functions, see the documentation for those specific functions. For a complete listing of these functions, see Stream I/O.

A format specification, which consists of optional and required fields, has the following form:

%[flags] [width] [.precision] [{h | l | ll | I | I32 | I64}]type

Each field of the format specification is a single character or a number signifying a particular format option. The simplest format specification contains only the percent sign and a type character (for example, %s). If a percent sign is followed by a character that has no meaning as a format field, the character is copied to stdout. For example, to print a percent-sign character, use %%.

The optional fields, which appear before the type character, control other aspects of the formatting, as follows:

type
Required character that determines whether the associated argument is interpreted as a character, a string, or a number (see the "printf Type Field Characters" table in printf Type Field Characters).

flags
Optional character or characters that control justification of output and printing of signs, blanks, decimal points, and octal and hexadecimal prefixes (see the "Flag Characters" table in Flag Directives). More than one flag can appear in a format specification.

width
Optional number that specifies the minimum number of characters output (see printf Width Specification).

precision
Optional number that specifies the maximum number of characters printed for all or part of the output field, or the minimum number of digits printed for integer values (see the "How Precision Values Affect Type" table in Precision Specification).

h| l| ll| I| I32| I64
Optional prefixes to type-that specify the size of argument (see the "Size Prefixes" table in Size and Distance Specification).

小魔菇 2010-10-17
  • 打赏
  • 举报
回复
如果要写到文件中 就用fprintf即可
fprintf(fd,"%x",str);

int
fprintf(FILE * restrict stream, const char * restrict format, ...);

fibbery 2010-10-17
  • 打赏
  • 举报
回复
int sprintf( char *buffer, const char *format [, argument] ... );
int swprintf( wchar_t *buffer, const wchar_t *format [, argument] ... );

Parameters
buffer
Storage location for output.
format
Format-control string.
argument
Optional arguments.
Libraries
All versions of the C run-time libraries.
Return Values
sprintf returns the number of bytes stored in buffer, not counting the terminating null character. swprintf returns the number of wide characters stored in buffer, not counting the terminating null wide character.
Remarks
The sprintf function formats and stores a series of characters and values in buffer. Each argument (if any) is converted and output according to the corresponding format specification in format.
The format consists of ordinary characters and has the same form and function as the format argument for printf. A null character is appended after the last character written. If copying occurs between strings that overlap, the behavior is undefined.
swprintf is a wide-character version of sprintf; the pointer arguments to swprintf are wide-character strings. Detection of encoding errors in swprintf may differ from that in sprintf.
Security Remarks
The first argument, buffer, must be large enough to hold the formatted version of format and the trailing NULL ('\0') character otherwise a buffer overrun may occur.
This can lead to a denial of service attack against the application if an access violation occurs, or in the worst case, allow an attacker to inject executable code into your process.
This is especially true if buffer is a stack-based buffer.
Be also aware of the dangers of a user or application providing format as a variable. The following example is dangerous because the attacker may set szTemplate to "%90s%10s" which will create a 100-byte string:
void test(char *szTemplate,char *szData1, char *szData2) {
char buf[BUFFER_SIZE];
sprintf(buf,szTemplate,szData1,szData2);
}
Consider using _snprintf instead, or consider using an appropriate strsafe.h function.
For more information, see Safe String Functions.
Generic-Text Routine Mappings
TCHAR.H Routine _UNICODE Defined
_stprintfswprintf

For more information about TCHAR.H routines, see Generic Text Mappings.
Example
/* SPRINTF.C: This program uses sprintf to format various
* data and place them in the string named buffer.
*/

#include <stdio.h>
void main( void )
{
char buffer[200], s[] = "computer", c = 'l';
int i = 35, j;
float fp = 1.7320534f;

/* Format and print various data: */
j = sprintf( buffer, "\tString: %s\n", s );
j += sprintf( buffer + j, "\tCharacter: %c\n", c );
j += sprintf( buffer + j, "\tInteger: %d\n", i );
j += sprintf( buffer + j, "\tReal: %f\n", fp );

printf( "Output:\n%s\ncharacter count = %d\n", buffer, j );
}
Output
Output:
String: computer
Character: l
Integer: 35
Real: 1.732053

character count = 71
fibbery 2010-10-17
  • 打赏
  • 举报
回复
晕楼上!
fibbery 2010-10-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 liangqingzhi 的回复:]
sprintf
[/Quote]

sprintf("%x\n",100);
老之 2010-10-17
  • 打赏
  • 举报
回复
sprintf

69,374

社区成员

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

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