C language code 1 for SMS messages

Source: Internet
Author: User
1. message encoding and decoding
The 7-bit encoding and decoding algorithm using C is as follows:

// 7-bit encoding
// Psrc: Source string pointer
// Pdst: Target encoding string pointer
// Nsclength: Source String Length
// Return: the length of the target encoding string.
Int gsmencode7bit (const char * psrc, unsigned char * pdst, int nsclength)
{
Int NSRC; // The Count value of the source string
Int ndst; // The Count value of the target encoding string
Int nchar; // the serial number of the character bytes in the processing group. The value range is 0-7.
Unsigned char nleft; // residual data of the last byte

// Count value Initialization
NSRC = 0;
Ndst = 0;

// Divide the source string into a group of 8 bytes and compress it into 7 bytes.
// Loop the processing process until the source string is processed
// If the group contains less than 8 bytes, it can be processed correctly.
While (NSRC <nsclength)
{
// Obtain the minimum three bits of the Count value of the source string
Nchar = NSRC & 7;

// Process each byte of the source string
If (nchar = 0)
{
// The first byte in the group, which is only saved and used for processing the next byte
Nleft = * psrc;
}
Else
{
// Add the right part of the other bytes in the group to the residual data to obtain a target encoded byte.
* Pdst = (* psrc <(8-nchar) | nleft;

// Save the remaining left part of the byte as residual data
Nleft = * psrc> nchar;
// Modify the pointer and count pdst ++ of the target string;
Ndst ++;
}

// Modify the pointer and counter value of the source string
Psrc ++; NSRC ++;
}

// Return the length of the target string
Return ndst;
}

// 7-bit Decoding
// Psrc: source encoding string pointer
// Pdst: Target string pointer
// Nsclength: source encoding String Length
// Return: the length of the target string.
Int gsmdecode7bit (const unsigned char * psrc, char * pdst, int nsclength)
{
Int NSRC; // The Count value of the source string
Int ndst; // The Count value of the target decoder string
Int nbyte; // The number of bytes in the group being processed. The value range is 0-6.
Unsigned char nleft; // residual data of the last byte

// Count value Initialization
NSRC = 0;
Ndst = 0;

// Initialize the byte numbers and residual data in the group
Nbyte = 0;
Nleft = 0;

// Divides the source data into a group of seven bytes and decompress the data into eight bytes.
// Loop the processing process until the source data is processed
// If the group contains less than 7 bytes, it can be processed correctly.
While (NSRC <nsclength)
{
// Add the right part of the Source byte to the residual data, remove the highest bit, and obtain a target decoded byte.
* Pdst = (* psrc <nbyte) | nleft) & 0x7f;
// Save the remaining left part of the byte as residual data
Nleft = * psrc> (7-nbyte );

// Modify the pointer and counter value of the target string
Pdst ++;
Ndst ++;

// Modify the Byte Count value
Nbyte ++;

// To the last byte of a group
If (nbyte = 7)
{
// Obtain an extra target decoded byte
* Pdst = nleft;

// Modify the pointer and counter value of the target string
Pdst ++;
Ndst ++;

// Initialize the byte numbers and residual data in the group
Nbyte = 0;
Nleft = 0;
}

// Modify the pointer and counter value of the source string
Psrc ++;
NSRC ++;
}

* Pdst = 0;

// Return the length of the target string
Return ndst;
}

It should be noted that the 7-bit character set is inconsistent with the ANSI standard character set, and some printable characters are also arranged below 0x20, however, the English letters, Arabic numbers, and common symbols have the same positions. Use the algorithm described above to send and receive short messages in English only. Generally, it is enough. If it is French, German, or Spanish, it contains "?" , "É", according to the above encoding output to look up the table, see the provisions of GSM 03.38.

8-bit encoding does not specify specific algorithms.

Ucs2 encoding converts each character (1-2 bytes) to a 16-bit Unicode wide character according to ISO/iec000046. In Windows systems, especially in 2000/XP, you can simply call API functions for encoding and decoding. If there is no system support, for example, using a single-chip microcomputer to control the mobile phone module to send and receive short messages, you have to use the look-up table method to solve the problem.

