hex error

F

Felix Palmen

* Seebs said:
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.

Hehe.

BTW (sorry for not taking this thread seriously any more) -- I found
this little gem in a web forum:

| in std C, there's no need to cast from malloc, but of you are compiling
| your code as C++, it will give an error without the cast being
| present.....this is due to C++ being more type sensitive (it has to be
| due to its ability to define custom types)

Beautiful, isn't it?

Regards,
Felix
 
B

Bill Cunningham

Shao said:
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.


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?

It's a hex dump of an AVI and the signature is RIFF. There seems to be
different sized headers in different files and I want to read all and write
to a text up to JUNK or the hex representation.

Bill
 
O

osmium

Bill Cunningham said:
It's a hex dump of an AVI and the signature is RIFF. There seems to be
different sized headers in different files and I want to read all and
write to a text up to JUNK or the hex representation.

There is a thing known as a "side by side" listing. Hex AND ASCII, you might
try to find one. It is a *program*, a tool, that doesn't care about the
AVI aspect.
 
S

Shao Miller

Bill said:
... ... ...
It's a hex dump of an AVI and the signature is RIFF. There seems to be
different sized headers in different files and I want to read all and write
to a text up to JUNK or the hex representation.

So in a file containing:

njdewJKWDJkdjeudU329DD212JUNKals786DASD786asd73kkbaf
^^^^

You'd wish to read everything until matching "JUNK" and write whatever
came before it to a file? And in a file containing:

njdewJKWDJkdjeudU329DD2124A554E4Bals786DASD786asd73kkbaf
^^^^^^^^

You'd wish to read everything until matching "4A554E4B" and write
whatever came before it to a file?

(Please forgive that both of these examples are not the hexadecimal
dumps that you specified.)
 
I

Ian Collins

This code compiled but I only got one hex for the J and that was it. I
want the string to be printed in hex. What did I do wrong.

Good troll Bill, just look at the size of the tread you spawned.

Is there a troll-o-meter the measures troll quality?
 
D

Default User

I guess it's pretty obvious I don't think he is a troll.

I would suggest digging out some of his really old posts, from 7-8 years
ago. He "knew" more C back then than demonstrates now.

Either he's trolling, or regressing in his knowledge. Either way, it's to no
one's benefit to reply. He's been in my killfile for a long time.



Brian
 
L

luserXtrog

    This code compiled but I only got one hex for the J and that was it. I
want the string to be printed in hex. What did I do wrong.

#include <stdio.h>

int main()
{
    char *p = "JUNK";
    printf("%x\n", *p);

}

Bill

If you're for real, here are two functions which may do what you want
and clarify the situation:


char *p = "JUNK|;

void printhexchar (int c) {
char b[3] = "00";

b[1] = (unsigned char)c / 16;
b[1] += (b[1] < 10)? '0': 'A';

b[0] = (unsigned char)c % 16;
b[0] += (b[0] < 10)? '0': 'A';

printf("%s",b);
}

void printhexstring (char *s) {
while (s) printhexchar(*s++);
}


Also addressed to any noobie lurkers.
 
L

Lew Pitcher

This code compiled but I only got one hex for the J and that was it. I
want the string to be printed in hex. What did I do wrong.

#include <stdio.h>

int main()
{
char *p = "JUNK";
printf("%x\n", *p);

}

Bill

If you're for real, here are two functions which may do what you want
and clarify the situation:


char *p = "JUNK|;

