简体   繁体   English

将NSData转换为base64encoded和C#中的字节数组

[英]Convert NSData to base64encoded AND a byte array in C#

I'm implementing in-app purchasing for our iOS app for various auto-renewable subscriptions. 我正在为我们的iOS应用程序实现各种自动续订订阅的应用程序内购买。 When the payment is complete we need to send the transaction information to our server (cloud) to record the information so that we can verify the receipt on a set interval to make sure the subscription is valid, not cancelled/refunded, or renewed. 付款完成后,我们需要将交易信息发送到我们的服务器(云)以记录信息,以便我们可以在设定的时间间隔内验证收据,以确保订阅有效,不会取消/退款或续订。 We are going to make the JSON calls from the server on a set interval to do this via the in-app purchasing guide and our shared secret, have yet to get to that but before we do we need to have the relevant data from the purchase, ie the TransactionReceipt which is an NSData object. 我们将在设定的时间间隔内从服务器进行JSON调用以通过应用程序内购买指南和我们的共享秘密进行此操作,但尚未达到此目的,但在此之前我们需要从购买中获取相关数据,即TransactionReceipt,它是一个NSData对象。

We want to send two parameters to our web service for the TransactionReceipt (among other items such as the ProductID purchased, etc.). 我们想要为TransactionReceipt向我们的Web服务发送两个参数(以及其他项目,例如购买的ProductID等)。 We want to send this as a base64encoded value which is what we believe needs to be sent in the JSON request for validation, so we'll store that in SQL Server. 我们希望将此作为base64encoded值发送,这是我们认为需要在JSON请求中发送以进行验证的内容,因此我们将其存储在SQL Server中。

HOw, using MonoTouch / C# can we convert the NSData "TransactionReceipt" to base64encoded and also a byte[]? 顺便说一句,使用MonoTouch / C#我们可以将NSData“TransactionReceipt”转换为base64encoded还有一个byte []吗?

Thank you. 谢谢。

There's two easy way to get data out of NSData , using a Stream or the Bytes and Length properties. 使用StreamBytesLength属性有两种简单的方法可以从NSData获取数据。 The stream version would look like: 流版本看起来像:

public byte[] ToByte (NSData data)
{
    MemoryStream ms = new MemoryStream ();
    data.AsStream ().CopyTo (ms);
    return ms.ToArray ();
}

the Bytes and Length version would be: BytesLength版本将是:

public byte[] ToByte (NSData data)
{
    byte[] result = new byte[data.Length];
    Marshal.Copy (data.Bytes, result, 0, (int) data.Length);
    return result;
}

Getting the base64 output string remains identical: 获取base64输出字符串保持相同:

public string ToBase64String (NSData data)
{
    return Convert.ToBase64String (ToByte (data));
}

这也有效:

string yourDataInBase64 = Convert.ToBase64String(yourData.ToArray());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM