c_cpp 十进制→十六进制(int→string)

- [C ++中二进制,字符串,十六进制,十进制之间的转换](https://blog.csdn.net/MOU_IT/article/details/89060249#4%E3%80%81%E5%8D %81%E8%BF%9B%E5%88%B6%E5%92%8C%E5%8D%81%E5%85%AD%E8%BF%9B%E5%88%B6%E7%9B%B8 %E4%BA%92%E8%BD%AC%E6%8D%A2)

dec2hex.cpp
#include <bits/stdc++.h>
using namespace std;

int main() {
	int x = 26;
	string s;
	stringstream ss;
	ss << hex << x;
	ss >> s;
	transform(s.begin(), s.end(), s.begin(), ::toupper);
	cout << s << endl;	// 1A
	return 0;
}

c_cpp Base58.c

base58.c
/*******************************************************************************
*   Ark Wallet
*   (c) 2017 Ledger
*   (c) ARK Ecosystem
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
********************************************************************************/

#include "utils/base58.h"

#include <os.h>

#include "constants.h"
#include "hashing.h"

//////////////////////////////////////////////////////////////////////

#define BASE58_ALPHABET_LENGTH      58U
#define BASE58_ENCODE_BUFFER_LENGTH 164U
#define BASE58_TABLE_LEN            128U
#define UINT256_BYTE_LENGTH         256U

//////////////////////////////////////////////////////////////////////

const uint8_t const BASE58TABLE[] = {
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff,  0x0,  0x1,  0x2,  0x3,  0x4,  0x5,  0x6,  0x7,  0x8, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff,  0x9,  0xa,  0xb,  0xc,  0xd,  0xe,  0xf,
    0x10, 0xff, 0x11, 0x12, 0x13, 0x14, 0x15, 0xff, 0x16, 0x17, 0x18, 0x19,
    0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
    0xff, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
    0x37, 0x38, 0x39, 0xff, 0xff, 0xff, 0xff, 0xff
};

const uint8_t const BASE58ALPHABET[] = {
    '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
    'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
    'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'm',
    'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
};

//////////////////////////////////////////////////////////////////////

static uint8_t decodeBase58(uint8_t WIDE *in,
                            uint8_t length,
                            uint8_t *out,
                            uint8_t maxoutlen) {
    uint8_t tmp[BASE58_ENCODE_BUFFER_LENGTH];
    uint8_t buffer[BASE58_ENCODE_BUFFER_LENGTH];
    uint8_t i;
    uint8_t j;
    uint8_t startAt;
    uint8_t zeroCount = 0U;

    if (length > sizeof(tmp)) {
        THROW(INVALID_PARAMETER);
    }

    os_memmove(tmp, in, length);
    for (i = 0U; i < length; i++) {
        if (in[i] > BASE58_TABLE_LEN) {
            THROW(EXCEPTION);
        }

        tmp[i] = BASE58TABLE[in[i]];
        if (tmp[i] == 0xFF) {
            THROW(EXCEPTION);
        }
    }

    while ((zeroCount < length) && (tmp[zeroCount] == 0U)) {
        ++zeroCount;
    }

    j = length;
    startAt = zeroCount;
    while (startAt < length) {
        uint16_t remainder = 0U;
        uint8_t divLoop;

        for (divLoop = startAt; divLoop < length; divLoop++) {
            uint16_t digit256 = (uint16_t)(tmp[divLoop] & 0xff);
            uint16_t tmpDiv = remainder * BASE58_ALPHABET_LENGTH + digit256;
            tmp[divLoop] = (uint8_t)(tmpDiv / UINT256_BYTE_LENGTH);
            remainder = (tmpDiv % UINT256_BYTE_LENGTH);
        }

        if (tmp[startAt] == 0U) {
            ++startAt;
        }

        buffer[--j] = (uint8_t)remainder;
    }

    while ((j < length) && (buffer[j] == 0U)) {
        ++j;
    }

    length = length - (j - zeroCount);
    if (maxoutlen < length) {
        THROW(EXCEPTION_OVERFLOW);
    }

    os_memmove(out, buffer + j - zeroCount, length);
    return length;
}

//////////////////////////////////////////////////////////////////////

static uint8_t encodeBase58(uint8_t WIDE *in,
                            uint8_t length,
                            uint8_t *out,
                            uint8_t maxoutlen) {
    uint8_t tmp[BASE58_ENCODE_BUFFER_LENGTH];
    uint8_t buffer[BASE58_ENCODE_BUFFER_LENGTH];
    uint8_t j;
    uint8_t startAt;
    uint8_t zeroCount = 0;

    if (length > sizeof(tmp)) {
        THROW(INVALID_PARAMETER);
    }

    os_memmove(tmp, in, length);
    while ((zeroCount < length) && (tmp[zeroCount] == 0U)) {
        ++zeroCount;
    }

    j = 2U * length;
    startAt = zeroCount;
    while (startAt < length) {
        uint16_t remainder = 0U;
        uint8_t divLoop;

        for (divLoop = startAt; divLoop < length; divLoop++) {
            uint16_t digit256 = (uint16_t)(tmp[divLoop] & 0xff);
            uint16_t tmpDiv = remainder * UINT256_BYTE_LENGTH + digit256;
            tmp[divLoop] = (uint8_t)(tmpDiv / BASE58_ALPHABET_LENGTH);
            remainder = (tmpDiv % BASE58_ALPHABET_LENGTH);
        }

        if (tmp[startAt] == 0U) {
            ++startAt;
        }

        buffer[--j] = (uint8_t)BASE58ALPHABET[remainder];
    }

    while ((j < (2U * length)) && (buffer[j] == BASE58ALPHABET[0])) {
        ++j;
    }

    while (zeroCount-- > 0U) {
        buffer[--j] = BASE58ALPHABET[0];
    }

    length = 2U * length - j;
    if (maxoutlen < length) {
        THROW(EXCEPTION_OVERFLOW);
    }

    os_memmove(out, (buffer + j), length);
    return length;
}

//////////////////////////////////////////////////////////////////////

uint16_t decodeBase58Address(uint8_t WIDE *in,
                             uint16_t inLength,
                             uint8_t *out,
                             uint16_t outLength) {
    uint8_t buffer[CX_SHA256_SIZE];
    outLength = decodeBase58(in, inLength, out, outLength);
    uint8_t hashLength = outLength - RIPEMD160_TMP_LENGTH;

    Sha256 hash;
    hash256(&hash, out, hashLength, buffer);
    hash256(&hash, buffer, CX_SHA256_SIZE, buffer);

    if (os_memcmp(&out[hashLength], buffer, RIPEMD160_TMP_LENGTH)) {
        THROW(INVALID_CHECKSUM);
    }

    return outLength;
}

//////////////////////////////////////////////////////////////////////

uint16_t encodeBase58Address(uint8_t WIDE *in,
                             uint16_t inLength,
                             uint8_t *out,
                             uint16_t outLength) {
    uint8_t temp[inLength + RIPEMD160_TMP_LENGTH];
    uint8_t checksum[CX_SHA256_SIZE];

    Sha256 hash;
    hash256(&hash, in, inLength, checksum);
    hash256(&hash, checksum, CX_SHA256_SIZE, checksum);

    os_memmove(&temp[inLength], checksum, RIPEMD160_TMP_LENGTH);

    return encodeBase58(temp, inLength + RIPEMD160_TMP_LENGTH, out, outLength);
}

//////////////////////////////////////////////////////////////////////

uint16_t encodeBase58PublicKey(uint8_t WIDE *in,
                               uint16_t inLength,
                               uint8_t *out,
                               uint16_t outLength,
                               uint16_t version,
                               uint8_t alreadyHashed) {
    uint8_t temp[inLength + RIPEMD160_TMP_LENGTH];
    uint8_t checksum[CX_SHA256_SIZE];
    uint8_t versionSize = (version > 255U ? 2U : 1U);
    uint8_t ripeLength = versionSize + CX_RIPEMD160_SIZE;

    if (version > 255U) {
        temp[0] = (version >> 8U);
        temp[1] = version;
    }
    else {
        temp[0] = version;
    }

    if (!alreadyHashed) {
        hash160(in, inLength, &temp[versionSize]);
    }
    else {
        os_memmove(&temp[versionSize], &in[versionSize], CX_RIPEMD160_SIZE);
    }

    Sha256 hash;
    hash256(&hash, temp, ripeLength, checksum);
    hash256(&hash, checksum, CX_SHA256_SIZE, checksum);

    os_memmove(&temp[ripeLength], checksum, RIPEMD160_TMP_LENGTH);

    return encodeBase58(temp, ripeLength + RIPEMD160_TMP_LENGTH, out, outLength);
}

//////////////////////////////////////////////////////////////////////

c_cpp Ledger SDK解包Big / Little-Endian 8字节Uint64

Ledger SDK解包Big / Little-Endian 8字节Uint64

ledger_unpack.h
/*******************************************************************************
*   Ark Wallet
*   (c) 2017 Ledger
*   (c) ARK Ecosystem
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
********************************************************************************/

#ifndef ARK_LEDGER_UNPACK_H
#define ARK_LEDGER_UNPACK_H

#include <os.h>

//////////////////////////////////////////////////////////////////////

#define U8BE(buf, off) (((uint64_t)(U4BE(buf, off)                      & 0xFFFFFFFF) << 32U)   |   \
                        ((uint64_t)(U4BE(buf, off + sizeof(uint32_t)))  & 0xFFFFFFFF))

