On Oct 7, 11:53*am, rtillm...@gmail .com wrote:
Hello,
I did a quick google search and nothing that was returned is quite
what I am looking for. *I have a 200 character hexadecimal string that
I need to convert into a 100 character string.
This is what I have so far:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ()
{
* char *temp=
"2b000020303257 7ef6a7325629024 b0b0a0abc0b7539 2040bc3c1a2be4b 7d93129f3f1de7b *2a920000b73aed c3f247839cc3000 000203032577ef6 a7325629024b0b0 a0abc0b75392040 b*c3c1a2be4b7d9 3129f3f1de7b2a9 20000b73aedc3f2 47839cc3";
* char *toHex, *output[100];
The above addresses do not point to allocated space.
* unsigned long nVal;
* int i,j;
* for (i=0; i<100; i++){
* * strcpy(toHex,&t emp[j]);
strcpy() keeps going until it finds a null terminator. You are moving
201 characters into a random location in memory. Furthermore, j is
uninitialized.
* * strcpy(toHex,&t emp[j+1]);
See above.
* * nVal = strtoul(toHex, NULL, 16);
* * output[i] = (char*)nVal;
The above cast is utter nonsense.
* * j=j+2;
* }
* printf("output = %d \n", output);
Wrong format specifier.
* return 0;
}
This program is supposed to take the first two characters from temp
and convert the hex 2b to char which is +. *Then it gets the next two
characters and repeats.
I compiled with gcc test.c -o test
When I run test I get:
segmentation fault
What did I do wrong?
Some diagnostic tools applied to the code:
c:\tmp>lin tt.c
c:\tmp>"C:\Lint \Lint-nt" +v -i"C:\Lint" std.lnt -os(_LINT.TMP)
tt.c
PC-lint for C/C++ (NT) Vers. 8.00u, Copyright Gimpel Software
1985-2006
--- Module: tt.c (C)
c:\tmp>type _LINT.TMP | more
--- Module: tt.c (C)
_
"2b000020303257 7ef6a7325629024 b0b0a0abc0b7539 2040bc3c1a2be4b 7d93129f3f1de7b ¡2a920000b73aed c3f247839cc3000 000203032577ef6 a7325629024b0b0 a0abc0b75392040 b¡c3c1a2be4b7d9 3129f3f1de7b2a9 20000b73aedc3f2 478
39cc3";
tt.c(8) : Info 1776: Converting a string literal to char * is not
const safe
(initialization )
_
strcpy (toHex, &temp[j]);
tt.c(16) : Warning 530: Symbol 'toHex' (line 9) not initialized ---
Eff. C++
3rd Ed. item 4
tt.c(9) : Info 830: Location cited in prior message
_
strcpy (toHex, &temp[j]);
tt.c(16) : Warning 530: Symbol 'j' (line 11) not initialized --- Eff. C
++ 3rd
Ed. item 4
tt.c(11) : Info 830: Location cited in prior message
_
printf ("output = %d \n", output);
tt.c(22) : Warning 626: argument no. 2 inconsistent with format
_
}
tt.c(24) : Note 953: Variable 'toHex' (line 9) could be declared as
const ---
Eff. C++ 3rd Ed. item 3
tt.c(9) : Info 830: Location cited in prior message
_
}
tt.c(24) : Note 953: Variable 'temp' (line 7) could be declared as
const ---
Eff. C++ 3rd Ed. item 3
tt.c(7) : Info 830: Location cited in prior message
_
}
tt.c(24) : Note 954: Pointer variable 'temp' (line 7) could be
declared as
pointing to const --- Eff. C++ 3rd Ed. item 3
tt.c(7) : Info 830: Location cited in prior message
---
output placed in _LINT.TMP
c:\tmp>splint tt.c tt.spl
Splint 3.1.1 --- 12 Mar 2007
Finished checking --- 3 code warnings
c:\tmp>type tt.spl
tt.c: (in function main)
tt.c(16,28): Variable j used before definition
An rvalue is used that may not be initialized to a value on some
execution
path. (Use -usedef to inhibit warning)
tt.c(16,15): Unallocated storage toHex passed as out parameter to
strcpy: toHex
tt.c(22,29): Format argument 1 to printf (%d) expects int gets char *
[100]:
output
Type of parameter is not consistent with corresponding code in
format string.
(Use -formattype to inhibit warning)
tt.c(22,22): Corresponding format code
c:\tmp>type tt.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *temp =
"2b000020303257 7ef6a7325629024 b0b0a0abc0b7539 2040bc3c1a2be4b 7d93129f3f1de7b ¡2a920000b73aed c3f247839cc3000 000203032577ef6 a7325629024b0b0 a0abc0b75392040 b¡c3c1a2be4b7d9 3129f3f1de7b2a9 20000b73aedc3f2 478
39cc3";
char *toHex, *output[100];
unsigned long nVal;
int i, j;
for (i = 0; i < 100; i++)
{
strcpy (toHex, &temp[j]);
strcpy (toHex, &temp[j + 1]);
nVal = strtoul (toHex, NULL, 16);
output[i] = (char *) nVal;
j = j + 2;
}
printf ("output = %d \n", output);
return 0;
}
c:\tmp>
Perhaps something like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ()
{
const unsigned char temp[]=
"2b000020303257 7ef6a7325629024 b0b0a0abc0b7539 2040bc3c1a2be4b 7d93129f3f1de7b *
2a920000b73aedc 3f247839cc30000 00203032577ef6a 7325629024b0b0a 0abc0b75392040b *
c3c1a2be4b7d931 29f3f1de7b2a920 000b73aedc3f247 839cc3";
unsigned char toHex[3]={0}, output[((sizeof temp) >1) + 1];
unsigned long nVal;
int i,j;
for (i=0, j=0; i<100; i++, j+=2){
toHex[0] = temp[j];
toHex[1] = temp[j+1];
nVal = strtoul(toHex, NULL, 16);
output[i] = (unsigned char)nVal;
printf("%02x ", output[i]);
if ((i+1) % 26 == 0) putchar('\n');
}
putchar('\n');
return 0;
}