converting string to hex

X

xyz

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

Francois Grieu

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
 
B

bert

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

Ike Naar

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
 
S

Shao Miller

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

Denis McMahon

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
 
S

Seebs

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
 
B

Ben Pfaff

Seebs said:
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;
}
 
D

Denis McMahon

Here's a fixed version:

......

Shouldn't the usage message be:

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

Rgds

Denis McMahon
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top