void printhexchar (int c) {
char b[3] = "00";

b[1] = (unsigned char)c / 16;
b[1] += (b[1] < 10)? '0': 'A';

Ahem....
While numeric characters are guaranteed to be contigious and ascending (that
is, '1' is one more than '0', '2' is one more than '1', etc.), alphabetic
characters are _not_.

I don't /know/ of a characterset where 'F' - 'A' != 5, but such
charactersets /are/ permitted under the C standard. Consequently, you're
suggested trinary test isn't strictly "up to code".

Additionally, should b[1] exceed 9, your trinary test will add /that value/
to 'A'. I doubt that you wanted 0xa to be represented by 'A'+10.

[snip]
 
K

Keith Thompson

luserXtrog said:
If you're for real, here are two functions which may do what you want
and clarify the situation:

Given the "If you're for real" clause, I'll assume you aren't trying to
troll by posting deliberately bad code.
char *p = "JUNK|;

| should be "
void printhexchar (int c) {

The entire body of this function could be replaced by:

printf("%02x", (unsigned)c);
char b[3] = "00";

Note that any 2-digit string literal would do. If I were going to
use this approach, I'd probably omit the initialization and add
b[2] = '\0';
at the end. (I'd also assign to b[0] before b[1], but that doesn't
really matter.)
b[1] = (unsigned char)c / 16;
b[1] += (b[1] < 10)? '0': 'A';

b[0] = (unsigned char)c % 16;
b[0] += (b[0] < 10)? '0': 'A';

Lew Pitcher already pointed out the problems with the above.
Another minor point: it probably makes more sense to cast to unsigned
int rather than unsigned char (the result will be promoted anyway).
printf("%s",b);
}

void printhexstring (char *s) {
while (s) printhexchar(*s++);

When will this loop terminate? (s) should be (*s) (or, perhaps more
clearly, (*s != '\0')).
 
S

Seebs

Is there a troll-o-meter the measures troll quality?

There is a canonical measure, though I can't remember now where I saw
it, but some people wrote up a pretty solid scoring system. Bill's quite
good.

-s
 
L

luserXtrog

If you're for real, here are two functions which may do what you want
and clarify the situation:
char *p = "JUNK|;
void printhexchar (int c) {
    char b[3] = "00";
    b[1] = (unsigned char)c / 16;
    b[1] += (b[1] < 10)? '0': 'A';

Ahem....
While numeric characters are guaranteed to be contigious and ascending (that
is, '1' is one more than '0', '2' is one more than '1', etc.), alphabetic
characters are _not_.

I don't /know/ of a characterset where 'F' - 'A' != 5, but such
charactersets /are/ permitted under the C standard. Consequently, you're
suggested trinary test isn't strictly "up to code".

Additionally, should b[1] exceed 9, your trinary test will add /that value/
to 'A'. I doubt that you wanted 0xa to be represented by 'A'+10.

Snap! I suppose my pride persuaded me that my top-of-head,
untested code couldn't be that bad. I did pause over the
ascii assumption, and then blissfully proceded with bad
algebra.
 
L

luserXtrog

<snip some well-deserved schooling>

Tested.



#include <stdio.h>

char *p = "JUNK";

void printhexchar (int c) {
printf("%02x",c);
}

void printhexstring (char *s) {
while (*s) printhexchar(*s++);
}

int main(void) {
printhexstring(p);
puts("");
return 0;
}
 
M

Morris Keesan

void printhexchar (int c) {
printf("%02x",c);
}

void printhexstring (char *s) {
while (*s) printhexchar(*s++);
}

Note that this will give possibly-unexpected results if (CHAR_BIT > 8).
 
L

luserXtrog

Note that this will give possibly-unexpected results if (CHAR_BIT > 8).

ACK.

#include <stdio.h>

char *p = "JUNK";

void printhexchar (unsigned int c) {
printf("%02x",c);
}

void printhexstring (unsigned char *s) {
while (*s) printhexchar(*s++);
}

int main(void) {
printhexstring((unsigned char *)p);
puts("");
return 0;
}

/* eof */
 
L

luserXtrog

This still doesn't change anything.  If CHAR_BIT is over 8, then %02x of
an unsigned char could be two or more digits, not always exactly two.

Oh. I get it now. Like a 9bit lisp machine or one of those 16bit
dsp machines people been talking about. This is where I put the
open can of worms back on the shelf and exit.
 
B

Bill Cunningham

Shao said:
So in a file containing:

njdewJKWDJkdjeudU329DD212JUNKals786DASD786asd73kkbaf
^^^^

You'd wish to read everything until matching "JUNK" and write whatever
came before it to a file? And in a file containing:

njdewJKWDJkdjeudU329DD2124A554E4Bals786DASD786asd73kkbaf
^^^^^^^^

You'd wish to read everything until matching "4A554E4B" and write
whatever came before it to a file?

Exactly.
 
K

Kenny McCormack

Tom St Denis said:
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.

You demonstrate quite clearly the new/current definition of "troll" as
simply "someone who posts stuff that makes me unhappy".

I trust you see how your argument above boils down to "Bill is a troll
because he posts bad things". Completely leaving aside the fact that
words are supposed to have meanings - each word is supposed to refer to
a specific thing. It is poor form to have just a single word to
describe all people who make one unhappy.

--
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.)
 
D

David Thompson

luserXtrog <[email protected]> writes:
The entire body of this function could be replaced by:

printf("%02x", (unsigned)c);
char b[3] = "00";

Note that any 2-digit string literal would do. If I were going to
use this approach, I'd probably omit the initialization and add
b[2] = '\0';
at the end. (I'd also assign to b[0] before b[1], but that doesn't
really matter.)
b[1] = (unsigned char)c / 16;
b[1] += (b[1] < 10)? '0': 'A';

b[0] = (unsigned char)c % 16;
b[0] += (b[0] < 10)? '0': 'A';
Maybe it's just me, but I'd (also?) put the more significant nibble in
b[0] and the less in b[1]. Or if that's what you meant, I don't think
you were clear about it. (Forest for trees maybe?)

<snip rest>
 
L

luserXtrog

<snip>



The entire body of this function could be replaced by:
    printf("%02x", (unsigned)c);
    char b[3] = "00";
Note that any 2-digit string literal would do.  If I were going to
use this approach, I'd probably omit the initialization and add
    b[2] = '\0';
at the end.  (I'd also assign to b[0] before b[1], but that doesn't
really matter.)
    b[1] = (unsigned char)c / 16;
    b[1] += (b[1] < 10)? '0': 'A';
    b[0] = (unsigned char)c % 16;
    b[0] += (b[0] < 10)? '0': 'A';

Maybe it's just me, but I'd (also?) put the more significant nibble in
b[0] and the less in b[1]. Or if that's what you meant, I don't think
you were clear about it. (Forest for trees maybe?)

<snip rest>

Yes. I really double-whammy'd that one.
My guess is that by the end of the line, I'd forgotten
that I was assigning to the array right-to-left.
But at least it has given us something to talk about.

I still don't understand what Bill wanted to do to that
RIFF file.
 

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,780
Messages
2,569,608
Members
45,250
Latest member
Charlesreero

Latest Threads

Top