Character Array To Byte Array & Char to Hex Conversion

Posted on 2004-11-05
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
    LVL 3

    Expert Comment

    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 49

    Expert Comment

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

    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,
    LVL 3

    Accepted Solution

    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 49

    Expert Comment

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

    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.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: HTML5 Specialist

    HTML5 development skills are critical to all developers. HTML5 is the foundation to almost any development process. That's why business, design studios, development shops and other organizations need HTML5 developers. Get your foot in the door as a HTML5 specialist.

    This is an explanation of a simple data model to help parse a JSON feed
    Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
    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…
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now