#define U8LE(buf, off) (((uint64_t)(U4LE(buf, off))                     & 0xFFFFFFFF)           |   \
                        ((uint64_t)(U4LE(buf, off + sizeof(uint32_t))   & 0xFFFFFFFF) << 32U))

//////////////////////////////////////////////////////////////////////

#endif

c_cpp 130.周围地区

0130.cpp
struct Coord {
    int x;
    int y;
    Coord(int tmp_x, int tmp_y): x(tmp_x), y(tmp_y) {}
};

class Solution {
public:
    void solve(vector<vector<char>>& board) {
        if (board.empty() || board.size() == 0 || board[0].empty() || board[0].size() == 0) return;
        four_ways = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
        for (int i = 0; i < board.size(); i++) {
            for (int j = 0; j < board[0].size(); j++) {
                if (i == 0 || i == board.size() - 1 || j == 0 || j == board[0].size() - 1) {
                    if (board[i][j] == 'O') {
                        Coord c(i, j);
                        bfs(board, c);
                    }
                }
            }
        }
        for (int i = 0; i < board.size(); i++) {
            for (int j = 0; j < board[0].size(); j++) {
                if (board[i][j] == 'O') board[i][j] = 'X';
                else if (board[i][j] == 'Y') board[i][j] = 'O';
            }
        }
    }
private:
    vector<vector<int>> four_ways;
    void bfs(vector<vector<char>>& board, Coord& c) {
        queue<Coord> Q;
        Q.push(c);
        board[c.x][c.y] = 'Y';
        while(!Q.empty()) {
            Coord tmp_c = Q.front(); Q.pop();
            for (int i = 0; i < 4; i++) {
                Coord new_c(tmp_c.x + four_ways[i][0], tmp_c.y + four_ways[i][1]);
                if (is_valid(board, new_c)) {
                    Q.push(new_c);
                    board[new_c.x][new_c.y] = 'Y';
                }
            }
        }
    }
    bool is_valid(vector<vector<char>>& board, Coord& c) {
        if (c.x < 0 || c.x >= board.size() || c.y < 0 || c.y >= board[0].size() || board[c.x][c.y] == 'X' || board[c.x][c.y] == 'Y') return false;
        return true;
    }
};

