尝试将二进制值转换为ascii值
c++ cli - Visual C++ : trying to convert binary to ascii value
我有二进制到ASCII转换的问题我所做的是我有文本框数组,我试图将二进制值放入文本框并将这些值传递给如下所述的函数:
System::String^ conversion(const char* input)
{
int length = strlen(input); //get length of string
int binary[8]; //array used to store 1 byte of binary number (1 character)
int asciiNum = 0; //the ascii number after conversion from binary
System::String^ ascii; //the ascii character itself
int z = 0; //counter used
for(int x = 0; x < length / 8; x++) //reading in bytes. total characters
{
for(int a = 0; a < 8; a++) //store info into binary[0] through binary[7]
{
binary[a] = (int) input[z] - 48; //z never resets
z++;
}
int power[8]; //will set powers of 2 in an array
int counter = 7; //power starts at 2^0, ends at 2^7
for(int x = 0; x < 8; x++)
{
power[x] = counter; //power[] = {7, 6, 5, ..... 1, 0}
counter--; //decrement counter each time
}
for(int y = 0; y < 8; y++) //will compute asciiNum
{
double a = binary[y]; //store the element from binary[] as "a"
double b = power[y]; //store the lement from power[] as "b"
asciiNum += a* pow(2, b);
}
ascii = System::Convert::ToString(asciiNum);
asciiNum = 0; //reset asciiNum for next loop
return ascii;
}
}
问题是我只得到ASCII值,不能得到相关字符。我想要那个角色,谁来帮我。
char asciichar=(unsigned char) asciinum;
这将把你的数字转换为ascii字符,你可以返回结果为char
而不是std::string
可以将整数作为字符打印。除了使用ToString,您还可以将整数重新转换为字符:
(char)asciiNum
的例子:
#include <iostream.h>
using namespace std;
int main ()
{
int beta=100;
cout<<"Value of integer is: "<<beta<<endl;
cout <<"Value of char is: "<<(char) beta<<endl;
}
结果:
Value of integer is : 100
Value of char is : d
相关文章:
- 在C++中使用 ASCII 代码将输入从小写转换为大写
- 我的程序将 26 个字母转换为 ascII 没有显示正确答案
- 我写的 ASCII 到二进制转换器C++向后显示二进制,如何使其正确显示?
- 我正在尝试编写将 ASCII 转换为十进制、添加并转换回 ASCII 的软件
- C++-将ASCII字符数组中可能的数值转换为字符的最简单方法
- 如何将ASCII数字转换为从文件中提取的整数
- 将非 ASCII 字符转换为英语对应字符C++
- 在Qt中将QString转换为ASCII值,反之亦然
- 输出使用 cout 转换为 ASCII 的文本时输出格式不正确,C++
- 如何在不将其转换为 ASCII 字符的情况下显示 QByteArray 的十六进制值
- 如何在 c++ 中将字节转换为 ASCII 十六进制表示形式?
- 如何使用 ASCII 转换使用字符堆栈计算后缀表达式
- 将 ASCII 文件转换为 C++ 的二进制格式
- 将字符转换为ASCII返回数字太大
- MFC Cedit将非ASCII字符转换为ASCII
- 使用 c++ 是否可以将 ASCII 字符转换为十六进制?
- 如何将字符串转换为 ASCII 值的总和
- 将句子转换为C 中的ASCII代码
- 在整数中转换ASCII文件列
- 如何将字符数转换为十进制并返回或转换ASCII'A'-'Z'/'a'-'