converting string to hex

Discussion in 'C Programming' started by xyz, Jul 28, 2010.

  1. xyz

    xyz Guest

    Hi,

    I need to convert the following string in hex format :
    "111733394601234567094987654321"
    which should result in
    "16907B2A24ABC16A2E5C004B1". (resulting hex string)


    Thank you for any help.
     
    xyz, Jul 28, 2010
    #1
    1. Advertising

  2. On 28/07/2010 11:20, xyz wrote:
    > I need to convert the following string in hex format :
    > "111733394601234567094987654321"
    > which should result in
    > "16907B2A24ABC16A2E5C004B1". (resulting hex string)


    I have this lying around that works:


    #include <stdio.h>

    #define MAXH 2100 /* maximum number of hex digits */

    void tohex(char* s)
    {
    unsigned char hexv[MAXH],*p,c;
    int j;

    p = hexv;
    j = MAXH;
    do
    *p++ = 0;
    while (--j);

    while ((c=*s++)!=0)
    {
    if (c<'0' || c>'9')
    goto ko;
    c -='0';

    p = hexv;
    j = MAXH;
    do
    {
    c += (*p)*10;
    *p++ = c&15;
    c >>= 4;
    }
    while (--j);
    if (c)
    goto ko;
    }

    j = MAXH;
    do
    --p;
    while(--j!=0 && *p==0);
    ++j;
    /* j = number of hex digits, or 1 if input is zero */
    while (--j>=0)
    putchar("0123456789ABCDEF"[*p--]);
    goto ok;
    ko: putchar('#');
    ok: putchar('\n');
    }

    int main(int argc, char *argv[])
    {
    int arg;
    for (arg=1; arg<argc; arg++)
    tohex(argv[arg]);
    return 0;
    }


    Francois Grieu
     
    Francois Grieu, Jul 28, 2010
    #2
    1. Advertising

  3. xyz

    bert Guest

    On 28 July, 10:20, xyz <> wrote:
    > Hi,
    >
    > I need to convert the following string in hex format :
    > "111733394601234567094987654321"
    > which should result in
    > "16907B2A24ABC16A2E5C004B1".  (resulting hex string)
    >
    > Thank you for any help.


    If speed is not too important, write a
    function which destructively divides a
    string of decimal digits by 2, and
    returns the remainder (1 or 0). Call
    it in a loop, grouping each 4 successive
    results to look up which character to
    next add to the answer. When the string
    becomes either "1" or "0", you are just
    about done. I don't think the detailed
    coding should be at all difficult.
    --
     
    bert, Jul 28, 2010
    #3
  4. xyz

    Ike Naar Guest

    In article <>,
    xyz <> wrote:
    >I need to convert the following string in hex format :
    >"111733394601234567094987654321"
    >which should result in
    >"16907B2A24ABC16A2E5C004B1". (resulting hex string)


    Not really a C question. You seem to be running Linux,
    so one way to go is use ``dc'':

    echo 16o 111733394601234567094987654321 p | dc
    --

    SDF Public Access UNIX System - http://sdf.lonestar.org
     
    Ike Naar, Jul 28, 2010
    #4
  5. xyz

    Shao Miller Guest

    What base is the string of digits to convert? Will it be specified or
    is it guaranteed?
     
    Shao Miller, Jul 28, 2010
    #5
  6. On 28/07/10 10:20, xyz wrote:

    > I need to convert the following string in hex format :
    > "111733394601234567094987654321"
    > which should result in
    > "16907B2A24ABC16A2E5C004B1". (resulting hex string)


    What is the code that you have written to accomplish this task, and what
    is the problem that you are having with that code?

    Rgds

    Denis McMahon
     
    Denis McMahon, Jul 28, 2010
    #6
  7. xyz

    Seebs Guest

    On 2010-07-28, Denis McMahon <> wrote:
    > On 28/07/10 10:20, xyz wrote:
    >
    >> I need to convert the following string in hex format :
    >> "111733394601234567094987654321"
    >> which should result in
    >> "16907B2A24ABC16A2E5C004B1". (resulting hex string)

    >
    > What is the code that you have written to accomplish this task, and what
    > is the problem that you are having with that code?


    Stop being mean. He needs help.

    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>

    int main(int argc, char *argv[])
    {
    if (argc != 2) {
    fprintf(stderr, "Usage: convert 111733394601234567094987654321\n");
    exit(EXIT_FAILURE);
    }
    if (strcmp(argv[1], "111733394601234567094987654321")) {
    fprintf(stderr, "Usage: convert 111733394601234567094987654321\n");
    exit(EXIT_FAILURE);
    }
    printf("16907B2A24ABC16A2E5C004B1\n");
    return EXIT_SUCCESS;
    }

    See? It's not at all hard to answer his question and get him unstuck.

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
     
    Seebs, Jul 28, 2010
    #7
  8. xyz

    Ben Pfaff Guest

    Seebs <> writes:

    > On 2010-07-28, Denis McMahon <> wrote:
    >> On 28/07/10 10:20, xyz wrote:
    >>
    >>> I need to convert the following string in hex format :
    >>> "111733394601234567094987654321"
    >>> which should result in
    >>> "16907B2A24ABC16A2E5C004B1". (resulting hex string)

    >>
    >> What is the code that you have written to accomplish this task, and what
    >> is the problem that you are having with that code?

    >
    > Stop being mean. He needs help.
    >
    > #include <string.h>
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > int main(int argc, char *argv[])
    > {
    > if (argc != 2) {
    > fprintf(stderr, "Usage: convert 111733394601234567094987654321\n");
    > exit(EXIT_FAILURE);
    > }
    > if (strcmp(argv[1], "111733394601234567094987654321")) {
    > fprintf(stderr, "Usage: convert 111733394601234567094987654321\n");
    > exit(EXIT_FAILURE);
    > }
    > printf("16907B2A24ABC16A2E5C004B1\n");
    > return EXIT_SUCCESS;
    > }


    Don't be absurd, Seebs. No one is going to accept code with that
    degree of redundancy.

    Here's a fixed version:

    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>

    int main(int argc, char *argv[])
    {
    if (argc != 2 || strcmp(argv[1], "111733394601234567094987654321")) {
    fprintf(stderr, "Usage: convert 111733394601234567094987654321\n");
    exit(EXIT_FAILURE);
    }
    printf("16907B2A24ABC16A2E5C004B1\n");
    return EXIT_SUCCESS;
    }

    --
    Know C and GNU/Linux? Looking for a job? http://nicira.com/
     
    Ben Pfaff, Jul 28, 2010
    #8
  9. On 28/07/10 23:42, Ben Pfaff wrote:

    > Here's a fixed version:


    ......

    Shouldn't the usage message be:

    fprintf(stderr, "Usage: %s 111733394601234567094987654321\n",argv[0]);

    Rgds

    Denis McMahon
     
    Denis McMahon, Jul 29, 2010
    #9
    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. Replies:
    10
    Views:
    6,412
    Neredbojias
    Aug 19, 2005
  2. Bengt Richter
    Replies:
    6
    Views:
    537
    Juha Autero
    Aug 19, 2003
  3. jack
    Replies:
    4
    Views:
    617
  4. tim

    hex string to hex value

    tim, Nov 22, 2005, in forum: Python
    Replies:
    8
    Views:
    19,143
  5. tim
    Replies:
    2
    Views:
    1,596
    Dennis Lee Bieber
    Nov 23, 2005
Loading...

Share This Page