473,725 Members | 2,017 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

convert string of hex characters to char

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 2a920000b73aedc 3f247839cc30000 00203032577ef6a 7325629024b0b0a 0abc0b75392040b c3c1a2be4b7d931 29f3f1de7b2a920 000b73aedc3f247 839cc3";
char *toHex, *output[100];
unsigned long nVal;
int i,j;

for (i=0; i<100; i++){
strcpy(toHex,&t emp[j]);
strcpy(toHex,&t emp[j+1]);
nVal = strtoul(toHex, NULL, 16);
output[i] = (char*)nVal;
j=j+2;
}
printf("output = %d \n", output);
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?
Thanks,
Oct 7 '08 #1
14 13512
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;
}

Oct 7 '08 #2
rt*******@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 2a920000b73aedc 3f247839cc30000 00203032577ef6a 7325629024b0b0a 0abc0b75392040b c3c1a2be4b7d931 29f3f1de7b2a920 000b73aedc3f247 839cc3";
char *toHex, *output[100];
unsigned long nVal;
int i,j;

for (i=0; i<100; i++){
strcpy(toHex,&t emp[j]);
Bzzzt! There are at least three things wrong here. First, you
have not given any value to the pointer variable toHex, so its value
is indeterminate: it "contains garbage" or "points to a random place,"
and you have no idea where you're trying to store the data you copy
out of temp. Second, you have not given any value to the integer
variable j, so its value is also indeterminate: it "holds a random
value" and you have no idea where you're trying to copy from. And
third, strcpy() is the wrong function to use if you're trying to copy
single characters: It will start with the first character you point it
at and just keep chugging along until it has copied a '\0' character.

Suggested fix: Change toHex from a pointer to an array of three
characters, initialize j to zero before the loop, and replace the
two strcpy() calls with

toHex[0] = temp[j];
toHex[1] = temp[j+1];
toHex[2] = '\0';

(There are snazzier ways to write this, but I'm trying to stick with
your original terminology for clarity's sake.)
strcpy(toHex,&t emp[j+1]);
nVal = strtoul(toHex, NULL, 16);
output[i] = (char*)nVal;
Here's another problem: output is an array of 100 pointer variables,
but your trying to store actual char values in them. I imagine that the
compiler complained about an earlier version of the code, and that you
added the (char*) cast to silence it. Unfortunately, this is almost
always the wrong thing to do: The right thing is to step back and ask
why the compiler is complaining.

Suggested fix: Change output to an array of char instead of an
array of char*. Also, try to figure out what you intend to do with
all these characters; at the moment, you're doing almost nothing.
j=j+2;
}
printf("output = %d \n", output);
Another problem: output is an array of char* (or an array of char
with the change suggested above), and the "%d" specifier wants ...
Right you are, it wants one integer. Since when is a 100-place
array the same as one integer?

Suggested fix: Get rid of output altogether, put the printf
call inside the loop (you may want to make adjustments to the format),
and print out each nVal as you compute it.
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?
For extra credit and another approach to the problem, study the
sscanf function and consider how you might use a "%2x" specifier.

--
Er*********@sun .com
Oct 7 '08 #3
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.
Ah, yes I see it now. It was starting right at me.
* 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
output deleted
c:\tmp>splint tt.c tt.spl
output deleted

It looks like there is a linux version of splint. I will start to use
it.

Thank you for the fixed code. I will try it tomorrow and let you know
if I get it working.
Oct 7 '08 #4
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 2a920000b73aedc 3f247839cc30000 00203032577ef6a 7325629024b0b0a 0abc0b75392040b c3c1a2be4b7d931 29f3f1de7b2a920 000b73aedc3f247 839cc3";
* char *toHex, *output[100];
* unsigned long nVal;
* int i,j;
* for (i=0; i<100; i++){
* * strcpy(toHex,&t emp[j]);

* * *Bzzzt! *There are at least three things wrong here. *First, you
have not given any value to the pointer variable toHex, so its value
is indeterminate: it "contains garbage" or "points to a random place,"
and you have no idea where you're trying to store the data you copy
out of temp.
Yeah I see that now.
*Second, you have not given any value to the integer
variable j, so its value is also indeterminate: it "holds a random
value" and you have no idea where you're trying to copy from.
I could have sworn that when you use gcc to compile a program the
variables are initialized to zero or Null. I read the linux-kernel
mail list and see messages that say NOT to initialize variables as it
isn't needed and just adds bloat to the executable. Oh well.
>*And
third, strcpy() is the wrong function to use if you're trying to copy
single characters: It will start with the first character you point it
at and just keep chugging along until it has copied a '\0' character.

* * *Suggested fix: Change toHex from a pointer to an array of three
characters, initialize j to zero before the loop, and replace the
two strcpy() calls with

* * * * toHex[0] = temp[j];
* * * * toHex[1] = temp[j+1];
* * * * toHex[2] = '\0';

(There are snazzier ways to write this, but I'm trying to stick with
your original terminology for clarity's sake.)
Thank you for sticking to the original code.
* * strcpy(toHex,&t emp[j+1]);
* * nVal = strtoul(toHex, NULL, 16);
* * output[i] = (char*)nVal;

* * *Here's another problem: output is an array of 100 pointer variables,
but your trying to store actual char values in them. *I imagine that the
compiler complained about an earlier version of the code, and that you
added the (char*) cast to silence it. *
That is exactly what I did.
Unfortunately, this is almost
always the wrong thing to do: The right thing is to step back and ask
why the compiler is complaining.

* * *Suggested fix: Change output to an array of char instead of an
array of char*. *Also, try to figure out what you intend to do with
all these characters; at the moment, you're doing almost nothing.
* * j=j+2;
* }
* printf("output = %d \n", output);

* * *Another problem: output is an array of char* (or an array of char
with the change suggested above), and the "%d" specifier wants ...
Right you are, it wants one integer. *Since when is a 100-place
array the same as one integer?

* * *Suggested fix: Get rid of output altogether, put the printf
call inside the loop (you may want to make adjustments to the format),
and print out each nVal as you compute it.
* return 0;
}

* * *For extra credit and another approach to the problem, study the
sscanf function and consider how you might use a "%2x" specifier.
I use sprintf elsewhere to store the hex version of something. I am
not in front of my PC at the moment.

Thanks for the tips. I will post when I figure it all out.

Thanks,
Oct 7 '08 #5
rt*******@gmail .com writes:
[...]
I could have sworn that when you use gcc to compile a program the
variables are initialized to zero or Null. I read the linux-kernel
mail list and see messages that say NOT to initialize variables as it
isn't needed and just adds bloat to the executable. Oh well.
[...]

Objects with static storage duration, if they're not explicitly
initialized, are implicitly initialized to 0 with the appropriate type
(0 for integers, 0.0 for floating-point, NULL (not Null) for pointers,
applied recursively to struct and union members and array elements).
This applies to objects declared outside any function, and to objects
declared within a function with the "static" keyword.

Objects with automatic storage duration, if they're not explicitly
initialized, are not implicitly initialized. Their initial values
will be garbage.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 7 '08 #6
[...]I could have sworn that when you use gcc to compile a program the
variables are initialized to zero or Null. *I read the linux-kernel
mail list and see messages that say NOT to initialize variables as it
isn't needed and just adds bloat to the executable. *Oh well.

[...]

Objects with static storage duration, if they're not explicitly
initialized, are implicitly initialized to 0 with the appropriate type
(0 for integers, 0.0 for floating-point, NULL (not Null) for pointers,
applied recursively to struct and union members and array elements).
This applies to objects declared outside any function, and to objects
declared within a function with the "static" keyword.

Objects with automatic storage duration, if they're not explicitly
initialized, are not implicitly initialized. *Their initial values
will be garbage.
I learned something new today.

Thanks,
Oct 8 '08 #7
On 8 Oct, 00:02, rtillm...@gmail .com wrote:
I imagine that the
compiler complained about an earlier version of the code, and that you
added the (char*) cast to silence it. *

That is exactly what I did.
break this habit

--
Nick Keighley
Oct 8 '08 #8
For extra credit and another approach to the problem, study the
sscanf function and consider how you might use a "%2x" specifier.
You mean something like this:
for (i=0, j=0; i<76; i++, j+=2){
sscanf(&temp[j], "%2x", &output[i]);
printf("%02x ", output[i]);
}

Thank you to everyone for your help.
Oct 8 '08 #9
On Oct 8, 3:38 pm, rtillm...@gmail .com wrote:
For extra credit and another approach to the problem, study the
sscanf function and consider how you might use a "%2x" specifier.

You mean something like this:
for (i=0, j=0; i<76; i++, j+=2){
sscanf(&temp[j], "%2x", &output[i]);
printf("%02x ", output[i]);
}

Thank you to everyone for your help.
The compiler threw a:
warning: format '%2x' expects type 'unsigned int *', but argument 3
has type 'unsigned int'

so I did the following:
sscanf(&temp[j], "%2x", (unsigned int*)&output[i]);

It silences the compiler and the output is the same but I wanted to
post to make sure this was the right way to handle the warning.

So is this right?
Oct 8 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
24531
by: Markus Hämmerli | last post by:
I ' ll tra to convert a Cstring to char* without success. I working in a Unicode enabled environment this is working in Unicode CString source = _T("TestString"); TCHAR *szSource = source.GetBuffer(0 ); but i need a char* and so this is not working CString source = _T("TestString");
15
34594
by: Kueishiong Tu | last post by:
How do I convert a Byte array (unsigned char managed) to a char array(unmanaged) with wide character taken into account?
6
12765
by: Allan Ebdrup | last post by:
How do I easily convert a int to a string? Kind Regards, Allan Ebdrup
20
3440
by: Niyazi | last post by:
Hi all, I have a integer number from 1 to 37000. And I want to create a report in excel that shows in 4 alphanumeric length. Example: I can write the cutomerID from 1 to 9999 as: 1 ----> 0001 2 ----> 0002
4
22039
by: uday.sen | last post by:
Hi, I need to convert a string from UTF8 to wide character (wchar_t *). I perform the same in windows using: MultiByteToWideChar(CP_UTF8, 0, pInput, -1, pOutput, nLen); However, in linux this API is not available. However, there exists mbstowcs() API, which converts multibyte string to wide character. But will this API convert UTF8 encoded string to wide character? Or this
4
17781
by: thinktwice | last post by:
i'm using VC++6 IDE i know i could use macros like A2T, T2A, but is there any way more decent way to do this?
8
8431
by: Polaris431 | last post by:
I have a buffer that holds characters. Four characters in a row represent an unsigned 32 bit value. I want to convert these characters to a 32 bit value. For example: char buffer; buffer = "aabbccdd"; where aa is the LSB and dd is the MSB.
4
25067
by: meendar | last post by:
Hi, I am having a character pointer which contains ascii values. i just want to convert all these ascii values to respective characters and again store it in another character pointer. Anybody please help in c language. Thanks in Advance.
4
3895
by: sagar | last post by:
Hi Guys.. I Wanted to convert an int to a string in C#...but i cannot use any built in functions.So, can any one tell me an algorithm or a program to do so....please is urgent....
0
9401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9111
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8096
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4517
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4782
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3221
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.