converting string to hex

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

1. xyzGuest

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

2. Francois GrieuGuest

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

3. bertGuest

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
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
4. Ike NaarGuest

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
5. Shao MillerGuest

What base is the string of digits to convert? Will it be specified or
is it guaranteed?

Shao Miller, Jul 28, 2010
6. Denis McMahonGuest

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

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
8. Ben PfaffGuest

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
9. Denis McMahonGuest

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