将十六进制字符串转换为char [英] convert string of hex characters to char

查看:135
本文介绍了将十六进制字符串转换为char的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我做了一个快速谷歌搜索,没有返回任何东西

我正在寻找。我有一个200字符的十六进制字符串

我需要转换成100个字符的字符串。


这是我到目前为止的结果:

#include< stdio.h>

#include< stdlib.h>

#include< string.h>


INT main()的

{

字符*温度=

" 2b0000203032577ef6a7325629024b0b0a0abc0b75392040b c3c1a2be4b7d93129f3f1de7b2a920000b73aedc3f247839cc 3000000203032577ef6a7325629024b0b0a0abc0b75392040b c3c1a2be4b7d93129f3f1de7b2a920000b73aedc3f247839cc 3英寸; <无线电通信/>
char * toHex,*输出[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",输出);

返回0;

}


这个程序应该从temp

中获取前两个字符,并将hex 2b转换为char,即+。然后它得到接下来的两个

字符并重复。


我用gcc test.c编译 - 测试

当我运行测试我得到:

分段错误


我做错了什么?

谢谢,

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=
"2b0000203032577ef6a7325629024b0b0a0abc0b75392040b c3c1a2be4b7d93129f3f1de7b2a920000b73aedc3f247839cc 3000000203032577ef6a7325629024b0b0a0abc0b75392040b c3c1a2be4b7d93129f3f1de7b2a920000b73aedc3f247839cc 3";
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;
}

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,

推荐答案

10月7日,11:53 * am,rtillm ... @ gmail.com写道:
On Oct 7, 11:53*am, rtillm...@gmail.com wrote:

您好,


我做了一个快速谷歌搜索,没有返回任何东西

我正在寻找。 *我有一个200个字符的十六进制字符串

我需要转换为100个字符的字符串。


这是我到目前为止:

#include< stdio.h>

#include< stdlib.h>

#include< string.h>


int main()

{

* char * temp =

" 2b0000203032577ef6a7325629024b0b0a0abc0b75392040b c3c1a2be4b7d93129f3f1de7b * 2a920000b73aedc3f247839c c3000000203032577ef6a7325629024b0b0a0abc0b75392040 b * c3c1a2be4b7d93129f3f1de7b2a920000b73aedc3f247839 cc3" ;;

* char * toHex,* output [100];
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=
"2b0000203032577ef6a7325629024b0b0a0abc0b75392040b c3c1a2be4b7d93129f3f1de7b*2a920000b73aedc3f247839c c3000000203032577ef6a7325629024b0b0a0abc0b75392040 b*c3c1a2be4b7d93129f3f1de7b2a920000b73aedc3f247839 cc3";
* 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) ,&安培;温度[J]);
* unsigned long nVal;
* int i,j;

* for (i=0; i<100; i++){
* * strcpy(toHex,&temp[j]);



strcpy()继续运行,直到找到空终止符。您正在将
201个字符移动到内存中的随机位置。此外,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,& temp [j + 1]);
* * strcpy(toHex,&temp[j+1]);



见上文。

See above.


* * nVal = strtoul(toHex,NULL,16);

* * output [i] =(char *)nVal;
* * nVal = strtoul(toHex, NULL, 16);
* * output[i] = (char*)nVal;



上面的演员完全是胡说八道。

The above cast is utter nonsense.


* * j = j + 2;

*}

* printf(" output =%d \ n",output);
* * j=j+2;
* }
* printf("output = %d \n", output);



错误的格式说明符。

Wrong format specifier.


*返回0;


}


这个程序应该从temp

中获取前两个字符,并将hex 2b转换为char + char。 *然后它获得接下来的两个

字符并重复。


我用gcc test.c编译 - 测试

当我运行测试我得到:

分段错误


我做错了什么?
* 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?



应用于代码的一些诊断工具:


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,版权所有Gimpel Software

1985-2006


---模块:tt.c(C)


c:\ tmp>输入_LINT.TMP |更多


