-
Notifications
You must be signed in to change notification settings - Fork 0
/
ascii_to_hex.py
39 lines (29 loc) · 889 Bytes
/
ascii_to_hex.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# Python3 program to convert ASCII
# string to Hexadecimal format string
# love you s much
# function to convert ASCII to HEX
def ASCIItoHEX(ascii):
# Initialize final String
hexa = ""
# Make a loop to iterate through
# every character of ascii string
for i in range(len(ascii)):
# take a char from
# position i of string
ch = ascii[i]
# cast char to integer and
# find its ascii value
in1 = ord(ch)
# change this ascii value
# integer to hexadecimal value
part = hex(in1).lstrip("0x").rstrip("L")
# add this hexadecimal value
# to final string.
hexa += part
# return the final string hex
return hexa
# Driver Function
if __name__ == '__main__':
# print the Hex String
print(ASCIItoHEX(" "))
# This code is contributed by pratham76