need to convert a char to an hexadecmial value

Discussion in 'C Programming' started by sam_cit@yahoo.co.in, Mar 27, 2006.

  1. Guest

    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...
     
    , Mar 27, 2006
    #1
    1. Advertising

  2. wrote:
    > 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;
    }

    --
    Ralph Moritz

    Laugh at your problems; everybody else does.
     
    Ralph A. Moritz, Mar 27, 2006
    #2
    1. Advertising

  3. santosh Guest

    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;
    }
     
    santosh, Mar 27, 2006
    #3
  4. On 2006-03-27, <> 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...
    >


    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!
     
    Richard G. Riley, Mar 27, 2006
    #4
  5. santosh Guest

    Ralph A. Moritz wrote:
    > wrote:
    > > 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.
     
    santosh, Mar 27, 2006
    #5
  6. santosh wrote:
    > Why so much complication for such a simple task?


    You're right.; my code is not optimal. Your example is much better :)
     
    Ralph A. Moritz, Mar 27, 2006
    #6
  7. <> wrote in message
    news:...
    > 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.
     
    stathis gotsis, Mar 27, 2006
    #7
  8. 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...



    #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 Ambuhl, Mar 27, 2006
    #8
  9. 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.
    > return 0;
    > }
     
    Martin Ambuhl, Mar 27, 2006
    #9
  10. santosh Guest

    Martin Ambuhl wrote:
    > 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?
     
    santosh, Mar 27, 2006
    #10
  11. santosh wrote:
    > Martin Ambuhl wrote:
    >
    >>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."
     
    Martin Ambuhl, Mar 27, 2006
    #11
  12. "santosh" <> writes:
    > 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>.

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

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Mar 27, 2006
    #12
  13. "Richard G. Riley" <> writes:
    > On 2006-03-27, <> 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...
    >>

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

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Mar 27, 2006
    #13
  14. Jordan Abel Guest

    On 2006-03-27, santosh <> wrote:
    > Martin Ambuhl wrote:
    >> 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?


    It's needed before the end of all output (that is, before the file is
    closed or the program exits) to a text stream.
     
    Jordan Abel, Mar 27, 2006
    #14
  15. "Martin Ambuhl" <> wrote in message
    news:SjUVf.18435$...
    > 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...

    >
    >
    > #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.
     
    Rod Pemberton, Mar 27, 2006
    #15
  16. Jordan Abel Guest

    On 2006-03-27, Keith Thompson <> wrote:
    > "Richard G. Riley" <> writes:
    >> On 2006-03-27, <> 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...
    >>>

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

    >
    >> 2) look up in the c reference how to convert values using programs
    >> like printf and sprintf.

    >
    > Functions, not "programs".


    potato, potato.

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


    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 '+'.
     
    Jordan Abel, Mar 27, 2006
    #16
  17. "santosh" <> writes:
    > Martin Ambuhl wrote:
    >> 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?


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

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Mar 27, 2006
    #17
  18. Michael Mair Guest

    [OT] Re: need to convert a char to an hexadecmial value

    Rod Pemberton schrieb:
    > "Martin Ambuhl" <> wrote in message
    > news:SjUVf.18435$...
    >> 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
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
     
    Michael Mair, Mar 27, 2006
    #18
  19. "Rod Pemberton" <> writes:
    [...]
    > 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.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Mar 27, 2006
    #19
  20. Jordan Abel <> writes:
    > On 2006-03-27, Keith Thompson <> wrote:
    >> "Richard G. Riley" <> writes:

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

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


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

    >>> 2) look up in the c reference how to convert values using programs
    >>> like printf and sprintf.

    >>
    >> Functions, not "programs".

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

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Mar 27, 2006
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wwj
    Replies:
    7
    Views:
    595
  2. wwj
    Replies:
    24
    Views:
    2,573
    Mike Wahler
    Nov 7, 2003
  3. Ben Pfaff
    Replies:
    5
    Views:
    505
    Tristan Miller
    Jan 17, 2004
  4. Steffen Fiksdal

    void*, char*, unsigned char*, signed char*

    Steffen Fiksdal, May 8, 2005, in forum: C Programming
    Replies:
    1
    Views:
    624
    Jack Klein
    May 9, 2005
  5. lovecreatesbeauty
    Replies:
    1
    Views:
    1,151
    Ian Collins
    May 9, 2006
Loading...

Share This Page