How to convert a hexadecimal string cstring to a byte [] Array

Source: Internet
Author: User

Requirements:
How to convert a hexadecimal string cstring to a byte [] Array
Cstring STR = "56 45 52 30 30 ";
Byte Buf [64];

After assignment
Buf [64] = {0x56,0x45,0x52,0x30,0x30 };

 

Int ccp210xserialtestdlg: str2hex (cstring STR, unsigned char * Data)
{
Unsigned int T;
Int length;
Int Len = Str. getlength ();
Char A [10] = {0 };

Length = 0;

For (INT I = 0; I <Len ;)
{
If (STR [I] = '') // Space
{
I ++;
Continue;
}

A [0] = STR [I]; // high byte

I ++;
If (I> = Len)
{
Break;
}

A [1] = STR [I]; // low byte

A [2] = '/0'; // string end flag

T = strtoul (A, null, 16); // converts a string to an unsigned long integer.
Data [length] = (unsigned char) T;
Length ++;

I ++;
}

Return length; // The number of converted bytes.
}

There are many ways to deal with such problems. My method features the use of the strtoul () function.

Although it is feasible, I still have doubts. Can the cstring class be accessed directly using the array underlying method (STR [I? I feel that I cannot do this, but can solve the problem. I am wondering. I hope to have a finger at it. Thank you very much.

Fu shaobing

2010-11-11

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.