Character Array To Byte Array & Char to Hex Conversion

Hi All,

I need help developing a function. Im developing this code using VC Editor. I need a function to convert data from a CharacterArray to a Byte Array at the same time converting from Ascii to Hex value. The Declarations are as below

char strSourceData[SIZE]
BYTE bytDestData[SIZE]

For e.g. The source array has {'1','2','3',...'a'}
Then the destination shud be populated with {0x31,0x32,0x33,....}
'1' Ascii = 49
49 Hex = 31.
Similarly for any other characters.
I require a function that will take inputs as the source array, destination array and the number of elements to convert.

Thanks & Kind Regards.
Leon.
leon_winstonrAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PodExpertCommented:
Dear Leon,

I don't know what does VC mean, but in C/C++, there is nothing to convert. Characters are stored as bytes in C/C++, and it depends only on the way you display them, if the result is a hexadecimal number, a decadic number or a character. You can use e.g.

printf("%x",strSourceData[i])

to diplay hexadecimal number,

printf("%d",strSourceData[i])

to display decadic number, or

printf("%c",strSourceData[i])

to display character. But internally, the data is the same in all cases, no conversion needs to be done.

Regards.

P.
0
Julian HansenCommented:
I am not sure what you are asking

an array of chars {'1','2','3' ... }

is exactly the same as an array of bytes with values {0x31,0x32,0x33 ...}

Remember 00110001 is the same as 0x31 is the same as '1' - it has the same underlying storage representation - it only changes when you decide to display the data at which point you decide the output format.


0
leon_winstonrAuthor Commented:
Hi Guys,

I agree that internally data is same, but here I have once system defined function that will return me values in a char array and another that takes actual byte values in a byte array as input. I have to plug the output of the first to the input of the second. I hope you guys are getting me.

Thanks & Regards,
Leon.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

PodExpertCommented:
Hi again,

because the data is internally the same, you can use typcasting to convert it. In C/C++, you can convert chars to bytes in the following way (assuming that the BYTE type is defined):

char myarray[20];
BYTE bytevalue;

byte_value = (BYTE)my_array[i];

Or, when there is a function requiring a byte array:

result = my_function((BYTE*)my_array);

Regards.
P.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Julian HansenCommented:
Just to expand on what PodExpert said

a BYTE type is just a redefinition of an unsigned char
i.e

typedef unsigned char BYTE

so they are essentially the same thing.

All you need to do is tell the compiler how to deal with the conversion from one to the other. Because a char has values -127 to 127 and an unsigned char can store values from 0 to 255 the compier gets confused when converting one to the other. In your case you are actually interested in unsgined chars because you are not going to be dealing with negative numbers so you could declare your array as either

BYTE myarray[size]
OR
unsigned char myarray[size]

However, you may have a problem with the call to the first function that expects chars so either way you might have to do a cast.

by specifying the (BYTE*) before an array declared as char or (char*) before an array declared as unsigned char or BYTE simply tells the compiler to treat char's as unsigned chars etc - it does not change the data in the array or how the data is passed - it just makes the compiler happy with life.

Hope this helps
 
0
leon_winstonrAuthor Commented:
Hi Pod,

The result = my_function((BYTE*)my_array); did it. I guess it was the most basic thing I didnt use. Thanks both for helping me.

Regards,
Leon.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.