目标c-如何将nsdata转换为字节数组 [英] objective c - How to convert nsdata to byte array

查看:350
本文介绍了目标c-如何将nsdata转换为字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将UIImage转换为NSData.而且我想将NsData转换为字节数组,并在json解析器的帮助下将该字节数组发布到服务器.

I am working with UIImage conversion to NSData. And I want to convert NsData to Byte Array and post that Byte array to server with the help of json Parser.

如果我将以下类型的静态字符串传递给服务器,它将接受并存储.以下只是示例字符串语法.我希望以下类型的字节数组将数据发布到服务器.

If i am passing following type of static string to server it accept and store. Following is just example string syntax. i want follwoing type of byte array to post data to server.

[255,216,255,224,0,16,74,70,73,70,0,1,1,1,0,96,0,96,0,0,255,219,0,67,0,8,6,6, 7,6,5,8,7,7,7,9,9,8,10,12,...]

[255,216,255,224,0,16,74,70,73,70,0,1,1,1,0,96,0,96,0,0,255,219,0,67,0,8,6,6,7,6,5,8,7,7,7,9,9,8,10,12,...]

对于以上结果,我将图像转换为数据,如下所示:

For above result i am converting image to data as following:

UIImage *image = [UIImage imageNamed:@"image.png"];
NSData *data = UIImagePNGRepresentation(image);

现在我要使用以下代码将NSdata转换为字节数组:

Now i want to convert NSdata to byte array for that i am using following code:

NSUInteger len = [data length];
Byte *byteData = (Byte*)malloc(len);
memcpy(byteData, [data bytes], len);
free(byteData)

,我也将其存储到NSdata.但无法获得结果. 我还输入了有关循环的代码,但是每次它给出 aPNG 作为结果.

and i also store this to NSdata. but cant get about result. I also typed about code with looping but every time it gives aPNG as result.

我在上述代码中做错了什么吗?

Am i doing any thing wrong in the above code?

请为我提供一些帮助,并要求为此提供任何帮助.

Please provide me some help and also request to provide any help for this.

谢谢.

推荐答案

我不确定100%是否是您要的内容.试试:

I'm not 100% sure this is what you are looking for or not. Try:

UIImage *image = [UIImage imageNamed:@"image.png"];
NSData *data = UIImagePNGRepresentation(image);
NSUInteger len = data.length;
uint8_t *bytes = (uint8_t *)[data bytes];
NSMutableString *result = [NSMutableString stringWithCapacity:len * 3];
[result appendString:@"["];
for (NSUInteger i = 0; i < len; i++) {
    if (i) {
        [result appendString:@","];
    }
    [result appendFormat:@"%d", bytes[i]];
}
[result appendString:@"]"];

这是您需要的吗?

这篇关于目标c-如何将nsdata转换为字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