c_cpp 200.岛屿数量

0200.cpp
struct Coord {
    int x;
    int y;
    Coord(int tmp_x, int tmp_y): x(tmp_x), y(tmp_y) {}
};

class Solution {
public:
    int numIslands(vector<vector<char>>& grid) {
        if (grid.empty() || grid.size() == 0 || grid[0].empty() || grid[0].size() == 0) return 0;
        res = 0;
        four_ways = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
        
        for (int i = 0; i < grid.size(); i++) {
            for (int j = 0; j < grid[0].size(); j++) {
                if (grid[i][j] == '1') {
                    Coord c(i, j);
                    bfs(grid, c);
                    res++;  
                }
            }
        }
        return res;
    }
    
private:
    int res;
    vector<vector<int>> four_ways;
    void bfs(vector<vector<char>>& grid, Coord& c) {
        queue<Coord> Q;
        Q.push(c);
        grid[c.x][c.y] = '0';
        while(!Q.empty()) {
            Coord cur_c = Q.front(); Q.pop();
            for (int i = 0; i < 4; i++) {
                Coord new_c(cur_c.x + four_ways[i][0], cur_c.y + four_ways[i][1]);
                if (is_valid(grid, new_c)) {
                    Q.push(new_c);
                    grid[new_c.x][new_c.y] = '0';
                }
            }
        }
    }
    bool is_valid(vector<vector<char>>& grid, Coord& c) {
        if (c.x < 0 || c.x >= grid.size() || c.y < 0 || c.y >= grid[0].size() || grid[c.x][c.y] == '0') {
            return false;
        }
        return true;
    }
};

c_cpp 127.单词阶梯

