dongxiaoke2018 2010-09-23 14:00
浏览 23

c相当于php apns代码

I am writing a .c apns (apple push notification server) application,
I have the rest of the program working ie connecting to the servers establishing ssl, json encoding the message etc.
However I am stuck on the part that converts the token to a hex string part.
Example values
$deviceToken is "4DBCD414F624842E581972E65D2DAA4B96279B209BD0CE10AB12E52AEA48A670"
$apnsMessage is "{"aps":{"alert":"testing","badge":1,"sound":"elephant.aiff"}}"

Here is a php snippet which does the job at the moment.

$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($message)) . $message;
fwrite($this->apnsConnection, $apnsMessage);

Any help would be appreciated. Thanks Peter

  • 写回答

1条回答 默认 最新

  • dongtaogou6226 2010-09-23 14:15
    关注

    chr function in PHP takes an integer and returns a string with one char which is the ASCII char equivalent of the input. Example chr(48) would return "0". In C you can do it using snprintf as:

    int val = 30;
    char str[2];
    snprintf(str,2,"%c",val);
    

    . in PHP is the string concatenation, in C you can do it using strcat and strcpy function. Example:

    $msg = "a" . "b";
    

    in C would be:

    char msg[MAX];
    strcpy(msg,"a");
    strcat(msg,"b");
    

    strlen works the same :) in both C and PHP. It takes a string and returns its length.

    There is no direct function to do the job of str_replace. You'll have to run though the string testing each char.

    Example: To simulate str_replace('a','b',$str):

    for(i=0;str[i];i++) {
     if(str[i] == 'a') { 
      str[i] = b;
     }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错