base16编码和解码-支持中文

 

这是一个base16 编码/解码 的方法。而且支持中文的编码和解码。

 

代码由chatgpt-3.5生成:

#include <iostream>
#include <string>

const std::string base16 = "0123456789ABCDEF";

void Base16Encode(const std::string& data, std::string& out) {
    for (unsigned char d : data) {
        char a = base16[d >> 4];
        char b = base16[d & 0x0F];
        out.push_back(a);
        out.push_back(b);
    }
}

void base16Decode(const std::string& data, std::string& out) {
    for (size_t i = 0; i < data.size(); i += 2) {
        int hi = data[i] <= '9' ? data[i] - '0' : (data[i] & 0xDF) - 'A' + 10;
        int lo = data[i + 1] <= '9' ? data[i + 1] - '0' : (data[i + 1] & 0xDF) - 'A' + 10;
        out.push_back((hi << 4) | lo);
    }
}

void testStr(const std::string& original) {
    std::string encoded;
    Base16Encode(original, encoded);
    std::cout << "Encoded string: " << encoded << std::endl;

    std::string decoded;
    base16Decode(encoded, decoded);
    std::cout << "Decoded string: " << decoded << std::endl;
}

int main() {
    const std::string original = "这test这是一个测试字符串。henry20240311串"; // 中文字符串
    testStr(original);

    const std::string original2 = "test这是一个测试字符串。henry20240311串";
    testStr(original2);

    const std::string original3 = "这test这是一个测试字符串。henry20240311";
    testStr(original3);


    return 0;
}

 


 

这是C风格的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

const char base16[] = "0123456789ABCDEF";

void Base16Encode(const unsigned char* data, int size, unsigned char* out) {
    for (int i = 0; i < size; i++) {
        unsigned char d = data[i];
        char a = base16[d >> 4];
        char b = base16[d & 0x0F];
        out[i * 2] = a;
        out[i * 2 + 1] = b;
    }
}

void base16Decode(const unsigned char* data, int size, unsigned char* out) {
    for (int i = 0; i < size; i += 2) {
        int hi = data[i] <= '9' ? data[i] - '0' : (data[i] & 0xDF) - 'A' + 10;
        int lo = data[i + 1] <= '9' ? data[i + 1] - '0' : (data[i + 1] & 0xDF) - 'A' + 10;
        out[i / 2] = (hi << 4) | lo;
    }
}

void testStr(const char* original) {
    int original_size = strlen(original);
    unsigned char encoded[original_size * 2 + 1]; // Allocate space for encoded string
    Base16Encode((const unsigned char*)original, original_size, encoded);
    encoded[original_size * 2] = '\0'; // Null terminate encoded string
    printf("Encoded string: %s\n", encoded);

    unsigned char decoded[original_size + 1]; // Allocate space for decoded string
    base16Decode(encoded, original_size * 2, decoded);
    decoded[original_size] = '\0'; // Null terminate decoded string
    printf("Decoded string: %s\n", decoded);
}

int main() {
    const char* original = "这test这是一个测试字符串。henry20240311串"; // 中文字符串
    testStr(original);

    return 0;
}

 

strlen是C标准库中的一个函数,用于计算字符串的长度。它的原型定义在头文件<string.h>中:

size_t strlen(const char *str);

strlen函数接受一个指向以 null 结尾的字符串的指针作为参数,并返回该字符串中字符的数量,不包括 null 终止符。它会从字符串的开头开始遍历,直到遇到 null 终止符为止,然后返回遍历过程中统计的字符数量。

例如,如果我们有一个字符串"hello",它实际上是一个字符数组,内容是{'h', 'e', 'l', 'l', 'o', '\0'},其中'\0'是 null 终止符。那么调用strlen("hello")会返回 5,因为在遍历到 null 终止符之前,它统计了5个字符。

strlen函数对于以 null 结尾的字符串非常有用,可以帮助我们确定字符串的实际长度,而不需要在代码中显式指定字符串的长度。

posted @ 2024-03-11 22:48  He_LiangLiang  阅读(321)  评论(0编辑  收藏  举报