0127.cpp
class Solution {
public:
    int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
        unordered_set<string> dict(wordList.begin(), wordList.end());
        queue<string> Q;
        int res = 1;
        Q.push(beginWord);
        
        while (!Q.empty()) {
            int n = Q.size();
            for (int i = 0; i < n; i++) {
                string curStr = Q.front(); Q.pop();
                if (curStr == endWord) return res;
                for (int j = 0; j < curStr.size(); j++) {
                    string curStr_c = curStr;
                    for (int k = 0; k < 26; k++) {
                        curStr_c[j] = 'a' + k;
                        if (dict.count(curStr_c)) {
                            Q.push(curStr_c);
                            dict.erase(curStr_c);
                        }
                    }
                }
            }
            res++;
        }
        
        return 0;
    }
};

c_cpp MOFSET

https://bildr.org/2012/03/rfp30n06le-arduino/ <br/> <br/>来源 - 电源接地和arduino接地<br/>门 - 转arduino PWM <br/>排水 - 到设备地面<br/>设备 - 地线排水,+电源+

MOFSET.ino
/*

Speed up the motor

This example shows how to control the speed of a DC motor an LED on pin 9 using the analogWrite() function. This example based on the Arduino Example Fade sketch but modified to use timing instead of the delay() function

*/

int turns = 0; // how fast the motor runs

int turnAmount = 1; // how many turns the motor makes

unsigned long currentTime;

unsigned long loopTime;

 

void setup() {

// declare pin 9 to be an output:

   pinMode(9, OUTPUT);

   currentTime = millis();

   loopTime = currentTime;

}

 

void loop() {

   currentTime = millis();

   if(currentTime >= (loopTime + 20)){

       // set the speed of pin 9:

       analogWrite(9, turns);

 

       // change the turnings for next time through the loop:

       turns = turns + turnAmount;

 

       // speed up or slow down the motor

       if (turns == 0 || turns == 255) {

           turnAmount = -turnAmount ;

       }

       if (turns == 0) {

           delay(5000);

       }

       loopTime = currentTime; // Updates loopTime

   }

   // Other processing can be done here

}

c_cpp MOFSET

https://bildr.org/2012/03/rfp30n06le-arduino/ <br/> <br/>来源 - 电源接地和arduino接地<br/>门 - 转arduino PWM <br/>排水 - 到设备地面<br/>设备 - 地线排水,+电源+

MOFSET.ino
/*

Speed up the motor

This example shows how to control the speed of a DC motor an LED on pin 9 using the analogWrite() function. This example based on the Arduino Example Fade sketch but modified to use timing instead of the delay() function

*/

int turns = 0; // how fast the motor runs

int turnAmount = 1; // how many turns the motor makes

unsigned long currentTime;

unsigned long loopTime;

 

void setup() {

// declare pin 9 to be an output:

   pinMode(9, OUTPUT);

   currentTime = millis();

   loopTime = currentTime;

}

 

void loop() {

   currentTime = millis();

   if(currentTime >= (loopTime + 20)){

       // set the speed of pin 9:

       analogWrite(9, turns);

 

       // change the turnings for next time through the loop:

       turns = turns + turnAmount;

 

       // speed up or slow down the motor

       if (turns == 0 || turns == 255) {

           turnAmount = -turnAmount ;

       }

       if (turns == 0) {

           delay(5000);

       }

       loopTime = currentTime; // Updates loopTime

   }

   // Other processing can be done here

}

c_cpp 31.下一个排列

0031.cpp
class Solution {
public:
    void nextPermutation(vector<int>& nums) {
        if (nums.empty() || nums.size() == 0) return;
        
        int firstSmall = -1;
        for (int i = nums.size() - 2; i >= 0; i--) {
            if (nums[i] < nums[i + 1]) {
                firstSmall = i;
                break;
            }
        }
        
        if (firstSmall == -1) {
            reverse(nums.begin(), nums.end());
            return;
        }
        
        int firstLarge = -1;
        for (int i = nums.size() - 1; i > firstSmall; i--) {
            if (nums[i] > nums[firstSmall]) {
                firstLarge = i;
                break;
            }
        }
        
        swap(nums[firstSmall], nums[firstLarge]);
        reverse(nums.begin() + firstSmall + 1, nums.end());   // need "+1"
        return;
    }
};

c_cpp string→char *

c_str.cpp
#include <cstdio>
#include <string>
using namespace std;

int main() {
	string s = "hello";
	printf("%s\n", s.c_str());	// hello
	return 0;
}