Skip to content

Instantly share code, notes, and snippets.

@maliubiao
Created October 1, 2014 06:20
Show Gist options
  • Save maliubiao/2f26f1aa7a54f79ffc0b to your computer and use it in GitHub Desktop.
Save maliubiao/2f26f1aa7a54f79ffc0b to your computer and use it in GitHub Desktop.
转换整型到某base的字符串
hex_table = {
0: "0",
1: "1",
2: "2",
3: "3",
4: "4",
5: "5",
6: "6",
7: "7",
8: "8",
9: "9",
10: "a",
11: "b",
12: "c",
13: "d",
14: "e",
15: "f"
}
def itoa(h, base):
if base > 16:
return
neg = 0
if h < 0:
neg = 1
h = -h
b = []
while h:
r = h % base
b.append(hex_table[r])
h = h / base
b.reverse()
if not neg:
return "".join(b)
else:
return "-"+"".join(b)
#for i in range(1000, 1100):
# print "{:<5}{:<10}".format(i, itoa(i, 16))
1000 3e8
1001 3e9
1002 3ea
1003 3eb
1004 3ec
1005 3ed
1006 3ee
1007 3ef
1008 3f0
1009 3f1
1010 3f2
1011 3f3
1012 3f4
1013 3f5
1014 3f6
1015 3f7
1016 3f8
1017 3f9
1018 3fa
1019 3fb
1020 3fc
1021 3fd
1022 3fe
1023 3ff
1024 400
1025 401
1026 402
1027 403
1028 404
1029 405
1030 406
1031 407
1032 408
1033 409
1034 40a
1035 40b
1036 40c
1037 40d
1038 40e
1039 40f
1040 410
1041 411
1042 412
1043 413
1044 414
1045 415
1046 416
1047 417
1048 418
1049 419
1050 41a
1051 41b
1052 41c
1053 41d
1054 41e
1055 41f
1056 420
1057 421
1058 422
1059 423
1060 424
1061 425
1062 426
1063 427
1064 428
1065 429
1066 42a
1067 42b
1068 42c
1069 42d
1070 42e
1071 42f
1072 430
1073 431
1074 432
1075 433
1076 434
1077 435
1078 436
1079 437
1080 438
1081 439
1082 43a
1083 43b
1084 43c
1085 43d
1086 43e
1087 43f
1088 440
1089 441
1090 442
1091 443
1092 444
1093 445
1094 446
1095 447
1096 448
1097 449
1098 44a
1099 44b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment