need to convert a char to an hexadecmial value

S

sam_cit

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

Ralph A. Moritz

Hi,
I needed help in converting a character to the correspoding
hexadecimal values

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;
}
 
S

santosh

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

#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;
}
 
R

Richard G. Riley

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

1) learn how to step along a string e.g

for(int i=0;i<strlen(s);i++)
char myChar = s;

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

santosh

Ralph said:
Hi,
I needed help in converting a character to the correspoding
hexadecimal values

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

Why so much complication for such a simple task? Besides it doesn't
quite do what the OP wants.
 
S

stathis gotsis

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

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

Martin Ambuhl

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


#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

whats the logic of conversion...

What conversion?
 
M

Martin Ambuhl

santosh said:
#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);

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

santosh

Martin said:
santosh said:
#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);

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.

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?
 
M

Martin Ambuhl

santosh said:
Martin said:
santosh wrote:

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

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.


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?

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."
 
K

Keith Thompson

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

#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;
}

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);
}
putchar('\n');
return 0;
}
 
K

Keith Thompson

Richard G. Riley said:
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...

1) learn how to step along a string e.g

for(int i=0;i<strlen(s);i++)
char myChar = s;


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.
2) look up in the c reference how to convert values using programs
like printf and sprintf.

Functions, not "programs".
3) look up the "%.1s" type format specifier for printf/sprintf

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.
good luck!

Indeed.
 
J

Jordan Abel

Martin said:
santosh said:
#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);

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.

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?

It's needed before the end of all output (that is, before the file is
closed or the program exits) to a text stream.
 
R

Rod Pemberton

Martin Ambuhl said:
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...


#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

whats the logic of conversion...

What conversion?

Martin, _fix_ your quoting. You merged your reply with to Santosh's
unquoted statements. You're missing Santosh's message header.
 
J

Jordan Abel

Richard G. Riley said:
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...

1) learn how to step along a string e.g

for(int i=0;i<strlen(s);i++)
char myChar = s;


That re-evaluates strlen(s) on each iteration, making the loop
O(N**2) rather than O(N).


While any decent compiler ...., you're right. How about i=0;s;i++?
Declaring a variable in a for loop is a new feature in C99; not all
compilers support it.

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.
Functions, not "programs".

potato, potato.
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.


Indeed.

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 '+'.
 
K

Keith Thompson

santosh said:
Martin said:
santosh said:
#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);

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.

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?

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

Michael Mair

Rod said:
Martin Ambuhl said:
(e-mail address removed) wrote:
[snip!]
Martin, _fix_ your quoting. You merged your reply with to Santosh's
unquoted statements. You're missing Santosh's message header.

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
 
K

Keith Thompson

Rod Pemberton said:
Martin, _fix_ your quoting. You merged your reply with to Santosh's
unquoted statements. You're missing Santosh's message header.

No, Martin wasn't replying to santosh and didn't quote anything he
said.
 
K

Keith Thompson

Jordan Abel said:
for(int i=0;i<strlen(s);i++)
char myChar = s;


That re-evaluates strlen(s) on each iteration, making the loop
O(N**2) rather than O(N).


While any decent compiler ...., you're right. How about i=0;s;i++?


Sure (though I'd write "s != '\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.)
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.

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).
potato, potato.

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.
 

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,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top