0

I'm trying to create a PDU encoder which requires me to:

  1. Convert each char in a string into an ASCII value
  2. Convert the ASCII value into 7-bit binary
  3. Using this method - Converting 7-bit into 8 bit. I'm doing this in a function by taking the first septet, and adding from the end of the next septet until the first has 8 bits (like shown in the url). This is continued throughout basically.
  4. Using the 8 bit binary I can convert to Hex for my PDU string.

All this works fine when using:

  1. ASCII = Convert.ToInt32(char)
  2. Convert.ToString(ASCII, 2) = 7bit
  3. SeptetToOctet(7bit) = 8bit (My function)
  4. Convert.ToString(Convert.ToInt32(8bit, 2), 16).ToUpper() (I'm adding a 0 if it converts to only 1 char)

Now comes my problem, when trying to convert special characters, like the danish 'ø', you get the ASCII value of 248, which with Convert.ToString(248, 2) gives me 11111000 (8 bits). So either I need to know how to force the conversion of 'ø' to 7 bit binary, or the SeptetToOctet conversion (this the conversion on the URL provided) needs revision, but I do now have enought knowledge about binary and binary conversion to know how to get it right.

VMAtm
  • 27,943
  • 17
  • 79
  • 125
Daniel Olsen
  • 1,020
  • 2
  • 15
  • 27
  • ASCII doesn't *have* a value of 248. ASCII is a 7-bit encoding. You need to work out which encoding you're *really* talking about. – Jon Skeet Sep 12 '11 at 08:09
  • Well - I looked at MSDN and it says that ToInt32 provides UTF-16 encoding. So not ASCII, but UTF-16 – Daniel Olsen Sep 12 '11 at 08:21
  • Well `char` is inherently a UTF-16 code unit. You don't need to call `Convert.ToInt32` at all. But basically if you've got non-ASCII characters, you need to know how they're meant to be represented... – Jon Skeet Sep 12 '11 at 08:32
  • @Jon Skeet: His character is in the 7 bit GSM alphabet, just need another ascii code. Daniel see my answer. – whosrdaddy Sep 12 '11 at 08:40

1 Answers1

1

Daniel,

look here:

http://www.dreamfabric.com/sms/default_alphabet.html

You need to convert to 0x0B or 0x0C (depending on capitals)

Make a translationtable for 7bit GSM to ASCII (your encoding) and vice versa. This will make your life easier.

Here's a small sample in delphi (I know OOP asks for C# but the principle stays the same)

function CharsetLatin1ToGsm(v : string) : string;

var Ps,I : Integer;
    Len  : Integer;
    c    : Char;

begin
 Result := '';
 if v = '' then Exit;
 Ps := 1;
 Len := Length(v);
 while Ps <= Len do
  begin
   c := v[Ps];
   I := Latin1ToGsm[Byte(c)];
   if I < 0 then
    begin
     Result := Result+#27+Chr(-I);
    end
   else
    Result := Result+Chr(I);
   Inc(Ps);
  end;
end;

Small explanation:

v is the original input string. each character is converted to a byte which serves as an index for the Latin1ToGsm array which contains the GSM alphabet counterparts. if the value returned is below 0, it means we have an extended character (like the € sign) and this means you need to send an escape character (0x027) in front.

EDIT

here's a link that explains how to do it in C#: http://codeglobe.blogspot.com/2009/02/sending-sms-in-cnet-using-gsm-modem-and.html

Cheers!

whosrdaddy
  • 11,720
  • 4
  • 50
  • 99
  • 1
    Additionally, you can use the UCS-2 character set (with a reduced capacity) – Rowland Shaw Sep 12 '11 at 08:46
  • whosrdaddy: I see, would i be possible for you to give me a hint on how I can proceed? Cause I need to convert this 7bit GSM hex to the 8bit hex? I just tried searching google which had lots of ways to do the opposite of what I want, but no usable way of reversing the code. – Daniel Olsen Sep 12 '11 at 08:57
  • Shaw: Your solution is elegant, but I shouldn't have to use the UCS-2 character set. – Daniel Olsen Sep 12 '11 at 08:58
  • @Daniel: first convert the ASCII input characters to their counterparts in the 7bit GSM alphabet (aka Translationtable). Then encode that string into a 7bit PDU sequence. – whosrdaddy Sep 12 '11 at 09:02
  • Yeah but its the 7bit PDU encoding thats a bit hazy to me, cause according to this example: http://www.dreamfabric.com/sms/hello.html - I need it in 7 bit binary to be able to convert it to 8 bit binary and then to an 8 bit representation of the 7 bit data. So yeah, I can easily create an array which provides the 7bit GSM hex for any given character. But how to get that to the 8 bit representation is the hard part. – Daniel Olsen Sep 12 '11 at 09:06
  • Daniels, all ASCII characters are represented by a byte. so is the 7bit GSM counterpart (the value will never be bigger than 0x7F, ie the highest bit will never be set). You need to shuffle bits from the other characters into a 7bit stream. Start with the first char, shift left so the lowest bit will be the highest bit (from bit 7) of the next character. – whosrdaddy Sep 12 '11 at 09:11