---模块:tt.c(C)


_


" ; 2b0000203032577ef6a7325629024b0b0a0abc0b75392040b c3c1a2be4b7d93129f3f1de7b 2a920000b73aedc3f247839c c3000000203032577ef6a7325629024b0b0a0abc0b75392040 b c3c1a2be4b7d93129f3f1de7b2a920000b73aedc3f2478

39cc3英寸;

tt.c(8):1776信息:转换一个字符串为char *不是

const safe

(初始化)

_

strcpy(toHex,& temp [j]);

tt.c(16):警告530:符号''toHex''(第9行)未初始化---

Eff。 C ++

第3版。第4项

tt.c(9):信息830:先前消息中引用的位置

_

strcpy(toHex,& temp [ j]);

tt.c(16):警告530:符号j(第11行)未初始化 - Eff。 C

++ 3rd

Ed。第4项

tt.c(11):信息830:先前消息中引用的位置

_

printf(" output =%d \ n",output);

tt.c(22):警告626:参数号。 2与格式不一致

_

}

tt.c(24):注953:变量''toHex''(第9行)可以声明为

const ---

Eff。 C ++ 3rd Ed。项目3

tt.c(9):信息830:先前消息中引用的位置

_

}

tt.c(24):注953:变量''temp''(第7行)可以声明为

const ---

Eff。 C ++ 3rd Ed。第3项

tt.c(7):信息830:先前消息中引用的位置

_

}

tt.c(24):注954:指针变量''temp''(第7行)可以是

声明为

指向const --- Eff。 C ++ 3rd Ed。第3项

tt.c(7):信息830:先前消息中引用的位置


---

输出放置在_LINT.TMP


c:\ tmp> splint tt.c tt.spl

Splint 3.1.1 --- 2007年3月12日


完成检查--- 3个代码警告


c:\ tmp>类型tt.spl

tt.c :(在函数main中)

tt.c(16,28):定义前使用的变量j

使用的rvalue可能无法初始化为某些值的某个值/>
执行

路径。 (使用-usedef禁止警告)

tt.c(16,15):未分配的存储toHex作为out参数传递给

strcpy:toHex

tt.c(22,29):将参数1格式化为printf(%d),期望int获取char *

[100]:

输出

参数类型与

格式字符串中的对应代码不一致。

(使用-formattype禁止警告)

tt .c(22,22):对应的格式代码

c:\ tmp>类型tt.c

#include< stdio.h>

#include< stdlib.h>

#include< string.h>


int main()

{

字符*温度=


"?2b0000203032577ef6a7325629024b0b0a0abc0b75392040b c3c1a2be4b7d93129f3f1de7b 2a920000b73aedc3f247839c c3000000203032577ef6a7325629024b0b0a0abc0b75392040 b c3c1a2be4b7d93129f3f1de7b2a920000b73aedc3f2478

39cc3英寸;

char * toHex,* output [100];

unsigned long nVal;

int i, j;

for(i = 0;我< 100; i ++)

{

strcpy(toHex,& temp [j]);

strcpy(toHex,& temp [j + 1] ]);

nVal = strtoul(toHex,NULL,16);

输出[i] =(char *)nVal;

j = j + 2;

}

printf(" output =%d \ n",output);

返回0;

}


c:\ tmp>


也许是这样的:

#包括< stdio.h>

#include< stdlib.h>

#include< string.h>


int main()