In Windows, the algorithm for ucs2 encoding and decoding using C is as follows:

// Ucs2 Encoding
// Psrc: Source string pointer
// Pdst: Target encoding string pointer
// Nsclength: Source String Length
// Return: the length of the target encoding string.
Int gsmencodeucs2 (const char * psrc, unsigned char * pdst, int nsclength)
{
Int ndstlength; // The number of Unicode characters.
Wchar [128]; // Unicode String Buffer

// String --> Unicode string
Ndstlength =: multibytetowidechar (cp_acp, 0, psrc, nsclength, wchar, 128 );

// High/low byte reconciliation, output
For (INT I = 0; I <ndstlength; I ++)
{
// Output the high byte first
* Pdst ++ = wchar [I]> 8;
// Post-output low byte
* Pdst ++ = wchar [I] & 0xff;
}

// Return the length of the target encoding string
Return ndstlength * 2;
}

// Ucs2 Decoding
// Psrc: source encoding string pointer
// Pdst: Target string pointer
// Nsclength: source encoding String Length
// Return: the length of the target string.
Int gsmdecodeucs2 (const unsigned char * psrc, char * pdst, int nsclength)
{
Int ndstlength; // The number of Unicode characters.
Wchar [128]; // Unicode String Buffer

// High/low byte reconciliation, And concatenated to Unicode
For (INT I = 0; I <nsclength/2; I ++)
{
// The first high byte
Wchar [I] = * psrc ++ <8;

// Post-low byte
Wchar [I] | = * psrc ++;
}

// Unicode string --> string
Ndstlength =: widechartomultibyte (cp_acp, 0, wchar, nsclength/2, pdst, 160, null, null );

// Add an ending character to the output string
Pdst [ndstlength] = '\ 0 ';

// Returns the length of the target string.
Return ndstlength;
}

Using the above encoding and decoding modules, you cannot encode short message strings into the formats required by PDU strings, or directly decode user information in the PDU strings into short message strings, this is because there is a difference between the printable string and the byte data. You can call the sscanf and sprintf functions cyclically to implement this transformation. The following provides algorithms that do not use these functions. They are also applicable to single-chip microcomputer and DSP programming environments.

// Printable string converted to byte data
// For example, "c8329bfd0e01" --> {0xc8, 0x32, 0x9b, 0xfd, 0x0e, 0x01}
// Psrc: Source string pointer
// Pdst: Target Data Pointer
// Nsclength: Source String Length
// Return: Target Data Length
Int gsmstring2bytes (const char * psrc, unsigned char * pdst, int nsclength)
{
For (INT I = 0; I <nsclength; I + = 2)
{
// The output height is 4 bits
If (* psrc> = '0' & * psrc <= '9 ')
{
* Pdst = (* psrc-'0') <4;
}
Else
{
* Pdst = (* psrc-'A' + 10) <4;
}

Psrc ++;

// 4 lower output bits
If (* psrc> = '0' & * psrc <= '9 ')
{
* Pdst | = * psrc-'0 ';
}
Else
{
* Pdst | = * psrc-'A' + 10;
}
Psrc ++;
Pdst ++;
}

// Return the length of the target data
Returnnsrclength/2;
}

// Convert byte data into printable strings
// For example, {0xc8, 0x32, 0x9b, 0xfd, 0x0e, 0x01} --> "c8329bfd0e01"
// Psrc: Source Data Pointer
// Pdst: Target string pointer
// Nsclength: Source Data Length
// Return: the length of the target string.
Int gsmbytes2string (const unsigned char * psrc, char * pdst, int nsclength)
{
Const char tab [] = "0123456789 abcdef"; // character query table for 0x0-0xf

For (INT I = 0; I <nsclength; I ++)
{
// 4 lower output bits
* Pdst ++ = tab [* psrc> 4];

// The output height is 4 bits
* Pdst ++ = tab [* psrc & 0x0f];

Psrc ++;
}

// Add an ending character to the output string
* Pdst = '\ 0 ';

// Returns the length of the target string.
Return nsclength * 2;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.