生活随笔
收集整理的這篇文章主要介紹了
C语言 十进制整数字符串转十六进制字符串
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
【問題描述】 例如:輸入字符串str1[]=“87632394”,需轉(zhuǎn)成字符串str2[]=“5392A0A”
【解決方案】 方案1: 先使用strtol函數(shù)將字符串轉(zhuǎn)成十進(jìn)制數(shù),再通過sprintf函數(shù)將十進(jìn)制數(shù)按十六進(jìn)制字符串形式輸出到字符數(shù)組保存 該方案弊端:轉(zhuǎn)換的數(shù)不能大于0x7FFFFFFF,因為strtol的返回值是long型
#include <stdio.h>
#include <string.h>
#include <stdlib.h> int main ( void )
{ char str1
[ ] = "87632394" ; char str2
[ 100 ] = { 0 } ; char * endptr
; long data
= 0 ; data
= strtol ( str1
, & endptr
, 10 ) ; printf ( "data=%ld\r\n" , data
) ; sprintf ( str2
, "%X" , data
) ; printf ( "str2=%s\r\n" , str2
) ; return 0 ;
}
運行結(jié)果: 方案2: 解決當(dāng)要轉(zhuǎn)換的值大于0x7FFFFFFF,如何處理 例如:輸入str1[]="86281102005404”,需轉(zhuǎn)成str2[]=“4E78E26F949C” 首先要解決的是如何把"86281102005404”轉(zhuǎn)成數(shù)字保存 借atol函數(shù)的理念來自己實現(xiàn)一個
#include <stdio.h> typedef enum { FALSE
= 0 , TRUE
= ! FALSE
} BOOL
;
__int64
my_atoll ( char * str
)
{ __int64 s
= 0 ; BOOL isMinus
= FALSE
; while ( * str
== ' ' ) { str
++ ; } if ( * str
== '+' || * str
== '-' ) { if ( * str
== '-' ) isMinus
= TRUE
; str
++ ; } else if ( * str
< '0' || * str
> '9' ) return 0 ; while ( * str
!= '\0' && * str
>= '0' && * str
<= '9' ) { s
= s
* 10 + * str
- '0' ; str
++ ; } return s
* ( isMinus
? - 1 : 1 ) ;
}
int main ( void )
{ char str
[ ] = "86281102005404" ; printf ( "str=%lld\r\n" , my_atoll ( str
) ) ; return 0 ;
}
已驗證:(以上代碼在VS2019上運行測試,在VC++6.0上不支持%lld打印) 然后要把數(shù)字轉(zhuǎn)成十六進(jìn)制字符,依然可以使用sprintf
#include <stdio.h> typedef enum { FALSE
= 0 , TRUE
= ! FALSE
} BOOL
;
__int64
my_atoll ( char * str
)
{ __int64 s
= 0 ; BOOL isMinus
= FALSE
; while ( * str
== ' ' ) { str
++ ; } if ( * str
== '+' || * str
== '-' ) { if ( * str
== '-' ) isMinus
= TRUE
; str
++ ; } else if ( * str
< '0' || * str
> '9' ) return 0 ; while ( * str
!= '\0' && * str
>= '0' && * str
<= '9' ) { s
= s
* 10 + * str
- '0' ; str
++ ; } return s
* ( isMinus
? - 1 : 1 ) ;
} int main ( void )
{ char str
[ ] = "86277343909020" ; char strhex
[ 100 ] = { 0 } ; long long data
= 0 ; data
= my_atoll ( str
) ; printf ( "str=%lld\r\n" , data
) ; if ( data
> 0xFFFFFFFF ) { sprintf ( strhex
, "%X%08X" , ( unsigned int ) ( data
>> 32 ) , ( unsigned int ) data
) ; } else { sprintf ( strhex
, "%X" , ( unsigned int ) data
) ; } printf ( "strhex=%s\r\n" , strhex
) ; return 0 ;
}
運行結(jié)果: 注意,當(dāng)數(shù)據(jù)大于0xFFFFFFFF,sprintf中%08X是必要的,不要寫成%X,因為0需要打出來。
方案三: 接上面的方案二,my_atoll還是要用,只是將數(shù)字轉(zhuǎn)成十六進(jìn)制字符串可以自己實現(xiàn)。
void ltoh ( long long x
, char * p
)
{ int div
; int cnt
= 0 ; if ( x
== 0 ) { * p
= '0' ; } if ( x
< 0 ) { x
= - x
; * p
= '-' ; p
++ ; } for ( int i
= 0 ; x
!= 0 ; i
++ ) { div
= x
% 16 ; x
= x
/ 16 ; if ( ( div
- 10 ) < 0 ) * ( p
+ i
) = div
+ '0' ; else * ( p
+ i
) = 'A' + div
- 10 ; div
= 0 ; cnt
++ ; } for ( int i
= 0 ; i
< cnt
/ 2 ; i
++ ) { char temp
= p
[ i
] ; p
[ i
] = p
[ cnt
- 1 - i
] ; p
[ cnt
- 1 - i
] = temp
; }
} int main ( void )
{ char str
[ ] = "86281102005404" ; char strhex
[ 100 ] = { 0 } ; long long data
= 0 ; data
= my_atoll ( str
) ; printf ( "str=%lld\r\n" , data
) ; ltoh ( data
, strhex
) ; printf ( "strhex=%s\r\n" , strhex
) ; return 0 ;
}
my_atoll函數(shù)見方案二。 運行結(jié)果: 如果比0xFFFF FFFF FFFF FFFF更大的數(shù)要轉(zhuǎn)成十六進(jìn)制字符串,就不在這里討論了
PS. vc++6.0由于版本過于陳舊,不支持long long,可以改用__int64來定義 報錯:error C2632: ‘long’ followed by ‘long’ is illegal 微軟最終在Visual Studio 2013上增加了對long long和unsigned long long的支持。
總結(jié)
以上是生活随笔 為你收集整理的C语言 十进制整数字符串转十六进制字符串 的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔 推薦給好友。