Re: need to convert a char to an hexadecmial value



Keith Thompson wrote:
"santosh" <santosh.k83@xxxxxxxxx> writes:
sam_cit@xxxxxxxxxxx wrote:
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>.

I thought, obviously wrongly, that fflush() was declared in 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.)

And I felt that an array based example would be simpler to understand
for the OP.

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.

Yes, I hope the OP can forgive me, modify it as necessary.

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.

Okay.

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.

Alternatively, the array could have been declared as unsigned int,
though that would be gratituous waste of memory.

3 is a magic number (not a huge deal in a snippet like this).

Yes. Should have coded it cleaner.

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

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?

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

Thanks for this improvement.

.



Relevant Pages

  • Re: need to convert a char to an hexadecmial value
    ... I needed help in converting a character to the correspoding ... For characters with values less than 16, you print a single digit, ... only an insigificant amount of data space, ... Just use int. ...
    (comp.lang.c)
  • Re: String.ASC and (int)
    ... When converting a character to an int, ... I have everything changed except a packed date routine> which uses Strings.ASC and uses the last char of the packed field to> indicate whether its a positive or negative number - I have isolated> the problem to a few lines of code and wonder if someone can explain> what is happening ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: printf and cout
    ... /will/ work (although heaven knows what character will be printed!). ... If no l length modifier is present, the int argument is converted ... Is it possible to "fail" when converting an int to unsigned char? ...
    (comp.lang.c)
  • Re: convert int to char
    ... atoi is for converting a *string* in to an int, ... character. ... converting a single digit is far simpler: ...
    (comp.lang.c)
  • Re: Test if an integer is a palindrome in c language
    ... <string-based palindrome tester snipped> ... int pal_aux ... Certainly much brighter than converting to character. ...
    (comp.lang.c)

Loading