将char []的数组转换为byte [],反之亦然? C ++ [英] Convert array of char[] to byte[] and vice versa? C++

查看:241
本文介绍了将char []的数组转换为byte [],反之亦然? C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将字符数组转换为字节的最佳方法是什么?

What is the best way to convert an array of chars to bytes and vice versa?

解决方案:

void CharToByte(char* chars, byte* bytes, unsigned int count){
    for(unsigned int i = 0; i < count; i++)
    	bytes[i] = (byte)chars[i];
}

void ByteToChar(byte* bytes, char* chars, unsigned int count){
    for(unsigned int i = 0; i < count; i++)
    	 chars[i] = (char)bytes[i];
}


推荐答案

少数几种具有由ANSI标准保证的大小的类型,并且该大小是1字节。据我所知C不直接定义类型字节。然而,只是没有疯狂的有一个类型命名的字节,事实上不是一个字节的大小。因此,一个简单的施法应该做的伎俩。

The type char is one of the few types that has a size guaranteed by the ANSI standard and that size is 1 byte. As far as I know C does not directly define the type byte. However it would be just short of insane to have a type named byte which is not in fact a byte in size. Therefore a simple cast should do the trick.

这篇关于将char []的数组转换为byte [],反之亦然? C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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