hex error

T

Tom St Denis

That really *IS* your problem Bill. Just flailing around, as you do, has
never worked for anyone and it won't work for you either.  You know you have
spent several years at this so you think you know the fundamentals.  But you

He's trolling usenet. Why would you expect any different.

The sad thing really is that some people think a good use of their
spare time is creating a work of art, while others think trolling an
obscure highly technical usenet is a grand old idea.

Tom
 
B

Bill Cunningham

Keith said:
Note that this is not addressed to Bill, but to anyone else who
might be interested. Bill, if you can learn something from it,
that's great, but I'm not holding my breath.

Understanding what your saying seems to sink in. Now when exactly to use
it is a little fuzzy.

Bill
 
O

osmium

Tom said:
He's trolling usenet. Why would you expect any different.

The sad thing really is that some people think a good use of their
spare time is creating a work of art, while others think trolling an
obscure highly technical usenet is a grand old idea.

I guess it's pretty obvious I don't think he is a troll. I can see
programming as being an intriguing thing to look into. Over a period of
years I have had a fascination with Rubik's Cube. My mind wanders from "How
did they build this damn thing - the innards" from "How do I solve it". My
mind just wanders around and I can't decide which problem to even attack.
If I weren't so lazy, I would get several of them and a hammer and answer
the "build" question at least. Instead I just fruitlessly fiddle with it.
Yes, I've looked at the net for help. What I need is a You Tube video with
several initial conditions. Hey, I should do that.

Anyhow I can kind of understand Bill.

I think.

-- osmium
 
K

Keith Thompson

Shao Miller said:
Ben Bacarisse wrote: [...]
Did you intend to print a null character the first time round?

Yes, I did.

Why? If you're printing to a terminal, a null character is likely
to be ignored, but itcould have odd effects. If you're writing to
a file, you'll end up with a corrupted text file.
 
O

osmium

osmium wrote:

< blather snipped>

I forgot to mention. I can't think of a worse way to learn C than from
having only K&R and Usenet as information sources. How could a novice
possibly separate the false leads (some pretty clever) and noise from
actual assistance? I certainly couldn't do that in a new field. I might
detect 90% of the bad stuff - the rest would con me.

But the inability, whatever, to make notes could be corrected. Hey. Maybe
Bill *is* the ultimate troll. I guess I will never know. That "no notes"
syndrome has always bothered me.
 
S

Seebs

But the inability, whatever, to make notes could be corrected. Hey. Maybe
Bill *is* the ultimate troll. I guess I will never know. That "no notes"
syndrome has always bothered me.

I think the most interesting thing is the complete inability to do anything
that even remotely resembles reasoning. There's a process most people can
engage in where they can look at things, talk about their known qualities,
and sort of derive things from this. He can't. He can get about as far as
a vague association between things, that they have some of the same words
in them, and are thus maybe the same.

And the thing is, that's not necessarily trolling; I've seen people like that
before. Some of them are capable of learning how to reason, others don't
seem to be.

I don't think he could make anything that would be usable as notes, because
he can't describe logical relations between things in order to conceive of
a way of categorizing or organizing knowledge. You can see this in the way
that he not only doesn't learn things, but often loses things he used to know.

-s
 
S

Shao Miller

Keith said:
Shao Miller said:
Ben Bacarisse wrote: [...]
Did you intend to print a null character the first time round?
Yes, I did.

Why? If you're printing to a terminal, a null character is likely
to be ignored, but itcould have odd effects. If you're writing to
a file, you'll end up with a corrupted text file.

Fine. Offer "%s" to the original poster and let's find out how well
that goes. ManyBlueNames already mentioned "%s" and arrays, but I
modified my example to avoid it: Not because it's the Right Thing but
for the same reason I might give the example to a pal... Followed-up
with the Right Thing at some point. Perhaps you missed the rest of my
post containing "Yes, I did"?

It's just an example. The original post doesn't even require a colon
delimiter. A good 'print_hex' would probably offer a lot more,
including all the usual variants for arrays, 'FILE *', etc. What you
say about a text file is what I said about redirection. As an aside,
I'm curious as to which terminals exhibit funny business on
'printf("%c", 0);'.
 
T

Tom St Denis

I guess it's pretty obvious I don't think he is a troll.  I can see
programming as being an intriguing thing to look into.  Over a period of
years I have had a fascination with Rubik's Cube.  My mind wanders from "How
did they build this damn thing - the innards" from "How do I solve it".  My
mind just wanders around and I can't decide which problem to even attack.
If I weren't so lazy, I would get several of them and a hammer and answer
the "build" question at least.  Instead I just fruitlessly fiddle with it.
Yes, I've looked at the net for help. What I need is a You Tube video with
several initial conditions.  Hey, I should do that.

