Velocity Reviews > converting string to hex

# converting string to hex

xyz
Guest
Posts: n/a

 07-28-2010
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.

Francois Grieu
Guest
Posts: n/a

 07-28-2010
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

bert
Guest
Posts: n/a

 07-28-2010
On 28 July, 10:20, xyz <(E-Mail Removed)> 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.
--

Ike Naar
Guest
Posts: n/a

 07-28-2010
In article <(E-Mail Removed)>,
xyz <(E-Mail Removed)> 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
--
http://www.velocityreviews.com/forums/(E-Mail Removed)
SDF Public Access UNIX System - http://sdf.lonestar.org

Shao Miller
Guest
Posts: n/a

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

Denis McMahon
Guest
Posts: n/a

 07-28-2010
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

Seebs
Guest
Posts: n/a

 07-28-2010
On 2010-07-28, Denis McMahon <(E-Mail Removed)> 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 / (E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!

Ben Pfaff
Guest
Posts: n/a

 07-28-2010
Seebs <(E-Mail Removed)> writes:

> On 2010-07-28, Denis McMahon <(E-Mail Removed)> 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/

Denis McMahon
Guest
Posts: n/a

 07-29-2010
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