hex error

B

Bill Cunningham

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
 
T

Tom St Denis

    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);

What type is '*p'?

is *p == 'J' true?

Tom
 
F

Fred

    *p is a pointer type. But I want the conversion to hex too. I will
experiment more.


No, *p is not a pointer. 'p' is a pointer-to-char. The type of *p is
'char'.
 
T

Tom St Denis

    *p is a pointer type. But I want the conversion to hex too. I will
experiment more.

No, *p is not a pointer. "char *p" declares a pointer, but
dereferencing it produces its base type.

Hence my comment about "is *p == 'J' true?" which you failed to
answer.

So then my question to you now is, do you enjoy trolling usenet? Does
it make you feel better? Is this how you pass your spare time?
Please, go outside and enjoy life.

Tom
 
B

Bill Cunningham

Tom said:
No, *p is not a pointer. "char *p" declares a pointer, but
dereferencing it produces its base type.

Hence my comment about "is *p == 'J' true?" which you failed to
answer.

I don't know the answer to your second question for sure but I would say
yes.

Your third question is an emotional non-C question not worth responding
to.

Bill
 
B

Bill Cunningham

Fred said:
No, *p is not a pointer. 'p' is a pointer-to-char. The type of *p is
'char'.

This is really getting me stumped. The code I try either doesn't compile
or prints garbage.

Bill
 
G

GET OUT

Bill said:
#include<stdio.h>

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

#include <stdio.h>
#define GET int main(void){for(char*p="JUNK";*p!='\0'&&
#define OUT printf("0x%x\n",*p);p++);return 0;}
GET OUT
 
T

Tom St Denis

    I don't know the answer to your second question for sure but I would say
yes.

Then why would you expect printf("%x\n", 'J') to not print out the hex
code for 'J'?

I'm confused as to why you can see the equivalence in terms of the
value of the variable but not the equivalence in the outcome.

It's like somehow you expect

int a, b;
a = 1;
b = 1;

if (a == 1) { ... }

and

if (a == b) { ... }

To produce different results in the comparison...

Tom
 
B

Bill Cunningham

Tom St Denis wrote:

[...]
Then why would you expect printf("%x\n", 'J') to not print out the hex
code for 'J'?
[...]

I think I might be seeing what your saying now. Maybe I will try
something other than a string.

Bill
 
B

Bill Cunningham

Tom said:
Then why would you expect printf("%x\n", 'J') to not print out the hex
code for 'J'?

I'm confused as to why you can see the equivalence in terms of the
value of the variable but not the equivalence in the outcome.

It's like somehow you expect

int a, b;
a = 1;
b = 1;

if (a == 1) { ... }

and

if (a == b) { ... }

To produce different results in the comparison...

Tom

I tried this code and it seems a little messy to me but it worked.

#include <stdio.h>

main()
{
printf("%x%x%x%x\n", 'J', 'U', 'N', 'K');
}

Bill
 
T

Tom St Denis

    I tried this code and it seems a little messy to me but it worked..

#include <stdio.h>

main()
{
    printf("%x%x%x%x\n", 'J', 'U', 'N', 'K');

}

You really need to learn how to step through an array...

Tom
 
K

Keith Thompson

The China Blue Lagoon said:
*p loads the first character, converts it to an integer, and makes the character
code available to the %x formatter.

Not exactly. *p is of type char, so it's *already* an integer.

Because it's passed as an argument to a variadic function, it's promote
from char to int (note the *very* important distinction between
"integer" and "int").

Digression: On some exotic systems it might be promoted to unsigned int.
Specifically, this happens if CHAR_MAX > INT_MAX. This can occur only
if plain char is unsigned and sizeof(int)==1; the latter can only occur
if CHAR_BIT >= 16. You're unlikely to run into this in practice, and
even if you do it's likely to be harmless.

Note that "%x" expects an unsigned int, not an int. There are rules
that make it very likely that this will work as expected, but I'd still
use an implicit conversion -- which also avoids problems on the "exotic
systems" I just mentioned.

printf("%x\n", (unsigned)*p);

It's much easier to ensure that the argument is of the right type
than to work through all the special cases to ensure that it doesn't
need to be. Note that arguments to variadic functions are one of
the few contexts where casts (explicit conversions) are actually
a good idea.
You have to make each character available to the formatter separately,
printf("%x%x%x%x\n",p[0],p[1],p[2],p[3]);

Note that that can give you ambiguous results in some cases.
For small values, "%x" gives you only one character of output.
If the above gives you "1234567", you can't tell which of the 4
values was only one hex digit wide. You can use "%02x" to avoid
this problem, giving, for example, "12345607". This assumes 8-bit
bytes (4 bits for each of 2 hex digits).
Other than %s, C doesn't have standard formatters to print arrays. You can
explicitly enumerate each element as above, or use a loop
for (k=0; *p; k++) printf("%x", p[k]);

This loop will continue to execute as long as *p is non-zero.
Since you don't modify p, that will be a very long time -- or until
your program crashes.

for (k = 0; p[k] != '\0'; k ++) {
printf("%x", p[k]);
}
printf("\n");

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.
 
S

Shao Miller

Bill said:
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);
}
-----
#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.
*/
while (*ustr && rc > 0) {
rc = printf("%c%x", d, *ustr);
/* Use delimiter every time after the first time. */
d = ':';
/* Point to the next character. */
ustr++;
}
if (rc < 1)
/* Error. */
return 1;

if (putchar('\n') == EOF)
/* Error. */
return 1;

/* Success. */
return 0;
}

int main(void) {
char *p = "JUNK";

print_hex(p);
return 0;
}
 
B

Ben Bacarisse

Shao Miller said:
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.
*/
while (*ustr && rc > 0) {
rc = printf("%c%x", d, *ustr);

Did you intend to print a null character the first time round? I'd use

char *d = "";

and a %s format here.
/* Use delimiter every time after the first time. */
d = ':';

(so then I'd need d = ":"; here)
/* Point to the next character. */
ustr++;
}

<snip>
 
M

Mark Bluemel

Richard said:
You turned your computer on too quickly and all your hex bytes got
stuck. Try dipping it in an oil bath and giving it a good shake. I find
that works.
I think his problem is that he hasn't made enough random changes yet.
 
S

Shao Miller

Ben said:
Did you intend to print a null character the first time round?

Yes, I did.
I'd use

char *d = "";

and a %s format here.


(so then I'd need d = ":"; here)


<snip>

Yes, what you suggest is the usual means for this sort of thing, as far
as I know. Agreed.

My instinct is that the "%c" format might be a fair example for the
original poster, given the challenges posed by "%x". In this silly
example, we use the standard output only. I've no idea if the original
poster intends to redirect standard output or not, but I wasn't betting
on it. :)
 
O

osmium

I think his problem is that he hasn't made enough random changes yet.

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
*don't* know the fundamentals. To resume your self education, you should do
the first program after "hello world" and go from there.

K&R is much too terse for you and, I would say, most people. It's written
for people who already have a certain amount of experience. Have you
replaced K&R with a more suitable book yet?

"Total immersion" doesn't work for programming.
 

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

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top