Anyhow I can kind of understand Bill.

I think.

What makes Bill a troll is that even after seemingly clear
explanations of the syntax he basically just keeps posting the same
erroneous code.

He's immune to being taught and I don't know if that's by design or by
fault, but frankly I don't care.

Tom
 
B

Bill Cunningham

Shao Miller wrote:

#include <stdio.h>

/* Please use a standard form of 'main'. */
int main()
{
char *p = "JUNK";
/*
* This prints one hex value; the first character
* of the "JUNK" string literal.
*/
printf("%x\n", *p);
/* Please return an 'int' from 'main'. */
}
-----

-----
#include <stdio.h>

/**
* Print the hexadecimal value for each 'char' in a string
* in lower-case hexadecimal. Delimit each value with a colon :)).
* The null character at the end of the string is excluded.
*
* @v str The string to print values for.
* @ret Return 0 for success, 1 for any error.
**/
static int print_hex(const char *str) {
int d = 0, rc = 1;
const unsigned char *ustr = (const unsigned char *)str;

/*
* While 'str' points to a non-null character and
* 'printf' doesn't yield an error.
*/

Here's where I get lost. I've seen people put all kinds of things in
those while parenthesis. I see ustr ?dereferenced? This code will need a lot
of study from me. I don't see it sorry no.
 
S

Shao Miller

Bill said:
Shao Miller wrote:



Here's where I get lost. I've seen people put all kinds of things in
those while parenthesis. I see ustr ?dereferenced? This code will need a lot
of study from me. I don't see it sorry no.

Ah yes. Well it might not help that there's a typo in the comment
above. It should have read "While 'ustr' points to..."

The 'while' loop has a condition which must be true for the 'while' loop
to execute/run. The condition is '*str && rc > 0', as you can see
in-between the parentheses.

The logical AND operator ('&&') actually connects two sub-conditions.

The first sub-condition is '*ustr' which, as you noted, is a
dereference. Since 'ustr' is an 'unsigned char *', then '*ustr' is an
'unsigned char'. So the first sub-condition is whether or not the
'unsigned char' value currently pointed-to by 'ustr' is non-zero.

The second sub-condition is 'rc > 0'. You can see that 'rc' was
declared above and initialized to the value 1.

SO: The 'while' loop will execute if and only if 'ustr' currently points
to a non-zero 'unsigned char' value AND 'rc' has a value greater than 0.

In each iteration of the loop, we change 'rc' to contain the value
yielded by 'printf'. If 'printf' encounters an error (given the
provided format string), it should yield a value <= 0. We store this in
'rc' and it will be checked the next time we check the loop's condition.

To tell the whole truth, it must be mentioned that the second
sub-condition 'rc > 0' is NOT checked if the first sub-condition fails.
That's just the nature of the '&&' operator.

As three others have noted, the use of "%c" in the 'printf' format
string is not really the best if you are actually trying to produce code
for a real situation. If that's your situation, please don't take this
simple, silly (but possibly useful) example code as anything more than
some reference code for discussion, should you care to.
 
K

Kenny McCormack

Highly technical usenet group? Where?

This group is all about the definition of main() and telling people to
do their own research from what I can see.

TSD could certainly have left out the word "Highly" from his
description. It serves no purpose other than to make him look foolish.
If he had just said "technical [newsgroup]", we'd have let it pass.

Oh, and you forgot about not casting the return value of malloc().

And, of course, recovering one's lost manhood...

--
One of the best lines I've heard lately:

Obama could cure cancer tomorrow, and the Republicans would be
complaining that he had ruined the pharmaceutical business.

(Heard on Stephanie Miller = but the sad thing is that there is an awful lot
of direct truth in it. We've constructed an economy in which eliminating
cancer would be a horrible disaster. There are many other such examples.)
 
S

Shao Miller

Richard said:
... ... ...
Highly technical usenet group? Where?

This group is all about the definition of main() and telling people to
do their own research from what I can see.

You're right about suggesting 'main' not being "highly technical". It's
still worth offering, perhaps... By multiple posters and on multiple
occasions, even. It's a nice habit to get into, in my opinion.
 
S

Squeamizh

What makes Bill a troll is that even after seemingly clear
explanations of the syntax he basically just keeps posting the same
erroneous code.

He's immune to being taught and I don't know if that's by design or by
fault, but frankly I don't care.

And yet, you and others continue to swallow his posts hook, line, and
sinker.
 
B

Bill Cunningham

Shao said:
Ah yes. Well it might not help that there's a typo in the comment
above. It should have read "While 'ustr' points to..."

The 'while' loop has a condition which must be true for the 'while'
loop to execute/run. The condition is '*str && rc > 0', as you can
see in-between the parentheses.

The logical AND operator ('&&') actually connects two sub-conditions.

The first sub-condition is '*ustr' which, as you noted, is a
dereference. Since 'ustr' is an 'unsigned char *', then '*ustr' is an
'unsigned char'. So the first sub-condition is whether or not the
'unsigned char' value currently pointed-to by 'ustr' is non-zero.

The second sub-condition is 'rc > 0'. You can see that 'rc' was
declared above and initialized to the value 1.

SO: The 'while' loop will execute if and only if 'ustr' currently
points to a non-zero 'unsigned char' value AND 'rc' has a value
greater than 0.
In each iteration of the loop, we change 'rc' to contain the value
yielded by 'printf'. If 'printf' encounters an error (given the
provided format string), it should yield a value <= 0. We store this
in 'rc' and it will be checked the next time we check the loop's
condition.
To tell the whole truth, it must be mentioned that the second
sub-condition 'rc > 0' is NOT checked if the first sub-condition
fails. That's just the nature of the '&&' operator.


As three others have noted, the use of "%c" in the 'printf' format
string is not really the best if you are actually trying to produce
code for a real situation. If that's your situation, please don't
take this simple, silly (but possibly useful) example code as
anything more than some reference code for discussion, should you
care to.

The thing is I don't use do or while. I use for most of the time. When
someone mentioned "delimiter" the first thing that came to mind for me was
strtok. C's functions I can figure out more simply than its' syntax.

I was experimenting. I don't so much as want to print to stdout the hex
values of ascii JUNK as to store those values in a type (possibly a struct?)
so when that code in a file stream using f*pos or the like comes across the
hex or binary for the ascii JUNK the file position indicator will stop.

Does that make sense?

Bill
 
F

Felix Palmen

* Richard said:
A true "reg" can NEVER resist the urge to tell someone about not casting
malloc or how to define main.

Maybe we should discuss the pro's and con's of casting malloc()'s return
value...
 
O

osmium

osmium said:
I can't think of a worse way to learn C than from having only K&R and
Usenet as information sources.

Not to mention a hideous choice of a first programming language. Pascal was
invented specifically for people with ordinary minds. He will most often get
idiomatic C as a Usenet response.

while(call obscure function with side effects and check the status of the
results of the call)
a++;

<Insert digression that APL would be an even worse language choice here>
 
F

Felix Palmen

* osmium said:
Not to mention a hideous choice of a first programming language. Pascal was
invented specifically for people with ordinary minds. He will most often get
idiomatic C as a Usenet response.

Well .. I don't think the language really matters much here. It's more a
question of the learning material.

Of course, Pascal was designed for learning to program and it probably
does a good job, but I still consider C a good choice, too. The language
is reasonably simple (unlike, e.g., C++) and so, given a competent
teacher or a didactically good book, shouldn't be all that hard to
learn.

I agree this newsgroup is NOT for starters. Most of the discussions here
are about good style and/or what exactly is well-defined by the C
standard and what isn't. Not the things a programming beginner
would/should care about.

Regards,
Felix
 
S

Shao Miller

Bill said:
... ... ...
The thing is I don't use do or while. I use for most of the time.

Ok. There are a few other looping constructs, as well. I've seen lots
of code that uses 'for', so you're not the only one.
When
someone mentioned "delimiter" the first thing that came to mind for me was
strtok. C's functions I can figure out more simply than its' syntax.

Ok.

I was experimenting. I don't so much as want to print to stdout the hex
values of ascii JUNK as to store those values in a type (possibly a struct?)
so when that code in a file stream using f*pos or the like comes across the
hex or binary for the ascii JUNK the file position indicator will stop.

Does that make sense?

I think so. I think that you are saying that you have a text file
(text, right?) full of stuff like:

492048415645204A554E4B20494E204D494E44

And you wish to scan along, reading the file data until you find some
particular sequence. Now you did say "ascii" and "hex or binary". So I
think that you're saying that for the example pattern "JUNK", that you'd
like to stop scanning in a text file when you reach the text:

4A554E4B

or in a text file when you reach the text:

JUNK

That is, both situations. Is that so? If you just mean the latter,
that's a situation that's different than the former. Are you trying to
find several different possible representations for some text as you
scan through a file?
 
S

Seebs

You know this IS an anachronistic example with all reasonably modern
compilers?

The possibility that this example might not rise to the height of
reliable portability across a broad variety of modern implementations
may have flickered briefly across my mind.

-s
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,114
Members
46,702
Latest member
VernitaGow

Latest Threads

Top