如何将 uint8[] 转换为字符串?

发布于 2024-11-02 02:21:24 字数 1115 浏览 0 评论 0原文

你好 我正在开发 iPhone 应用程序 我在缓冲区中收到一些数据类型为 Uint8 的服务器响应 现在我想将我的缓冲区与字符串匹配,任何人都可以告诉我如何检查它吗?给出了一段代码,

void receiveData(CFSocketRef s, CFSocketCallBackType type,  CFDataRef address, const void *data, void *info)
{

    CFDataRef df = (CFDataRef) data;
    int len = CFDataGetLength(df);
    if(len <= 0) return;

    CFRange range = CFRangeMake(0,len);
    UInt8 buffer[len];
    NSLog(@"Received %d bytes from socket %d\n", 
          len, CFSocketGetNative(s));
    CFDataGetBytes(df, range, buffer);
    NSLog(@"Client received: %s\n", buffer); 

    NSLog(@"As UInt8 coding: %@", df);
    [mTextViewAlias setText:[NSString stringWithUTF8String: buffer]];
    ////////////.............////////////
    LoginViewController *lvc = [[LoginViewController alloc] init];

    NSString *search = @"ACPT";

    NSRange match;

    match = [buffer rangeOfString: search];

    if (match.location == NSNotFound)
        NSLog (@"Invalid Username Or Password");
    else
    {
        [lvc goToWatchList];

        NSLog (@"match found at index %i", match.location);
    }
}

任何人都知道吗? 我会非常感激

Hi
I am doing work on an iPhone application
I am getting some response of server in my buffer with data type Uint8
now i want to Match my buffer with a string, can any body tell me how can i check it? a piece of code is given

void receiveData(CFSocketRef s, CFSocketCallBackType type,  CFDataRef address, const void *data, void *info)
{

    CFDataRef df = (CFDataRef) data;
    int len = CFDataGetLength(df);
    if(len <= 0) return;

    CFRange range = CFRangeMake(0,len);
    UInt8 buffer[len];
    NSLog(@"Received %d bytes from socket %d\n", 
          len, CFSocketGetNative(s));
    CFDataGetBytes(df, range, buffer);
    NSLog(@"Client received: %s\n", buffer); 

    NSLog(@"As UInt8 coding: %@", df);
    [mTextViewAlias setText:[NSString stringWithUTF8String: buffer]];
    ////////////.............////////////
    LoginViewController *lvc = [[LoginViewController alloc] init];

    NSString *search = @"ACPT";

    NSRange match;

    match = [buffer rangeOfString: search];

    if (match.location == NSNotFound)
        NSLog (@"Invalid Username Or Password");
    else
    {
        [lvc goToWatchList];

        NSLog (@"match found at index %i", match.location);
    }
}

any body have some idea?
i'll be very thankful

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

想你的星星会说话 2024-11-09 02:21:24

您的响应可能是 UInt8 但您不需要将其复制到 UInt8[] 中,因为您已经在 CFDataRef 中拥有它(假设您传入的 void * 确实是一个 CFDataRef,如果不是,您还有其他问题...) - CFDataRef 是收费的- 免费桥接NSData (即您可以从一个转换为另一个),并且您可以直接从 NSData 中的字节构造一个 NSString。您的代码,删除日志记录并添加直接 NSString 转换:

void receiveData(CFSocketRef s, CFSocketCallBackType type,  CFDataRef address, const void *data, void *info)
{
    LoginViewController *lvc = [[LoginViewController alloc] init];

    NSString *buffer = [[NSString alloc] initWithData:(NSData *)data
                                             encoding:NSUTF8StringEncoding];
    NSRange match = [buffer rangeOfString:@"ACPT"];
    [buffer release];

    if (match.location == NSNotFound)
        NSLog (@"Invalid Username Or Password");
    else
        [lvc goToWatchList];
}

或者,至少在您的示例代码中:不保存转换后的缓冲区,使用文字字符串进行搜索,并且使用 %s 意味着数据以空终止;您可以直接使用 C 函数完成所有这些操作,而无需创建中间对象:

void receiveData(CFSocketRef s, CFSocketCallBackType type,  CFDataRef address, const void *data, void *info)
{
    LoginViewController *lvc = [[LoginViewController alloc] init];

    char *buffer = (char *)CFDataGetBytePtr((CFDataRef)data);

    if (strstr(buffer, "ACPT") == NULL)
        NSLog (@"Invalid Username Or Password");
    else
        [lvc goToWatchList];
}

Your response may be UInt8 but you don't need to copy it into a UInt8[] as your already have it in a CFDataRef (assuming the void * you pass in really is a CFDataRef, if it isn't you have other problems...) - a CFDataRef is toll-free bridged with NSData (i.e. you can just cast from one to the other), and you can construct an NSString directly from the bytes in an NSData. Your code, removing the logging and adding direct NSString conversion:

void receiveData(CFSocketRef s, CFSocketCallBackType type,  CFDataRef address, const void *data, void *info)
{
    LoginViewController *lvc = [[LoginViewController alloc] init];

    NSString *buffer = [[NSString alloc] initWithData:(NSData *)data
                                             encoding:NSUTF8StringEncoding];
    NSRange match = [buffer rangeOfString:@"ACPT"];
    [buffer release];

    if (match.location == NSNotFound)
        NSLog (@"Invalid Username Or Password");
    else
        [lvc goToWatchList];
}

Alternatively as, at least in your sample code: are not saving the converted buffer, are using a literal string for the search, and your use of %s implies the data is null-terminated; you could do all this directly with C functions without creating intermediate objects:

void receiveData(CFSocketRef s, CFSocketCallBackType type,  CFDataRef address, const void *data, void *info)
{
    LoginViewController *lvc = [[LoginViewController alloc] init];

    char *buffer = (char *)CFDataGetBytePtr((CFDataRef)data);

    if (strstr(buffer, "ACPT") == NULL)
        NSLog (@"Invalid Username Or Password");
    else
        [lvc goToWatchList];
}
负佳期 2024-11-09 02:21:24

您在找这个吗?

NSString *string = [[NSString alloc]
    initWithData:(id)data
    encoding:NSUTF8StringEncoding];

Are you looking for this?

NSString *string = [[NSString alloc]
    initWithData:(id)data
    encoding:NSUTF8StringEncoding];
深爱成瘾 2024-11-09 02:21:24

出现警告是因为 rangeOfStringNSString 的方法,而 buffer 不是 NSString。我认为如果你能将数据转换成 NSString 会更好。我认为如果您尝试使用 stringWithData:

UPDATE

请检查此 链接

The warning appears because rangeOfString is a method of NSString and buffer is not an NSString. I think it would be better if you could convert the data into NSString. I think it will work if you try using stringWithData:

UPDATE

Pls check this Link

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文