C ++ HEX to ASCII (hexadecimal string)

tags: C/C++

#include <iostream>

using namespace std;
// Get the length of the string
unsigned int strlen(const char *str)
{
    const char *cp = str;
    while(*cp++);
    return (cp - str -1);
}
 // Hexadecimal turf
int hex2char(uint8_t c)
{
    return ((c >= '0') && (c <= '9')) ? int(c - '0') :
           ((c >= 'A') && (c <= 'F')) ? int(c - 'A' + 10) :
           ((c >= 'a') && (c <= 'f')) ? int(c - 'a' + 10) :
           -1;
}
 // heel to turn string
int Hex2Ascii(char* hex, char* ascii)
{
    int hexLen = strlen(hex);
    int asciiLen = 0;

    for (int i = 0, cnt = 0; i<hexLen; i++)
    {
        char c = hex2char(hex[i]);

        if (-1 == c)
            continue;
        if(cnt) {
            cnt = 0;
            ascii[asciiLen++] += c;
        } else {
            cnt = 1;
            ascii[asciiLen] = c << 4;
        }
    }
    ascii[asciiLen++] = 0;
    return asciiLen;
}

int main()
{
    char in[] = "31 32 33 34 35 36 37 38 39 41 42 43 44 45 46 61 62 63 64 65 66 ";
    char out[100];
    Hex2Ascii(in, out);

    int len = strlen(out);
    for(int i=0; i<len; i++)
        cout << out[i];

    return 0;
}

 

Intelligent Recommendation

Converting the string to hex and hexadecimal conversion to the interactive instance code of the string in the C #

Being a small tool in these two days, this start starts with byte modification, and finally feel more convenient to use 16 credits, so give these two mutual transcodes.  ...

Review C Language 005 - Convert an ASCII Digital String to HEX Number

In the STM32 microcontroller, an unsigned (long) integer data (U32 / uint32_t / unsigned int) has 32-bit binary data, the maximum value is 0xfffffffff = 0d4294967295. Successful screenshot of VS2012...

C # 10 in HEX and ASCII - Byte [] Type String Type

1 Decoding the array of the entire ASCII type (Byte []) as (String) byte[0]=65 byte[1]=66 string=AB 2. Decoding the entire ASCII type (byte []) as a scientific count method (Decimal) s[0] =57;s[1] = 4...

More Recommendation

c# Hexadecimal to ASCII

Convenient for your own use, record it...

C # 16 hexadecimal (hex) String to convert exemplary byte stream

In engineering practice, the number of bytes of information will often be converted to a hexadecimal character string, so as to transmit and share information such as the encryption key. Here are some...

Python hexadecimal string turn hex

When you write the code, you want to turn the hexadecimal string to a hexadecimal.hex() There will be an error.TypeError: 'str' object cannot be interpreted as an integer After the query, it is found ...

C language -ASCII and hex transformation

0. Introduction Write a function of ASCII and hexadecimal conversion, older, not good memory, sometimes you always have to turn over and see how to achieve it, let's remember it, you will find it late...

Bytes byte string and hex string (hexadecimal) - python

Transfer from: Repeated assembly and readable printing of the stream in several environments, always encounter conversion between hex string and bytes, recorded here. 1. On Python 2.7.x (the older env...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top