Jagruti Kapgate
Jagruti Kapgate

Reputation: 35

SMS PDU encoding in c

I am working on SMS PDU encoding in C. I am having problem while encoding the phone number. If phone number is "12345678912", it is encoded as "2143658719F2" which is correct. But when phone number is "33689004000", it is encoded as "33869400F0". Zeros at odd index are missing after encoding.

Code is

static int
EncodePhoneNumber(const char* phone_number, unsigned char* output_buffer, int buffer_size)
{
    int output_buffer_length = 0;  
    const int phone_number_length = strlen(phone_number);
    if ((phone_number_length + 1) / 2 > buffer_size)
        return -1;

    int i = 0;
    for (; i < phone_number_length; ++i) {

       if (phone_number[i] < '0' && phone_number[i] > '9')
           return -1;

        if (i % 2 == 0) {
            output_buffer[output_buffer_length++] = BITMASK_HIGH_4BITS | (phone_number[i] - '0');
        } else {
            output_buffer[output_buffer_length - 1] =
                (output_buffer[output_buffer_length - 1] & BITMASK_LOW_4BITS) |
                 ((phone_number[i] - '0') << 4); 
        }
    }

   return output_buffer_length;
}

What is the solution to this problem?

Upvotes: 1

Views: 926

Answers (1)

Armali
Armali

Reputation: 19375

The error is presumably not in the shown code, but rather where you view the result. I suspect that you have a printf("%x", …) instead of printf("%02x", …).

Upvotes: 1

Related Questions