什么是十六进制数字串?

时间:2013-11-19 05:57:55

标签: c string hex

既然问题已经得到解答,我将使用我从大家那里得到的帮助发布我的解决方案(谢谢!)

我不喜欢的主要事情是假设每个输入都以'\ n'结尾,因为我的linux终端没有!还有一种更优雅的方式将A-F / a-f转换为十进制,也许使用我自己编写的辅助函数(除了pow和printf之外,还没有任何函数的访问权限)!

我正在做K& R C编程语言,练习2.3:

“写一个函数,htoi(s),它将一个十六进制数字(包括一个可选的0x或0X)转换为它的等效整数值。允许的数字是0到0 9,a到f,和A到F.“

#include <stdio.h>
#include <math.h>

#define MAXLEN 1000                                /* maximum accepted user input is 999 digits */

int htoi( char s[], int maxPower ) ;               /* maxPower will be two less than string length (len - 2) because the exponentiation is 0-indexed                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
                                                   *  remove the length values which represent '\n' and '\0' in user input                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
                                                   *  if the string begins with "0x" or "0X", we remove two more from the power variable inside the htoi() body. */

int main( void )
{
  int c, i ;
  int len ;                                        /* tracks length of hexString, used to determine exponent value */
  char hexString[ MAXLEN ] ;

  len = 0 ;
  for ( i = 0 ; i < MAXLEN - 1 && ( c = getchar() ) != EOF ; ++i ) {
    hexString[i] = c ;
    ++len ;
  }
  hexString[len] = '\0' ;                          /* value of len is one more than termination value of i in above loop */

  printf( "Hex String: %s\nInt: %d\n", hexString, htoi( hexString, len - 2 ) ) ;
  return 0 ;
}

int htoi( char s[], int power )
{
  int j, i, n, decimal ;

  n = 0 ;
  if ( s[0] == '0' && ( s[1] == 'x' || s[1] == 'X' ) ) {  /* cutting off first two array values if string begins with 0x or 0X */
      j = 0 ;
      while ( ( s[j] = s[j + 2] ) != '\0' ) {
        ++j ;
      }
      power = power - 2 ;                                 /* maximum exponent value now represents the usable data */
    }

    for ( i = 0 ; s[i] != '\n' && s[i] != '\0' ; ++i ) {
      if ( s[i] >= '0' && s[i] <= '9' ) {
        decimal = s[i] - '0' ;
        n = ( decimal * pow( 16, power ) ) + n ;
        --power ;
      } else if ( s[i] == 'A' || s[i] == 'a' ) {
        decimal = 10 ;
        n = ( decimal * pow( 16, power ) ) + n ;
        --power ;
      } else if ( s[i] == 'B' || s[i] == 'b' ) {
        decimal = 11 ;
        n = ( decimal * pow( 16, power ) ) + n ;
        --power ;
      } else if ( s[i] == 'C' || s[i] == 'c' ) {
        decimal = 12 ;
        n = ( decimal * pow( 16, power ) ) + n ;
        --power ;
      } else if ( s[i] == 'D' || s[i] == 'd' ) {
        decimal = 13 ;
        n = ( decimal * pow( 16, power ) ) + n ;
        --power ;
      } else if ( s[i] == 'E' || s[i] == 'e' ) {
        decimal = 14 ;
        n = ( decimal * pow( 16, power ) ) + n ;
        --power ;
      } else if ( s[i] == 'F' || s[i] == 'f' ) {
        decimal = 15 ;
        n = ( decimal * pow( 16, power ) ) + n ;
        --power ;
      } else {
        printf( "ERROR 69:\nInput string was not hexidecimal (0-9, A-F, a-f)\nResult is 0!\n" ) ;
        n = 0;
        return n;
      }
    }
    return n ;
}

3 个答案:

答案 0 :(得分:2)

是什么意思是像 AB0C5342F 这样的字符串。用C表示:

char s[] = "AB0C5342F";

答案 1 :(得分:1)

字符表示为ASCII,它们是1字节长,一个字节可以用十六进制格式的2位数表示,例如255是0xFF。你需要做的是从hexa转换为charachters然后构建你的字符串。

请记住,要检索角色的ascii,您可以执行以下操作: int a ='a'

答案 2 :(得分:0)

通过十六进制数字串,它们的含义是数字0-9和字符A-F的组合,就像二进制字符串是0和1的组合一样。 例如:“245FC”是十六进制字符串。