十六度进制负数如何转换为十进制数

ldkcumt 2013-11-04 11:45:47
例如:_itoa(-1000,buffer,16);
可以将 -1000转换为 FFFFFC18;
但反过来如何把 FFFFFC18 转换为 -1000呢?
我用 strtol( "FFFFFC18", NULL, 16 );得到是一个很大的正数。
...全文
795 6 打赏 收藏 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
mujiok2003 2013-11-04
  • 打赏
  • 举报
回复
#include <stdio.h>
int main()
{
  int a = 0;
  sscanf("-1000", "%x", &a);
  printf("%+d",a);
  
  return 0;
}
//-4096
赵4老师 2013-11-04
  • 打赏
  • 举报
回复
纠正1楼: strtol, wcstol Convert strings to a long-integer value. long strtol( const char *nptr, char **endptr, int base ); long wcstol( const wchar_t *nptr, wchar_t **endptr, int base ); Routine Required Header Compatibility strtol <stdlib.h> ANSI, Win 95, Win NT wcstol <stdlib.h> or <wchar.h> ANSI, Win 95, Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value strtol returns the value represented in the string nptr, except when the representation would cause an overflow, in which case it returns LONG_MAX or LONG_MIN. strtol returns 0 if no conversion can be performed. wcstol returns values analogously to strtol. For both functions, errno is set to ERANGE if overflow or underflow occurs. Parameters nptr Null-terminated string to convert endptr Pointer to character that stops scan base Number base to use Remarks The strtol function converts nptr to a long. strtol stops reading the string nptr at the first character it cannot recognize as part of a number. This may be the terminating null character, or it may be the first numeric character greater than or equal to base. wcstol is a wide-character version of strtol; its nptr argument is a wide-character string. Otherwise these functions behave identically. Generic-Text Routine Mappings TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined _tcstol strtol strtol wcstol The current locale’s LC_NUMERIC category setting determines recognition of the radix character in nptr; for more information, see setlocale. If endptr is not NULL, a pointer to the character that stopped the scan is stored at the location pointed to by endptr. If no conversion can be performed (no valid digits were found or an invalid base was specified), the value of nptr is stored at the location pointed to by endptr. strtol expects nptr to point to a string of the following form: [whitespace] [{+ | –}] [0 [{ x | X }]] [digits] A whitespace may consist of space and tab characters, which are ignored; digits are one or more decimal digits. The first character that does not fit this form stops the scan. If base is between 2 and 36, then it is used as the base of the number. If base is 0, the initial characters of the string pointed to by nptr are used to determine the base. If the first character is 0 and the second character is not 'x' or 'X', the string is interpreted as an octal integer; otherwise, it is interpreted as a decimal number. If the first character is '0' and the second character is 'x' or 'X', the string is interpreted as a hexadecimal integer. If the first character is '1' through '9', the string is interpreted as a decimal integer. The letters 'a' through 'z' (or 'A' through 'Z') are assigned the values 10 through 35; only letters whose assigned values are less than base are permitted. Example /* STRTOD.C: This program uses strtod to convert a * string to a double-precision value; strtol to * convert a string to long integer values; and strtoul * to convert a string to unsigned long-integer values. */ #include <stdlib.h> #include <stdio.h> void main( void ) { char *string, *stopstring; double x; long l; int base; unsigned long ul; string = "3.1415926This stopped it"; x = strtod( string, &stopstring ); printf( "string = %s\n", string ); printf(" strtod = %f\n", x ); printf(" Stopped scan at: %s\n\n", stopstring ); string = "-10110134932This stopped it"; l = strtol( string, &stopstring, 10 ); printf( "string = %s", string ); printf(" strtol = %ld", l ); printf(" Stopped scan at: %s", stopstring ); string = "10110134932"; printf( "string = %s\n", string ); /* Convert string using base 2, 4, and 8: */ for( base = 2; base <= 8; base *= 2 ) { /* Convert the string: */ ul = strtoul( string, &stopstring, base ); printf( " strtol = %ld (base %d)\n", ul, base ); printf( " Stopped scan at: %s\n", stopstring ); } } Output string = 3.1415926This stopped it strtod = 3.141593 Stopped scan at: This stopped it string = -10110134932This stopped it strtol = -2147483647 Stopped scan at: This stopped itstring = 10110134932 strtol = 45 (base 2) Stopped scan at: 34932 strtol = 4423 (base 4) Stopped scan at: 4932 strtol = 2134108 (base 8) Stopped scan at: 932 Data Conversion Routines | Locale Routines | strtod Functions Overview See Also strtod, strtoul, atof, localeconv, setlocale
#include <stdio.h>
#include <stdlib.h>
int main() {
    long l;
    char *e;
    l=strtol("FFFFFC18", &e, 16 );
    printf("%ld\n",l);//2147483647
    sscanf("FFFFFC18","%X",&l);
    printf("%ld\n",l);//-1000
    return 0;
}
赵4老师 2013-11-04
  • 打赏
  • 举报
