C ++ converts byte to a hexadecimal string

Source: Internet
Author: User

In Program During the development process, you sometimes need to convert byte to string. I believe there are a lot of instructions on the Internet. At the beginning, I also want to directly find a usable function on the Internet. I did not expect to find it, so I wrote one myself during debugging.

In C ++, there is no basic data type such as byte. It is actually an unsigned char, that is, an unsigned character type. In general, the range of the char type is-128 to 127, while the range of the unsigned char is The value range is 0 to 255, while the hexadecimal format is usually used. The unsigned char is used to represent the unsigned char hexadecimal value.

Obviously, the range of unsigned char and char values is different. It cannot be expressed in only one digit. Two char values must be used to represent the hexadecimal values of an unsigned char.

In the computer, the hexadecimal representation must be very clear, 0---9, A----F is a hexadecimal letter to indicate the range, in this way, we only need to convert one unsigned char to two char each time. The key point is to use the mandatory conversions of char and INT supported by C/C ++. First, convert unsigned char to int type, which can be directly converted, then, divide the obtained int type value by 16 and calculate the quotient and remainder. The quotient and remainder must be between 0---9 and a ---- F and will never exceed this value, in this case, we only need to convert the quotient and remainder into letters. It is also mandatory type conversion of int and char supported by C/C ++, in this way, two letters are displayed in hexadecimal notation.

The basic process is as follows:
1: Convert unsigned char to int Value
2: divide the value by 16, take the commodity V1 and the remainder V2, the ASCII code using the letter 0-9 is 48-57, the ASCII code of the A--F is 65-60, convert integers V1 and V2 into letters
3: connect two letters and return a hexadecimal string.

The corresponding C ++ functions are as follows:
// Two parameters are required. One is the byte array and the other is the length of the array.
// After the length of an array is passed in, the for loop is directly used for processing and no judgment is required,
// If it is a byte pointer, you can modify it.
String * bytetohexstr (unsigned char byte_arr [], int arr_len)
{
String * hexstr = new string ();
For (INT I = 0; I <arr_len; I ++)
{
Char hex1;
Char hex2;
Int value = byte_arr [I]; // directly assign the unsigned char value to the integer value, and the system will force the conversion.
Int V1 = value/16;
Int v2 = Value % 16;

// Convert the operator into a letter
If (V1> = 0 & V1 <= 9)
Hex1 = (char) (48 + V1 );
Else
Hex1 = (char) (55 + V1 );

// Convert the remainder into letters
If (V2> = 0 & V2 <= 9)
Hex2 = (char) (48 + V2 );
Else
Hex2 = (char) (55 + V2 );

// Connect letters into strings
* Hexstr = * hexstr + hex1 + hex2;
}
Return hexstr;
}

In this way, you can use a function to convert a byte array into a hexadecimal String object.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.