1. 程式人生 > >Python刷題筆記(3)- 16進位制和ascii碼互轉

Python刷題筆記(3)- 16進位制和ascii碼互轉

今天看了下等級標示,原來kyu上面還有dan的等級,升級路漫漫,今天是5kyu題目

題目:

Write a module Converter that can take ASCII text and convert it tohexadecimal. The class should also be able to take hexadecimal andconvert it to ASCII text.

Example

Converter.to_hex("Look mom, no hands")
=> "4c6f6f6b206d6f6d2c206e6f2068616e6473"

Converter.to_ascii("4c6f6f6b206d6f6d2c206e6f2068616e6473"
) => "Look mom, no hands"

中文簡介:

就是寫個ASCII碼和16進位制的轉換器,可以互相轉換。

想法:

1、字串轉16進位制,直接使用ord()函式然後逐個轉換為16進位制,再將16進位制數轉化成字串形式加入到列表,再把列表合成字串返回

2、16進位制轉換字串,間隔為2歷遍字串,然後將16進位制字串轉換成10進位制數字,用char()函式轉換為ASCII碼加入列表,然後把列表合成字串返回。

3、int(str,16)可以實現字串轉換成16進位制,str()實現轉換字串,hex()函式轉換為16進位制,但是返回的是0x00形式

實現:

class Converter():
    @staticmethod
    def to_ascii(h):
        list_s = []
        for i in range(0,len(h),2):
            list_s.append(chr(int(h[i:i+2].upper(),16)))
        return ''.join(list_s)
    @staticmethod
    def to_hex(s):
        list_h = []
        for c in s:
            list_h.append(str(hex(ord(c)))[-2:]) #取hex轉換16進位制的後兩位
        return ''.join(list_h)

比較總結:

提交後看到別人的方法實在是簡單。。直接使用系統函式encode("hex")和decode("hex")就解決了。。感覺有點作弊啊。。

相關推薦

Python筆記3- 16ascii

今天看了下等級標示,原來kyu上面還有dan的等級,升級路漫漫,今天是5kyu題目 題目: Write a module Converter that can take ASCII text and convert it tohexadecimal. The class

C語言 16ascii

/*把ASCII字元轉換為16進位制 */ uint8_t char_to_hex(const uint8_t *ch) { uint8_t value = 0; if(*ch >= 0 && *ch <= 9

Python筆記2- 取5大數字

In the following 6 digit number: 283910 91 is the greatest sequence of 2 digits. Complete the solution so that it returns the largest five digit number fo

Python筆記5- 秒轉化為時間

題目: Write a function, which takes a non-negative integer (seconds) as input and returns the time in a human-readable format (HH:MM:SS)

Python筆記4- 字串重組

題目: Complete the method/function so that it converts dash/underscore delimited words into camel casing. The first word within the output

Python筆記1- 數獨判斷

Write a function done_or_not passing a board (list[list_lines]) as parameter. If the board is valid return 'Finished!', otherwise return 'Try again!' Sudo

Python內建轉換函式(實現16ASCII轉換)

在進行wireshark抓包時你會發現底端視窗報文內容左邊是十六進位制數字,右邊是每兩個十六進位制轉換的ASCII字元,這裡使用Python程式碼實現一個十六進位制和ASCII的轉換方法。 hex() 轉換一個整數物件為十六進位制的字串 >>> hex(16) '0x10' >&

python將字串16ASCii的值

binascii.a2b_hex(hexstr) binascii.unhexlify(hexstr) Return the binary data represented by the hexadecimal string hexstr. This function

C# 中2,10,16及其ASCII之間轉化

另外將16進位制string轉byte byte b = Convert.ToByte("1A",16); C#串列埠傳送資料  懸賞分:5 - 解決時間:2009-10-19 21:15 我想給串列埠傳送十六進位制命令,命令字串是:“00 00 00 1B 54 59 55 54 00 00 00 00 0

2018QBXT遊記3

【2018QBXT刷題遊記】 Day1 TEST1 T3 difer 【問題描述】 在數學中, 對光滑函式求微分是一種常見的操作。 在實際應用中, 一些函式沒有解析形式, 通常會從函式上取若干個點,用這些點來近似地表示這個函式。 現在有一個函式\(f(x)\),我們在函式上取 \(n\)個值 \(f(1

LeetCode筆記

問題一:兩數相加 給定兩個非空連結串列來表示兩個非負整數,位數按照逆序方式儲存,它們的每個節點只儲存單個數字,將兩數相加返回一個新的連結串列。 例項: 輸入:(2 -> 4->3) + (5->6->4) 輸出:7 -> 0 - >

LeetCode筆記

4. 兩個排序陣列的中位數 給定兩個大小為 m 和 n 的有序陣列 nums1 和 nums2 。 請找出這兩個有序陣列的中位數。要求演算法的時間複雜度為 O(log (m+n)) 。 你可以假設 nums1 和 nums2 不同時為空。 示例 1: nums1 =

Rrui的Leetcode筆記

102. 二叉樹的層次遍歷 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode

Rrui的Leetcode筆記

225. 用佇列實現棧 class MyStack { public: /** Initialize your data structure here. */ queue<int> a; int k=0; MyStack

Rrui的Leetcode筆記

388. 檔案的最長絕對路徑 class Solution { public: int lengthLongestPath(string input) { int res = 0, n = input.size(), level = 0;

Rrui的Leetcode筆記

448. 找到所有陣列中消失的數字 class Solution { public: vector<int> findDisappearedNumbers(vector<int>& nums) { vector&l

牛客華為機試題筆記

馬上華為提前批開始了,嚇得我趕緊上牛客網刷題,記錄如下: 所有程式碼都在github 1.字串最後一個單詞的長度 一段英文字串中最後一個單詞的長度。 題目比較簡單,做法有很多: 比如, 可以放到stringstream裡面split,拿到最後一個單詞

LeetCode筆記:binary-tree-maximum-path-sum

題目描述 Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example:

LeetCode筆記貪心:maximum-subarray

題目描述 Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For exam

LeetCode筆記模擬:pascals-triangle

題目描述 Given numRows, generate the first numRows of Pascal’s triangle. For example, given numRows = 5, Return [