{

const unsigned char temp [] =

" 2b0000203032577ef6a7325629024b0b0a0abc0b75392040b c3c1a2be4b7d93129f3f1de7b *

2a920000b73aedc3f247839cc3000000203032577ef6a73256 29024b0b0a0abc0b75392040b *

c3c1a2be4b7d93129f3f1de7b2a920000b73aedc3f247839cc 3" ;;

unsigned char toHex [3] = {0},输出[((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",输出[i]);

if((i + 1)%26 == 0)putchar(''\ n'');

}

putchar(''\ n'');

返回0;

}

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)

_

"2b0000203032577ef6a7325629024b0b0a0abc0b75392040b c3c1a2be4b7d93129f3f1de7b?2a920000b73aedc3f247839c c3000000203032577ef6a7325629024b0b0a0abc0b75392040 b?c3c1a2be4b7d93129f3f1de7b2a920000b73aedc3f2478
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 =

"2b0000203032577ef6a7325629024b0b0a0abc0b75392040b c3c1a2be4b7d93129f3f1de7b?2a920000b73aedc3f247839c c3000000203032577ef6a7325629024b0b0a0abc0b75392040 b?c3c1a2be4b7d93129f3f1de7b2a920000b73aedc3f2478
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[]=
"2b0000203032577ef6a7325629024b0b0a0abc0b75392040b c3c1a2be4b7d93129f3f1de7b*
2a920000b73aedc3f247839cc3000000203032577ef6a73256 29024b0b0a0abc0b75392040b*
c3c1a2be4b7d93129f3f1de7b2a920000b73aedc3f247839cc 3";
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;
}


rt*******@gmail.com 写道:

你好,


我做了一个快速谷歌搜索,没有返回任何东西

我正在寻找什么。我有一个200字符的十六进制字符串

我需要转换成100个字符的字符串。


这是我到目前为止的结果:

#include< stdio.h>

#include< stdlib.h>

#include< string.h>


INT main()的

{

字符*温度=

" 2b0000203032577ef6a7325629024b0b0a0abc0b75392040b c3c1a2be4b7d93129f3f1de7b2a920000b73aedc3f247839cc 3000000203032577ef6a7325629024b0b0a0abc0b75392040b c3c1a2be4b7d93129f3f1de7b2a920000b73aedc3f247839cc 3英寸; <无线电通信/>
char * toHex,*输出[100];

unsigned long nVal;

int i,j;


for(i = 0; i< 100; i ++){

strcpy(toHex,& temp [j]);
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=
"2b0000203032577ef6a7325629024b0b0a0abc0b75392040b c3c1a2be4b7d93129f3f1de7b2a920000b73aedc3f247839cc 3000000203032577ef6a7325629024b0b0a0abc0b75392040b c3c1a2be4b7d93129f3f1de7b2a920000b73aedc3f247839cc 3";
char *toHex, *output[100];
unsigned long nVal;
int i,j;

for (i=0; i<100; i++){
strcpy(toHex,&temp[j]);



Bzzzt!这里至少有三件事是错的。首先,你
没有给指针变量toHex赋值,所以它的值

是不确定的:它包含垃圾或者指向一个随机的地方,

你不知道你在哪里试图存储你复制的数据

out of temp。其次,你没有给整数

变量j赋值,所以它的值也是不确定的:它持有一个随机的

值。你不知道你要从哪里复制。并且

三,strcpy()是错误的函数,如果你试图复制

单个字符:它将从你指向的第一个字符开始

at并且一直保持不变,直到它复制了''\ 0''字符。


建议修复:从指向数组的指针改为toHex三个
个字符,在循环前将j初始化为零,并用


替换

两个strcpy()调用toHex [0] = temp [j];

toHex [1] = temp [j + 1];

toHex [2] =''\ 0'';


(有很多方法可以写这个,但为了清楚起见,我试图坚持使用原始术语

。)

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,& temp [j + 1]);

nVal = strtoul(toHex,NULL,16);

输出[i] =(char *)nVal;
strcpy(toHex,&temp[j+1]);
nVal = strtoul(toHex, NULL, 16);
output[i] = (char*)nVal;



这是另一个问题:输出是一个包含100个指针变量的数组,

但你试图在其中存储实际的char值。我想,

编译器抱怨代码的早期版本,并且你为
添加了(char *)强制转换以使其静音。不幸的是,这几乎是完全错误的事情:正确的做法是退后一步并询问

为什么编译器在抱怨。

建议修复:将输出更改为char数组而不是char *的
数组。另外,试着找出你打算用什么来做什么

所有这些字符;此刻,你几乎什么也没做。

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);
j=j+2;
}
printf("output = %d \n", output);



