need to convert a char to an hexadecmial value | | |
Hi,
I needed help in converting a character to the correspoding
hexadecimal values, like the following example,
ASCII value : ABC
Hex Code Value : %41%42%43...
whats the logic of conversion... | | | | re: need to convert a char to an hexadecmial value sam_cit@yahoo.co.in wrote:[color=blue]
> Hi,
> I needed help in converting a character to the correspoding
> hexadecimal values[/color]
Use the following as an example.
Regards,
Ralph
#include <stdio.h>
int main(void)
{
char c = ' ';
char hex[4];
char *hp = &hex[1];
hex[0] = '%';
hex[3] = '\0';
snprintf(hp, 3, "%x", c);
printf("%s\n", hex);
return 0;
}
--
Ralph Moritz
Laugh at your problems; everybody else does. | | | | re: need to convert a char to an hexadecmial value sam_cit@yahoo.co.in wrote:[color=blue]
> Hi,
> I needed help in converting a character to the correspoding
> hexadecimal values, like the following example,
> ASCII value : ABC
> Hex Code Value : %41%42%43...
>
> whats the logic of conversion...[/color]
#include <stdio.h>
#include <stdlib.h>
int main(void) {
char arr[3] = { 'A', 'B', 'C' };
short cnt;
for(cnt = 0; cnt < 3; cnt++)
printf("%%%x ", arr[cnt]);
fflush(stdout);
return 0;
} | | | | re: need to convert a char to an hexadecmial value
On 2006-03-27, sam_cit@yahoo.co.in <sam_cit@yahoo.co.in> wrote:[color=blue]
> Hi,
> I needed help in converting a character to the correspoding
> hexadecimal values, like the following example,
> ASCII value : ABC
> Hex Code Value : %41%42%43...
>
> whats the logic of conversion...
>[/color]
1) learn how to step along a string e.g
for(int i=0;i<strlen(s);i++)
char myChar = s[i];
2) look up in the c reference how to convert values using programs
like printf and sprintf.
3) look up the "%.1s" type format specifier for printf/sprintf
good luck! | | | | re: need to convert a char to an hexadecmial value
Ralph A. Moritz wrote:[color=blue]
> sam_cit@yahoo.co.in wrote:[color=green]
> > Hi,
> > I needed help in converting a character to the correspoding
> > hexadecimal values[/color]
>
> Use the following as an example.
>
> Regards,
> Ralph
>
> #include <stdio.h>
>
> int main(void)
> {
> char c = ' ';
> char hex[4];
> char *hp = &hex[1];
> hex[0] = '%';
> hex[3] = '\0';
>
> snprintf(hp, 3, "%x", c);
> printf("%s\n", hex);
> return 0;
> }[/color]
Why so much complication for such a simple task? Besides it doesn't
quite do what the OP wants. | | | | re: need to convert a char to an hexadecmial value
santosh wrote:[color=blue]
> Why so much complication for such a simple task?[/color]
You're right.; my code is not optimal. Your example is much better :-) | | | | re: need to convert a char to an hexadecmial value
<sam_cit@yahoo.co.in> wrote in message
news:1143462526.673129.75490@j33g2000cwa.googlegro ups.com...[color=blue]
> Hi,
> I needed help in converting a character to the correspoding
> hexadecimal values, like the following example,
> ASCII value : ABC
> Hex Code Value : %41%42%43...
>
> whats the logic of conversion...[/color]
There is actually no conversion performed. You may choose to view the value
stored in: char a = 'A'; as a hexadecimal number. You can also print the
corresponding character to this value in your character set, which you told
us is ASCII. The thing is the value resides there, it is a matter of how you
choose to interpret it. | | | | re: need to convert a char to an hexadecmial value sam_cit@yahoo.co.in wrote:[color=blue]
> Hi,
> I needed help in converting a character to the correspoding
> hexadecimal values, like the following example,
> ASCII value : ABC
> Hex Code Value : %41%42%43...[/color]
#include <stdio.h>
int main(void)
{
unsigned char src[] = "ABC", *t;
printf("For this implementation, the string \"%s\"\n"
"has the encoding: \n", src);
for (t = src; *t; t++)
printf("%%%x", *t);
putchar('\n');
return 0;
}
[output]
For this implementation, the string "ABC"
has the encoding:
%41%42%43
[color=blue]
> whats the logic of conversion...[/color]
What conversion? | | | | re: need to convert a char to an hexadecmial value
santosh wrote:
[color=blue]
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main(void) {
> char arr[3] = { 'A', 'B', 'C' };
> short cnt;
>
> for(cnt = 0; cnt < 3; cnt++)
> printf("%%%x ", arr[cnt]);
>
> fflush(stdout);[/color]
This is, unfortunately, not sufficient. To have portably defined
behavior the last line of output must end with an end-of-line character
('\n'). fflush() does not accomplish this.[color=blue]
> return 0;
> }[/color] | | | | re: need to convert a char to an hexadecmial value
Martin Ambuhl wrote:[color=blue]
> santosh wrote:
>[color=green]
> >
> > #include <stdio.h>
> > #include <stdlib.h>
> >
> > int main(void) {
> > char arr[3] = { 'A', 'B', 'C' };
> > short cnt;
> >
> > for(cnt = 0; cnt < 3; cnt++)
> > printf("%%%x ", arr[cnt]);
> >
> > fflush(stdout);[/color]
>
> This is, unfortunately, not sufficient. To have portably defined
> behavior the last line of output must end with an end-of-line character
> ('\n'). fflush() does not accomplish this.[/color]
Is a NL sequence needed at the end of every invocation of an output
function or is one after a sequence, (possibly interleaved with other
statements), of such calls sufficient? | | | | re: need to convert a char to an hexadecmial value
santosh wrote:[color=blue]
> Martin Ambuhl wrote:
>[color=green]
>>santosh wrote:
>>
>>[color=darkred]
>>>#include <stdio.h>
>>>#include <stdlib.h>
>>>
>>>int main(void) {
>>> char arr[3] = { 'A', 'B', 'C' };
>>> short cnt;
>>>
>>> for(cnt = 0; cnt < 3; cnt++)
>>> printf("%%%x ", arr[cnt]);
>>>
>>> fflush(stdout);[/color]
>>
>>This is, unfortunately, not sufficient. To have portably defined
>>behavior the last line of output must end with an end-of-line character
>>('\n'). fflush() does not accomplish this.[/color]
>
>
> Is a NL sequence needed at the end of every invocation of an output
> function or is one after a sequence, (possibly interleaved with other
> statements), of such calls sufficient?[/color]
Just exactly what I said: "To have portably defined
behavior the last line of output must end with an end-of-line character
('\n')." The last line is not "every invocation." | | | | re: need to convert a char to an hexadecmial value
"santosh" <santosh.k83@gmail.com> writes:[color=blue]
> sam_cit@yahoo.co.in wrote:[color=green]
>> Hi,
>> I needed help in converting a character to the correspoding
>> hexadecimal values, like the following example,
>> ASCII value : ABC
>> Hex Code Value : %41%42%43...
>>
>> whats the logic of conversion...[/color]
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main(void) {
> char arr[3] = { 'A', 'B', 'C' };
> short cnt;
>
> for(cnt = 0; cnt < 3; cnt++)
> printf("%%%x ", arr[cnt]);
>
> fflush(stdout);
> return 0;
> }[/color]
You don't use anything from <stdlib.h>.
That prints the hexadecimal values rather than converting them, but
the original problem statement wasn't very clear so it's probably ok.
(Converting to a string would require some moderately complex memory
management.)
For characters with values less than 16, you print a single digit,
e.g., "%f" rather than "%f". Again, the problem statement wasn't
clear on this point.
You print a spaces between the characters, which is inconsistent with
the example.
Why do you use type short for the array index? It typically saves
only an insigificant amount of data space, and the resulting code
could be larger and slower on many systems. Just use int.
The "%x" format expects an unsigned int; you're giving it a char.
It's likely to work anyway, but it could cause problems -- and proving
that it does what you want is a lot more work than just fixing the
code. This is one of those rare cases where a cast is actually
appropriate.
3 is a magic number (not a huge deal in a snippet like this).
The output isn't terminated by a new-line, so it's not guaranteed to
appear even with the fflush(stdout) (and the standard is unclear on
just what can go wrong).
#include <stdio.h>
int main(void)
{
char arr[3] = { 'A', 'B', 'C' };
const int arr_len = sizeof(arr) / sizeof(arr[0]);
int i;
for (i = 0; i < arr_len; i++) {
printf("%%%02x", (unsigned int)arr[i]);
}
putchar('\n');
return 0;
}
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this. | | | | re: need to convert a char to an hexadecmial value
"Richard G. Riley" <rgrdev@gmail.com> writes:[color=blue]
> On 2006-03-27, sam_cit@yahoo.co.in <sam_cit@yahoo.co.in> wrote:[color=green]
>> Hi,
>> I needed help in converting a character to the correspoding
>> hexadecimal values, like the following example,
>> ASCII value : ABC
>> Hex Code Value : %41%42%43...
>>
>> whats the logic of conversion...
>>[/color]
>
> 1) learn how to step along a string e.g
>
> for(int i=0;i<strlen(s);i++)
> char myChar = s[i];[/color]
That re-evaluates strlen(s) on each iteration, making the loop
O(N**2) rather than O(N).
Declaring a variable in a for loop is a new feature in C99; not all
compilers support it.
[color=blue]
> 2) look up in the c reference how to convert values using programs
> like printf and sprintf.[/color]
Functions, not "programs".
[color=blue]
> 3) look up the "%.1s" type format specifier for printf/sprintf[/color]
It prints the first character of the corresponding string argument.
It's simpler just to pass the character itself and use "%c" -- or to
use putchar(). Even so, I don't see how that would be useful in this
context.
[color=blue]
> good luck![/color]
Indeed.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this. | | | | re: need to convert a char to an hexadecmial value
On 2006-03-27, santosh <santosh.k83@gmail.com> wrote:[color=blue]
> Martin Ambuhl wrote:[color=green]
>> santosh wrote:
>>[color=darkred]
>> >
>> > #include <stdio.h>
>> > #include <stdlib.h>
>> >
>> > int main(void) {
>> > char arr[3] = { 'A', 'B', 'C' };
>> > short cnt;
>> >
>> > for(cnt = 0; cnt < 3; cnt++)
>> > printf("%%%x ", arr[cnt]);
>> >
>> > fflush(stdout);[/color]
>>
>> This is, unfortunately, not sufficient. To have portably defined
>> behavior the last line of output must end with an end-of-line character
>> ('\n'). fflush() does not accomplish this.[/color]
>
> Is a NL sequence needed at the end of every invocation of an output
> function or is one after a sequence, (possibly interleaved with other
> statements), of such calls sufficient?[/color]
It's needed before the end of all output (that is, before the file is
closed or the program exits) to a text stream. | | | | re: need to convert a char to an hexadecmial value
"Martin Ambuhl" <mambuhl@earthlink.net> wrote in message
news:SjUVf.18435$S25.8226@newsread1.news.atl.earth link.net...[color=blue]
> sam_cit@yahoo.co.in wrote:[color=green]
> > Hi,
> > I needed help in converting a character to the correspoding
> > hexadecimal values, like the following example,
> > ASCII value : ABC
> > Hex Code Value : %41%42%43...[/color]
>
>
> #include <stdio.h>
> int main(void)
> {
> unsigned char src[] = "ABC", *t;
> printf("For this implementation, the string \"%s\"\n"
> "has the encoding: \n", src);
> for (t = src; *t; t++)
> printf("%%%x", *t);
> putchar('\n');
> return 0;
> }
>
> [output]
> For this implementation, the string "ABC"
> has the encoding:
> %41%42%43
>
>[color=green]
> > whats the logic of conversion...[/color]
>
> What conversion?
>[/color]
Martin, _fix_ your quoting. You merged your reply with to Santosh's
unquoted statements. You're missing Santosh's message header. | | | | re: need to convert a char to an hexadecmial value
On 2006-03-27, Keith Thompson <kst-u@mib.org> wrote:[color=blue]
> "Richard G. Riley" <rgrdev@gmail.com> writes:[color=green]
>> On 2006-03-27, sam_cit@yahoo.co.in <sam_cit@yahoo.co.in> wrote:[color=darkred]
>>> Hi,
>>> I needed help in converting a character to the correspoding
>>> hexadecimal values, like the following example,
>>> ASCII value : ABC
>>> Hex Code Value : %41%42%43...
>>>
>>> whats the logic of conversion...
>>>[/color]
>>
>> 1) learn how to step along a string e.g
>>
>> for(int i=0;i<strlen(s);i++)
>> char myChar = s[i];[/color]
>
> That re-evaluates strlen(s) on each iteration, making the loop
> O(N**2) rather than O(N).[/color]
While any decent compiler ...., you're right. How about i=0;s[i];i++?
[color=blue]
> Declaring a variable in a for loop is a new feature in C99; not all
> compilers support it.[/color]
Many do even without full c99 support, though, mainly as a consequence
of also being C++ compilers. The same applies to //comments, though
those are bad in code to be posted on this group for other reasons.
[color=blue]
>[color=green]
>> 2) look up in the c reference how to convert values using programs
>> like printf and sprintf.[/color]
>
> Functions, not "programs".[/color]
potato, potato.
[color=blue][color=green]
>> 3) look up the "%.1s" type format specifier for printf/sprintf[/color]
>
> It prints the first character of the corresponding string argument.
> It's simpler just to pass the character itself and use "%c" -- or to
> use putchar(). Even so, I don't see how that would be useful in this
> context.
>[color=green]
>> good luck![/color]
>
> Indeed.[/color]
For extra credit, modify your program to do URL encoding, that is,
encode only characters that need escaping (which ones need to be escaped
is left as an exercise for the reader) and encode 040 as '+'. | | | | re: need to convert a char to an hexadecmial value
"santosh" <santosh.k83@gmail.com> writes:[color=blue]
> Martin Ambuhl wrote:[color=green]
>> santosh wrote:
>>[color=darkred]
>> >
>> > #include <stdio.h>
>> > #include <stdlib.h>
>> >
>> > int main(void) {
>> > char arr[3] = { 'A', 'B', 'C' };
>> > short cnt;
>> >
>> > for(cnt = 0; cnt < 3; cnt++)
>> > printf("%%%x ", arr[cnt]);
>> >
>> > fflush(stdout);[/color]
>>
>> This is, unfortunately, not sufficient. To have portably defined
>> behavior the last line of output must end with an end-of-line character
>> ('\n'). fflush() does not accomplish this.[/color]
>
> Is a NL sequence needed at the end of every invocation of an output
> function or is one after a sequence, (possibly interleaved with other
> statements), of such calls sufficient?[/color]
The new-line is required at the end of the last line of output, before
the file is closed. For stdout, this means before your program
terminates (or before you close stdout explicitly, but you probably
don't want to do that).
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this. | | | | re: need to convert a char to an hexadecmial value
Rod Pemberton schrieb:[color=blue]
> "Martin Ambuhl" <mambuhl@earthlink.net> wrote in message
> news:SjUVf.18435$S25.8226@newsread1.news.atl.earth link.net...[color=green]
>>sam_cit@yahoo.co.in wrote:[/color][/color]
[snip!][color=blue]
> Martin, _fix_ your quoting. You merged your reply with to Santosh's
> unquoted statements. You're missing Santosh's message header.[/color]
Martin Ambuhl's post is a direct answer to the OP as can
be seen from the message headers. santosh was never in
between here. You probably mixed that up with another
subthread.
-Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address. | | | | re: need to convert a char to an hexadecmial value
"Rod Pemberton" <do_not_have@sorry.bitbuck.cmm> writes:
[...][color=blue]
> Martin, _fix_ your quoting. You merged your reply with to Santosh's
> unquoted statements. You're missing Santosh's message header.[/color]
No, Martin wasn't replying to santosh and didn't quote anything he
said.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this. | | | | re: need to convert a char to an hexadecmial value
Jordan Abel <random832@gmail.com> writes:[color=blue]
> On 2006-03-27, Keith Thompson <kst-u@mib.org> wrote:[color=green]
>> "Richard G. Riley" <rgrdev@gmail.com> writes:[/color][/color]
[...][color=blue][color=green][color=darkred]
>>> for(int i=0;i<strlen(s);i++)
>>> char myChar = s[i];[/color]
>>
>> That re-evaluates strlen(s) on each iteration, making the loop
>> O(N**2) rather than O(N).[/color]
>
> While any decent compiler ...., you're right. How about i=0;s[i];i++?[/color]
Sure (though I'd write "s[i] != '\0'"), or use a pointer, or compute
strlen() outside the loop. (The latter does a single unnecessary
traversal of the string, which isn't nearly as bad as doing N
unnecessary traversals.)
[color=blue][color=green]
>> Declaring a variable in a for loop is a new feature in C99; not all
>> compilers support it.[/color]
>
> Many do even without full c99 support, though, mainly as a consequence
> of also being C++ compilers. The same applies to //comments, though
> those are bad in code to be posted on this group for other reasons.[/color]
Many != All. Using C99-specific feature, even ones that are widely
implemented, limits the portability of your code. If you're willing
to accept that, that's fine, but you should be aware of it, and you
should know how to avoid the problem if you need to (in this case, by
declaring the variable separately).
[color=blue][color=green][color=darkred]
>>> 2) look up in the c reference how to convert values using programs
>>> like printf and sprintf.[/color]
>>
>> Functions, not "programs".[/color]
>
> potato, potato.[/color]
Would you rather call printf and sprintf "potatoes"?
The word "program" has a specific meaning. If you use the words
interchangeably, how are you going to explain the difference between
exit() and return when used outside main()? Using words correctly is
important, especially when communicating with newbies.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this. | | | | re: need to convert a char to an hexadecmial value
On 27 Mar 2006 19:57:27 GMT, in comp.lang.c , Jordan Abel
<random832@gmail.com> wrote:
[color=blue]
>On 2006-03-27, Keith Thompson <kst-u@mib.org> wrote:[color=green]
>>
>> Functions, not "programs".[/color]
>
>potato, potato.[/color]
More like potatoe, potato, Mr Vice President.
Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan | | | | re: need to convert a char to an hexadecmial value
"Michael Mair" <Michael.Mair@invalid.invalid> wrote in message
news:48qvcmFllur9U1@individual.net...[color=blue]
> Rod Pemberton schrieb:[color=green]
> > "Martin Ambuhl" <mambuhl@earthlink.net> wrote in message
> > news:SjUVf.18435$S25.8226@newsread1.news.atl.earth link.net...[color=darkred]
> >>sam_cit@yahoo.co.in wrote:[/color][/color]
> [snip!][color=green]
> > Martin, _fix_ your quoting. You merged your reply with to Santosh's
> > unquoted statements. You're missing Santosh's message header.[/color]
>
> Martin Ambuhl's post is a direct answer to the OP as can
> be seen from the message headers. santosh was never in
> between here. You probably mixed that up with another
> subthread.
>[/color]
Sorry, it appears there is something wrong with the server I'm connected to.
I'll start cross checking with another for a while. | | | | re: need to convert a char to an hexadecmial value
Keith Thompson wrote:[color=blue]
> "santosh" <santosh.k83@gmail.com> writes:[color=green]
> > sam_cit@yahoo.co.in wrote:[color=darkred]
> >> Hi,
> >> I needed help in converting a character to the correspoding
> >> hexadecimal values, like the following example,
> >> ASCII value : ABC
> >> Hex Code Value : %41%42%43...
> >>
> >> whats the logic of conversion...[/color]
> >
> > #include <stdio.h>
> > #include <stdlib.h>
> >
> > int main(void) {
> > char arr[3] = { 'A', 'B', 'C' };
> > short cnt;
> >
> > for(cnt = 0; cnt < 3; cnt++)
> > printf("%%%x ", arr[cnt]);
> >
> > fflush(stdout);
> > return 0;
> > }[/color]
>
> You don't use anything from <stdlib.h>.[/color]
I thought, obviously wrongly, that fflush() was declared in stdlib.h
[color=blue]
> That prints the hexadecimal values rather than converting them, but
> the original problem statement wasn't very clear so it's probably ok.
> (Converting to a string would require some moderately complex memory
> management.)[/color]
And I felt that an array based example would be simpler to understand
for the OP.
[color=blue]
> For characters with values less than 16, you print a single digit,
> e.g., "%f" rather than "%f". Again, the problem statement wasn't
> clear on this point.
>
> You print a spaces between the characters, which is inconsistent with
> the example.[/color]
Yes, I hope the OP can forgive me, modify it as necessary.
[color=blue]
> Why do you use type short for the array index? It typically saves
> only an insigificant amount of data space, and the resulting code
> could be larger and slower on many systems. Just use int.[/color]
Okay.
[color=blue]
> The "%x" format expects an unsigned int; you're giving it a char.
> It's likely to work anyway, but it could cause problems -- and proving
> that it does what you want is a lot more work than just fixing the
> code. This is one of those rare cases where a cast is actually
> appropriate.[/color]
Alternatively, the array could have been declared as unsigned int,
though that would be gratituous waste of memory.
[color=blue]
> 3 is a magic number (not a huge deal in a snippet like this).[/color]
Yes. Should have coded it cleaner.
[color=blue]
> The output isn't terminated by a new-line, so it's not guaranteed to
> appear even with the fflush(stdout) (and the standard is unclear on
> just what can go wrong).[/color]
I was thus far under the impression that fflush(stdout) was equivalent,
(in terms of writing buffered output), to a newline character. If
fflush(stdout) is not guaranteed to do it's job, then why define it,
atleast for stdout?
[color=blue]
> #include <stdio.h>
> int main(void)
> {
> char arr[3] = { 'A', 'B', 'C' };
> const int arr_len = sizeof(arr) / sizeof(arr[0]);
> int i;
>
> for (i = 0; i < arr_len; i++) {
> printf("%%%02x", (unsigned int)arr[i]);
> }
> putchar('\n');
> return 0;
> }[/color]
Thanks for this improvement. | | | | re: need to convert a char to an hexadecmial value
"santosh" <santosh.k83@gmail.com> writes:[color=blue]
> Keith Thompson wrote:[/color]
[...][color=blue][color=green]
>> The output isn't terminated by a new-line, so it's not guaranteed to
>> appear even with the fflush(stdout) (and the standard is unclear on
>> just what can go wrong).[/color]
>
> I was thus far under the impression that fflush(stdout) was equivalent,
> (in terms of writing buffered output), to a newline character. If
> fflush(stdout) is not guaranteed to do it's job, then why define it,
> atleast for stdout?[/color]
Here's what the standard says:
A text stream is an ordered sequence of characters composed into
lines, each line consisting of zero or more characters plus a
terminating new-line character. Whether the last line requires a
terminating new-line character is implementation-defined.
And here's the description of fflush():
If stream points to an output stream or an update stream in which
the most recent operation was not input, the fflush function
causes any unwritten data for that stream to be delivered to the
host environment to be written to the file; otherwise, the
behavior is undefined.
fflush() will cause a partial line to be written to the output file
(or at least "the host environment to be written to the file", which
may not be the same thing), but if the file is then closed without a
new-line, you could still have an ill-formed text file.
Closing a file flushes it anyway, so an fflush() just before the end
of the program doesn't do any good.
fflush() is commonly used when you want to make sure that your output
appears. For example:
printf("Here's a prompt: ");
fflush(stdout);
fgets(answer);
Without the fflush() the prompt won't necessarily appear in time to be
useful. Another example:
fprintf(LOG_FILE, "About to do something dangerous\n");
fflush(LOG_FILE);
do_something_dangerous();
Here, if do_something_dangerous() causes the program to crash badly
enough, the message might never be written. <OT>It can also ensure
that the message will be visible to another process.</OT>
None of this is really guaranteed, since there can still be additional
layers of buffering in the operating system.
[color=blue]
> Thanks for this improvement.[/color]
No problem.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this. | | | | re: need to convert a char to an hexadecmial value
On 2006-03-27, Keith Thompson <kst-u@mib.org> wrote:[color=blue]
> Jordan Abel <random832@gmail.com> writes:[color=green]
>> On 2006-03-27, Keith Thompson <kst-u@mib.org> wrote:[color=darkred]
>>> "Richard G. Riley" <rgrdev@gmail.com> writes:[/color][/color]
> [...][color=green][color=darkred]
>>>> for(int i=0;i<strlen(s);i++)
>>>> char myChar = s[i];
>>>
>>> That re-evaluates strlen(s) on each iteration, making the loop
>>> O(N**2) rather than O(N).[/color]
>>
>> While any decent compiler ...., you're right. How about i=0;s[i];i++?[/color]
>
> Sure (though I'd write "s[i] != '\0'"), or use a pointer, or compute
> strlen() outside the loop. (The latter does a single unnecessary
> traversal of the string, which isn't nearly as bad as doing N
> unnecessary traversals.)
>[color=green][color=darkred]
>>> Declaring a variable in a for loop is a new feature in C99; not all
>>> compilers support it.[/color]
>>
>> Many do even without full c99 support, though, mainly as a consequence
>> of also being C++ compilers. The same applies to //comments, though
>> those are bad in code to be posted on this group for other reasons.[/color]
>
> Many != All. Using C99-specific feature, even ones that are widely
> implemented, limits the portability of your code. If you're willing
> to accept that, that's fine, but you should be aware of it, and you
> should know how to avoid the problem if you need to (in this case, by
> declaring the variable separately).
>[color=green][color=darkred]
>>>> 2) look up in the c reference how to convert values using programs
>>>> like printf and sprintf.
>>>
>>> Functions, not "programs".[/color]
>>
>> potato, potato.[/color]
>
> Would you rather call printf and sprintf "potatoes"?[/color]
"potato, potato" is pronounced as /p@teIto p@tAto/ i.e. "ay" and "ah"
sounds for the 'a'.
And I was joking, of course there's a different. There are also
differences between functions and subroutines, but we call them all
'functions', even the ones that aren't. | | | | re: need to convert a char to an hexadecmial value
On 2006-03-27, Mark McIntyre <markmcintyre@spamcop.net> wrote:[color=blue]
> On 27 Mar 2006 19:57:27 GMT, in comp.lang.c , Jordan Abel
> <random832@gmail.com> wrote:
>[color=green]
>>On 2006-03-27, Keith Thompson <kst-u@mib.org> wrote:[color=darkred]
>>>
>>> Functions, not "programs".[/color]
>>
>>potato, potato.[/color]
>
> More like potatoe, potato, Mr Vice President.[/color]
I was thinking of "potayto potahto" :P | | | | re: need to convert a char to an hexadecmial value
Jordan Abel <random832@gmail.com> writes:[color=blue]
> On 2006-03-27, Keith Thompson <kst-u@mib.org> wrote:[color=green]
>> Jordan Abel <random832@gmail.com> writes:[color=darkred]
>>> On 2006-03-27, Keith Thompson <kst-u@mib.org> wrote:
>>>> "Richard G. Riley" <rgrdev@gmail.com> writes:[/color][/color][/color]
[...][color=blue][color=green][color=darkred]
>>>>> 2) look up in the c reference how to convert values using programs
>>>>> like printf and sprintf.
>>>>
>>>> Functions, not "programs".
>>>
>>> potato, potato.[/color]
>>
>> Would you rather call printf and sprintf "potatoes"?[/color]
>
> "potato, potato" is pronounced as /p@teIto p@tAto/ i.e. "ay" and "ah"
> sounds for the 'a'.
>
> And I was joking, of course there's a different. There are also
> differences between functions and subroutines, but we call them all
> 'functions', even the ones that aren't.[/color]
Yeah, I got the joke. The point of the joke is that there's no
practical difference between "potayto" and "potahto". There's a very
real difference between "function" and "program" (and the C standard
doesn't even use the word "subroutine".)
What exactly was your point?
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this. | | | | re: need to convert a char to an hexadecmial value
Keith Thompson wrote:
[color=blue]
> Jordan Abel <random832@gmail.com> writes:[color=green]
> > On 2006-03-27, Keith Thompson <kst-u@mib.org> wrote:[color=darkred]
> >> Jordan Abel <random832@gmail.com> writes:[/color][/color][/color]
[color=blue][color=green]
> > "potato, potato" is pronounced as /p@teIto p@tAto/ i.e. "ay" and
> > "ah" sounds for the 'a'.
> >
> > And I was joking, of course there's a different. There are also
> > differences between functions and subroutines, but we call them all
> > 'functions', even the ones that aren't.[/color]
>
> Yeah, I got the joke. The point of the joke is that there's no
> practical difference between "potayto" and "potahto". There's a very
> real difference between "function" and "program" (and the C standard
> doesn't even use the word "subroutine".)
>
> What exactly was your point?[/color]
Let's call the whole thing off.
Well, somebody had to do it.
Brian |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|