Character Array To Byte Array & Char to Hex Conversion

Posted on 2004-11-05
Medium Priority
Last Modified: 2012-06-27
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.
Question by:leon_winstonr
  • 2
  • 2
  • 2

Expert Comment

ID: 12502765
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.


to diplay hexadecimal number,


to display decadic number, or


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


LVL 60

Expert Comment

by:Julian Hansen
ID: 12502827
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.


Author Comment

ID: 12504902
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,

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


Accepted Solution

PodExpert earned 2000 total points
ID: 12505104
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);

LVL 60

Expert Comment

by:Julian Hansen
ID: 12505283
Just to expand on what PodExpert said

a BYTE type is just a redefinition of an unsigned char

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]
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

Author Comment

ID: 12505435
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.


Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Loops Section Overview

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question