/* ITOA.C: This program converts integers of various * sizes to strings in various radixes. */
#include <stdlib.h> #include <stdio.h>
void main( void ) { char buffer[20]; int i = 3445; long l = -344115L; unsigned long ul = 1234567890UL;
_itoa( i, buffer, 10 ); printf( \"String of integer %d (radix 10): %s\\n\", i, buffer ); _itoa( i, buffer, 16 ); printf( \"String of integer %d (radix 16): 0x%s\\n\", i, buffer ); _itoa( i, buffer, 2 ); printf( \"String of integer %d (radix 2): %s\\n\", i, buffer );
_ltoa( l, buffer, 16 ); printf( \"String of long int %ld (radix 16): 0x%s\\n\", l, buffer );
_ultoa( ul, buffer, 16 ); printf( \"String of unsigned long %lu (radix 16): 0x%s\\n\", ul, buffer ); }
Output
String of integer 3445 (radix 10): 3445 String of integer 3445 (radix 16): 0xd75 String of integer 3445 (radix 2): 110101110101 String of long int -344115 (radix 16): 0xfffabfcd String of unsigned long 1234567890 (radix 16): 0x499602d2
du kan bruge enten \"printf()\" eller \"sprintf()\" Den første printer diretke til consol vinduet hvis det altså bare er et simpelt kommandolinje program. Hvis du har vinduer bør du nok brug sprintf() som afleverer resultatet i en streng.
f.eks.
char s[40]; long tal= 255; sprintf(s, \"Her er tallet i hex: %X\", tal);
Så skulle s gerne indeholde \"Her er tallet i hex: FF\"
Du kan selfølgelig også undlade min liodet intelligente tekst og så blot skrive: sprintf(s, \"%X\", tal);
Du kan slå printf og sprintf op i hjælpen eller hvis du har en bog om C. De kan formattere tekst på alle mulige måder, meget nyttige....
Min oventående kommentar var faktisk et svar og klart den letteste måde. Husk at inkludere stdio.h filen som høre til Ansi C og derfor altid vil være tilgængelig uanset compiler...
printer direkte til kommandovinduet som du har startet programmet fra... virker umiddelbart i linux. Hemmeligheden ligger i i %X det store X fortæller printf at den skal konvertere argumentet til hex. Du kan skrive %-et-eller-andet lige så mange gange du vil og så bare supplere med argumenter til sidst, men se nedenfor... der er en del eksempler...
Måske kan du skrive \"man printf\" i din shell og få man siderne på printf, men ellers får du lidt her fra Microsofts hjælpefiler vedrørende C:
printf, wprintf Print formatted output to the standard output stream.
int printf( const char *format [, argument]... );
int wprintf( const wchar_t *format [, argument]... );
Routine Required Header Compatibility printf <stdio.h> ANSI, Win 95, Win NT wprintf <stdio.h> or <wchar.h> ANSI, Win 95, Win NT
For additional compatibility information, see Compatibility in the Introduction.
Return Value
Each of these functions returns the number of characters printed, or a negative value if an error occurs.
Parameters
format
Format control
argument
Optional arguments
Remarks
The printf function formats and prints a series of characters and values to the standard output stream, stdout. If arguments follow the format string, the format string must contain specifications that determine the output format for the arguments. printf and fprintf behave identically except that printf writes output to stdout rather than to a destination of type FILE.
wprintf is a wide-character version of printf; format is a wide-character string. wprintf and printf behave identically otherwise.
Generic-Text Routine Mappings
TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined _tprintf printf printf wprintf
The format argument consists of ordinary characters, escape sequences, and (if arguments follow format) format specifications. The ordinary characters and escape sequences are copied to stdout in order of their appearance. For example, the line
printf(\"Line one\\n\\t\\tLine two\\n\");
produces the output
Line one Line two
Format specifications always begin with a percent sign (%) and are read left to right. When printf encounters the first format specification (if any), it converts the value of the first argument after format and outputs it accordingly. The second format specification causes the second argument to be converted and output, and so on. If there are more arguments than there are format specifications, the extra arguments are ignored. The results are undefined if there are not enough arguments for all the format specifications.
Example
/* PRINTF.C: This program uses the printf and wprintf functions * to produce formatted output. */
/* Display in different radixes. */ printf( \"Digits 10 equal:\\n\\tHex: %i Octal: %i Decimal: %i\\n\", 0x10, 010, 10 );
/* Display characters. */
printf(\"Characters in field (1):\\n%10c%5hc%5C%5lc\\n\", ch, ch, wch, wch); wprintf(L\"Characters in field (2):\\n%10C%5hc%5c%5lc\\n\", ch, ch, wch, wch);
/* Display strings. */
printf(\"Strings in field (1):\\n%25s\\n%25.4hs\\n\\t%S%25.3ls\\n\", string, string, wstring, wstring); wprintf(L\"Strings in field (2):\\n%25S\\n%25.4hs\\n\\t%s%25.3ls\\n\", string, string, wstring, wstring);
Integer formats: Decimal: -9234 Justified: -009234 Unsigned: 4294958062 Decimal -9234 as: Hex: FFFFDBEEh C hex: 0xffffdbee Octal: 37777755756 Digits 10 equal: Hex: 16 Octal: 8 Decimal: 10 Characters in field (1): h h w w Characters in field (2): h h w w Strings in field (1): computer comp Unicode Uni Strings in field (2): computer comp Unicode Uni Real numbers: 251.736600 251.74 2.517366e+002 2.517366E+002
Address as: 0012FFAC
Display to here: 123456789012345678901234567890 Number displayed: 16
Floating-Point Support Routines | Stream I/O Routines | Locale Routines
See Also fopen, fprintf, scanf, sprintf, vprintf Functions
--------------------------- Og de forskellige format specifies... -------------------------------- printf Type Field Characters The type character is the only required format field ; it appears after any optional format fields. The type character determines whether the associated argument is interpreted as a character, string, or number. The types C and S, and the behavior of c and s with printf functions, are Microsoft extensions and are not ANSI-compatible.
Table R.3 printf Type Field Characters
Character Type Output Format c int or wint_t When used with printf functions, specifies a single-byte character; when used with wprintf functions, specifies a wide character. C int or wint_t When used with printf functions, specifies a wide character; when used with wprintf functions, specifies a single-byte character. d int Signed decimal integer. i int Signed decimal integer. o int Unsigned octal integer. u int Unsigned decimal integer. x int Unsigned hexadecimal integer, using “abcdef.” X int Unsigned hexadecimal integer, using “ABCDEF.” e double Signed value having the form [ – ]d.dddd e [sign]ddd where d is a single decimal digit, dddd is one or more decimal digits, ddd is exactly three decimal digits, and sign is + or –. E double Identical to the e format except that E rather than e introduces the exponent. f double Signed value having the form [ – ]dddd.dddd, where dddd is one or more decimal digits. The number of digits before the decimal point depends on the magnitude of the number, and the number of digits after the decimal point depends on the requested precision. g double Signed value printed in f or e format, whichever is more compact for the given value and precision. The e format is used only when the exponent of the value is less than –4 or greater than or equal to the precision argument. Trailing zeros are truncated, and the decimal point appears only if one or more digits follow it. G double Identical to the g format, except that E, rather than e, introduces the exponent (where appropriate). n Pointer to integer Number of characters successfully written so far to the stream or buffer; this value is stored in the integer whose address is given as the argument. p Pointer to void Prints the address pointed to by the argument in the form xxxx:yyyy where xxxx is the segment and yyyy is the offset, and the digits x and y are uppercase hexadecimal digits. s String When used with printf functions, specifies a single-byte–character string; when used with wprintf functions, specifies a wide-character string. Characters are printed up to the first null character or until the precision value is reached. S String When used with printf functions, specifies a wide-character string; when used with wprintf functions, specifies a single-byte–character string. Characters are printed up to the first null character or until the precision value is reached.
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.