回复
strtol( "FFFFFFFFFFFFFC18", NULL, 16 );
ldkcumt 2013-11-04
  • 打赏
  • 举报
回复
更正一下:关于数据类型 long都是32位的
ldkcumt 2013-11-04
  • 打赏
  • 举报
回复
xiaohuh421 君 正解。 虽然不太明白,但可以实现。 另外,我写的是32位的MFC应用程序(虽然用的是WIN7 64位操作系统),所以strtoul的第一个参数必须是 "FFFFFC18", 如果写64位应用程序,则第一个参数是"FFFFFFFFFFFFFC18".
xiaohuh421 2013-11-04
  • 打赏
  • 举报
回复
对于strtol函数, 有溢出处理的. strtol的源码中 实际调用的是strtol.c文件中的strtoxl, 在strtoxl的源中有如下代码:
/* overflow or signed overflow occurred */
            errno = ERANGE;
            if ( flags & FL_UNSIGNED )
                number = ULONG_MAX;
            else if ( flags & FL_NEG )
                number = (unsigned long)(-LONG_MIN);
            else
                number = LONG_MAX;
可以明显看到, 如果不是无符号数, 超过了0x7FFFFFFF就强制等于0x7FFFFFFF. 所以你想格式化任意的16进制,有符号无符号. 可以使用strtoul然后再转换成有符号. long iVal = (long)strtoul( "FFFFFC18", NULL, 16 );
内容概要:本文聚焦于电力电子化电力系统中变换器端口导纳的黑盒提取与振荡模式辨识,结合Matlab与PSCAD仿真平台开展建模、仿真与分析。通过黑盒建模方法提取变换器在不同工况下的端口导纳特性,利用频域扫描与系统辨识技术获得其小信号阻抗模型,并基于广义奈奎斯特判据等频域稳定性分析工具对系统的潜在振荡模式进行精确辨识与评估。研究涵盖了小信号建模、阻抗测量、频域稳定性判据应用等关键技术,旨在解决高比例电力电子设备接入引起的次同步振荡、宽频振荡等稳定性难题,提升复杂电力系统的安全稳定运行能力。; 适合人群:具备电力系统分析、电力电子技术及自动控制理论基础的研究生、科研人员,以及从事新能源并网、微电网、柔性输电系统等领域的工程技术人员。; 使用场景及目标:① 掌握电力电子变换器的黑盒导纳建模方法,理解其在多变换器交互系统中的等效动态行为;② 学习并应用阻抗分析法进行系统级频域稳定性评估,深入理解振荡机理与失稳诱因;③ 熟练运用Matlab与PSCAD联合仿真平台,完成从建模、小信号扰动仿真、数据采集到频域分析的全流程研究,服务于新型电力系统稳定性分析与控制器设计优化。; 阅读建议:建议读者在掌握电力系统小信号稳定性理论的基础上,结合文中提供的Matlab与PSCAD仿真案例进行实践操作,重点关注阻抗提取的仿真设置、频率扫描策略、数据处理方法以及Nyquist判据在多输入多输出系统中的应用细节,以深入理解系统振荡的成因、传播路径及抑制策略。
内容概要:本文围绕基于混沌系统与DNA编码的彩色数字图像加密解密体系展开研究,提出了一种多层级、高复杂度的复合加密方案。通过结合混沌系统的伪随机遍历特性与DNA编码的海量组合优势,实现了对彩色图像RGB三通道的像素混淆与扩散,有效提升了加密算法的密钥空间与抗破解能力。研究重点包括加密解密流程设计、密钥敏感性分析,并系统性开展了抗噪声与抗裁剪性能测试。实验结果表明,该算法在面对高斯噪声、椒盐噪声及不同程度的图像裁剪攻击时,仍能较好地恢复图像主体结构与核心信息,展现出优异的鲁棒性与实用性。; 适合人群:具备一定图像处理或信息安全基础知识的科研人员、研究生及从事多媒体安全技术开发的工程技术人员。; 使用场景及目标:①用于保障彩色图像在开放网络环境中的安全传输与存储;②应对图像在传输过程中遭受的噪声干扰与恶意裁剪攻击;③为高鲁棒性图像加密算法的设计与评估提供理论依据和技术参考。; 阅读建议:建议读者结合Matlab代码实现,深入理解混沌序列生成、DNA编码映射及像素置乱扩散的具体过程,并通过复现实验验证算法在不同干扰场景下的性能表现。

65,214

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