另一个问题:输出是一个char *数组(或者一个char的数组

,上面提到的更改),以及;%d"说明者想要...

你是对的,它需要一个整数。从什么时候开始是一个100位

数组与一个整数相同?


建议修复:完全摆脱输出,把printf

在循环内部调用(您可能需要调整格式),

并在计算时打印出每个nVal。

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.


返回0;

}


这个程序应该从temp

转换前两个字符并转换十六进制2b到char是+。然后它得到接下来的两个

字符并重复。


我用gcc test.c编译 - 测试

当我运行测试我得到:

分段错误


我做错了什么?
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?



对于额外的信用和另一种解决问题的方法,研究

sscanf函数并考虑如何使用%2x&#说明者。


-
Er ****** ***@sun.com


我做了一个快速谷歌搜索,没有返回任何东西是
I did a quick google search and nothing that was returned is quite

我正在寻找什么。 *我有一个200字符的十六进制字符串

我需要转换为100个字符的字符串。
what I am looking for. *I have a 200 character hexadecimal string that
I need to convert into a 100 character string.


这是我到目前为止:

#include< stdio.h>

#include< stdlib.h>

#include< string.h>
This is what I have so far:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main()

{

* char * temp =

" 2b0000203032577ef6a7325629024b0b0a0abc0b75392040b c3c1a2be4b7d93129f3f1de7b * 2a920000b73aedc3f247839c c3000000203032577ef6a7325629024b0b0a0abc0b75392040 b *表CC3 c3c1a2be4b7d93129f3f1de7b2a920000b73aedc3f247839英寸;

*的字符* toHex,*输出[100];
int main ()
{
* char *temp=
"2b0000203032577ef6a7325629024b0b0a0abc0b75392040b c3c1a2be4b7d93129f3f1de7b*2a920000b73aedc3f247839c c3000000203032577ef6a7325629024b0b0a0abc0b75392040 b*c3c1a2be4b7d93129f3f1de7b2a920000b73aedc3f247839 cc3";
* 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;
* unsigned long nVal;
* int i,j;


* for(i = 0; i< 100; i ++){

* * strcpy(toHex,&温度[J]);
* for (i=0; i<100; i++){
* * strcpy(toHex,&temp[j]);



strcpy()继续运行,直到找到空终止符。 *您正在将
201个字符移动到内存中的随机位置。 *此外,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,& temp [j + 1]);
* * strcpy(toHex,&temp[j+1]);



见上文。


See above.


* * nVal = strtoul(toHex,NULL,16);

* * output [i] =(char *)nVal;
* * nVal = strtoul(toHex, NULL, 16);
* * output[i] = (char*)nVal;



上面的演员完全是胡说八道。


The above cast is utter nonsense.


* * j = j + 2;

*}

* printf(" output =%d \ n",output);
* * j=j+2;
* }
* printf("output = %d \n", output);



格式说明符错误。


Wrong format specifier.


* return 0;
* return 0;


}
}


这个程序应该是前两个来自temp

的字符并将hex 2b转换为char,即+。 *然后它获得接下来的两个

字符并重复。
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.


我使用gcc test.c -o test编译

当我运行测试时,我得到:

分段错误
I compiled with gcc test.c -o test
When I run test I get:
segmentation fault


我做错了什么?
What did I do wrong?



应用于代码的一些诊断工具:


c:\ tmp> lin tt.c


Some diagnostic tools applied to the code:

c:\tmp>lin tt.c



输出已删除

output deleted


c:\ tmp> splint tt.c tt.spl
c:\tmp>splint tt.c tt.spl



输出已删除


看起来有一个linux版本的splint。我将开始使用

吧。


谢谢你修复的代码。我明天会试一下,让你知道

如果我让它工作的话。

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.


这篇关于将十六进制字符串转换为char的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