Home » C programming language

Convert ASCII string (char[]) to BYTE array in C

C | Convert ASCII string to BYTE array: Here, we are going to learn how to convert a given string (that contains ascii characters) to its equivalent BYTE array in C?
Submitted by IncludeHelp, on February 13, 2019

Given an ASCII string (char[]) and we have to convert it into BYTE array (BYTE[]) in C.

Logic:

To convert an ASCII string to BYTE array, follow below-mentioned steps:

  • Extract characters from the input string and get the character's value in integer/number format using %d format specifier, %d gives integer (number) i.e. BYTE value of any character.
  • Add these bytes (number) which is an integer value of an ASCII character to the output array.
  • After each iteration increase the input string's loop counter (loop) by 1 and output array's loop counter (i) by 1.

Example:

    Input: "Hello world!"
    Output:
    72
    101
    108
    108
    111
    32 
    119
    111
    114
    108
    100
    33

C program to convert ASCII char[] to BYTE array

In this example, ascii_str is an input string that contains "Hello world!", we are converting it to a BYTE array. Here, we created a function void string2ByteArray(char* input, BYTE* output), to convert ASCII string to BYTE array, the final output (array of integers) is storing in arr variable, which is passed as a reference in the function.

Note: Here, we created a typedef BYTE for unsigned char data type and as we know an unsigned char can store value from 0 to 255.

Read more: typedef in C, unsigned char in C

#include <stdio.h>
#include <string.h>

typedef unsigned char BYTE;

//function to convert string to byte array
void string2ByteArray(char* input, BYTE* output)
{
    int loop;
    int i;
    
    loop = 0;
    i = 0;
    
    while(input[loop] != '\0')
    {
        output[i++] = input[loop++];
    }
}

int main(){
    char ascii_str[] = "Hello world!";
    int len = strlen(ascii_str);
    BYTE arr[len];
    int i;
    
    //converting string to BYTE[]
    string2ByteArray(ascii_str, arr);
    
    //printing
    printf("ascii_str: %s\n", ascii_str);
    printf("byte array is...\n");
    for(i=0; i<len; i++)
    {
        printf("%c - %d\n", ascii_str[i], arr[i]);
    }
    printf("\n");
    
    return 0;
}

Output

ascii_str: Hello world!
byte array is...
H - 72
e - 101
l - 108
l - 108
o - 111
  - 32 
w - 119
o - 111
r - 114
l - 108
d - 100
! - 33

Read more...






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.