十六进制字符串转换为整数用C [英] Hexadecimal String Conversion to Integer in C

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

问题描述

我的十六进制字符一个4字节的字符串,我想将它们转换成在C 2字节整数。

我不能使用与strtol,或fprintf中的fscanf。

我想这一点: -

 无符号字符*十六进制串=12FF

要被转换成这样的: -

 无符号整数hexInt = 0x12FF


解决方案

编辑:卫生署,刚读azmuhak所建议的链接。这肯定是这个问题的一个副本。在azmuhak的链接答案也更加完整,因为它以0xprefixes ...

交易

下面将一起工作的退出使用标准库...
看到它在 ideone这里

 的#include<&stdio.h中GT;#定义ASCII_0_VALU 48
#定义ASCII_9_VALU 57
#定义ASCII_A_VALU 65
#定义ASCII_F_VALU 70unsigned int类型HexStringToUInt(字符常量*十六进制串)
{
    unsigned int类型的结果= 0;
    字符常量* C =十六进制串;
    焦炭thisC;    而((thisC = * C)!= NULL)
    {
        无符号整型加;
        thisC = TOUPPER(thisC);        导致与所述;&下; = 4;        如果(thisC> = ASCII_0_VALU和放大器;&安培; thisC< = ASCII_9_VALU)
            添加= thisC - ASCII_0_VALU;
        否则,如果(thisC> = ASCII_A_VALU和放大器;&安培; thisC< = ASCII_F_VALU)
            添加= thisC - ASCII_A_VALU + 10;
        其他
        {
            的printf(未确认的十六进制字符\\%C \\\\ n,thisC);
            出口(-1);
        }        结果+ =增加;
        ++℃;
    }    返回结果;
}INT主要(无效)
{
    的printf(\\ nANSWER(\\12FF \\)数:%d \\ n,HexStringToUInt(12FF));
    的printf(\\ nANSWER(\\ABCD \\)数:%d \\ n,HexStringToUInt(ABCD));    返回0;
}

在code可以作出更有效,我使用 TOUPPER 库函数,但是你可以很容易地实现了自己......

此外,这将无法解析以0x开头的字符串...但你可以在函数的开头添加一个快速检查为,只是咀嚼这些字符...

I have a 4 byte string of hex characters and I want to convert them into a 2 byte integer in c.

I cannot use strtol, fprintf or fscanf.

I want this:-

unsigned char *hexstring = "12FF";

To be converted to this:-

unsigned int hexInt = 0x12FF

解决方案

EDIT: Doh, just read azmuhak's suggested link. This is definitely a duplicate of that question. The answer in azmuhak's link is also more complete because it deals with "0x" prefixes...

The following will work with out using the standard library... See it on ideone here

#include <stdio.h>

#define ASCII_0_VALU 48
#define ASCII_9_VALU 57
#define ASCII_A_VALU 65
#define ASCII_F_VALU 70

unsigned int HexStringToUInt(char const* hexstring)
{
    unsigned int result = 0;
    char const *c = hexstring;
    char thisC;

    while( (thisC = *c) != NULL )
    {
        unsigned int add;
        thisC = toupper(thisC);

        result <<= 4;

        if( thisC >= ASCII_0_VALU &&  thisC <= ASCII_9_VALU )
            add = thisC - ASCII_0_VALU;
        else if( thisC >= ASCII_A_VALU && thisC <= ASCII_F_VALU)
            add = thisC - ASCII_A_VALU + 10;
        else
        {
            printf("Unrecognised hex character \"%c\"\n", thisC);
            exit(-1);
        }

        result += add;
        ++c;
    }

    return result;  
}

int main(void) 
{
    printf("\nANSWER(\"12FF\"): %d\n", HexStringToUInt("12FF"));
    printf("\nANSWER(\"abcd\"): %d\n", HexStringToUInt("abcd"));

    return 0;
}

The code could be made more efficient and I use the toupper library function, but you could easily implement that yourself...

Also, this won't parse strings beginning with "0x"... but you could add a quick check for that at the beginning of the function and just chew up those characters...

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

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