C C++ Java 打印byte char 为16进制数调试

C C++ Java 打印byte char 为16进制数调试,第1张

C C++ Java 打印byte char 为16进制数调试 C C++ Java 打印byte char 为16进制数调试 C & C++
void byteToHex(int num,char* buf)
{
    const char array[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    int d1 = num / 16;
    int d2 = num % 16;
    sprintf(buf," 0x%c%c",array[d1],array[d2]);
}

void printByte(void* addr,int size){
    char buf[size * 5];
    for (int i = 0; i < size; i++)
    {
        byteToHex(((char*)addr)[i],buf+(i*5));
    }
    ALOGE("%s",buf);
}

Java
    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            stringBuilder.append("0x");

            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
            if (i != src.length-1) {
                stringBuilder.append(",");
            }
        }
        return stringBuilder.toString();
    }

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5651182.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存