English 中文(简体)
C[复制]中Hx至Bary
原标题:Convert Hex to Binary in C [duplicate]
  • 时间:2011-11-20 22:31:21
  •  标签:
  • c
  • binary
  • hex
This question already has answers here:
Closed 11 years ago.

Possible Duplicate:
Convert a long hex string in to int array with sscanf

我以前曾看到过这方面的一些议题,但大多数解决办法都是在沙尔(因为你很容易!) 。

在C这样做是否容易? 这是我的解决办法。 我知道这在概念上并不复杂,但我对C扼杀操纵和处理问题感到可怕。

在帮助下,gu!

页: 1

奥凯,因此我解决了这一问题,我还有另一个问题(我不想开新的路!) 。 以下是我对特定法典的修改,职能要求和产出。

void htoi(const char *ptr, char *binAddr) {
char value[32] = "";
char ch = *ptr;
int i;
const char* quads[] = {"0000", "0001", "0010", "0011", "0100", "0101",
                     "0110", "0111", "1000", "1001", "1010", "1011",
                     "1100", "1101", "1110", "1111"};

while (ch ==     || ch ==  	 )
    ch = *(++ptr);

for (i = 0; i < 8; i++) {
    if (ch >=  0  && ch <=  9 )
        strncat(value, quads[ch -  0 ], 4);
    if (ch >=  A  && ch <=  F )
        strncat(value, quads[10 + ch -  A ], 4);
    if (ch >=  a  && ch <=  f )
        strncat(value, quads[10 + ch -  a ], 4);

    ch = *(++ptr);
    printf("%s
", value);
}

*binAddr = *value;
}

我的职责是:

char line[11], hexAddr[8], binAddr[32];
htoi(hexAddr, binAddr);
printf("%s
", binAddr);

这里是产出(当投入时为001133c0):

0000

单位:千美元

单位:千美元0001

单位:千美元00010001

单位:千美元000100010011

单位:千美元0001000100110011

单位:千美元00010001001100111100

单位:千美元000100010011001111000000

页: 1

The last line (with the special characters) is the printf(binAddr) in the main function above. It is clear from the printf statements inside the function that the binary code is being constructed correctly.

我再说一遍,这并不值得一提操纵。 我做了什么错误?

最佳回答

概览表非常容易:

const char * const quads = { "0000", "0001", "0010", .... };

const char * hex_to_bin_quad(unsigned char c)
{
  if (c >=  0  && c <=  9 ) return quads[     c -  0 ];
  if (c >=  A  && c <=  F ) return quads[10 + c -  A ];
  if (c >=  a  && c <=  f ) return quads[10 + c -  a ];
  return -1;
}

现在按您的顺序排列如下:hex_to_bin_quad(c),每种特性(例如,使用strncat)。 你们已经知道,你需要一个定点,即:<代码>4 * strlen(src) + 1,这样就可以分配、储存和压缩。

问题回答

暂无回答




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签