将数据帧十六进制转换为二进制 [英] convert data frame hex to binary

查看:267
本文介绍了将数据帧十六进制转换为二进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

晚上好,

我在解码一个帧时遇到了一个小问题,所以我的帧是以十六进制接收的,我希望转换为二进制,然后将她放入二进制变量表中进一步的过程,这是我的代码0错误,当我编译相当正常的整体(0-> 9),但当他收到一个字母(A-> F)发生编译错误,我找不到解决方案错误。

谢谢你的帮助。

good evening,
I encountered a small problem with the decoding of a frame, so my frame is received in hex and I want to convert to binary and then put her in a binary variable table for further process, here is my code 0 error when I compile quite normal with whole (0-> 9), but when he receives a letter (A-> F) a compilation error occurs, I can not find the solution for this error.
thank you for your help.

#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <bitset>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h> 
using namespace std;


const char* hex2bin(char c)
{
	
	switch (toupper(c))
	{
	case '0': return "0000";
	case '1': return "0001";
	case '2': return "0010";
	case '3': return "0011";
	case '4': return "0100";
	case '5': return "0101";
	case '6': return "0110";
	case '7': return "0111";
	case '8': return "1000";
	case '9': return "1001";
	case 'a': return "1010";
	case 'b': return "1011";
	case 'c': return "1100";
	case 'd': return "1101";
	case 'e': return "1110";
	case 'f': return "1111";
	}
}
std::string hex_to_bin( std::string& hex)
{
	
	std::string bin;
	for (unsigned i = 0; i <8; ++i)
		bin += hex2bin(hex[i]);
	return bin ;
}

std::string dfg(const std::string resp[10])
{
	std::string  RS;
	std::string a;
	std::string tab[16];
	for (int i = 4; i < 8; ++i)
		RS += resp[i];

	a = hex_to_bin(RS);
	for (int i = 0; i != a.length(); ++i)
	{
		tab[i] = a[i];
	}
	StatusFlag();
	for (int i = 0; i < 16; i++)
	{
		if (tab[i] == "1")
			status[i].statu = true;
		else
			status[i].statu = false;
	}
	return RS;
}

int main()
{
	std::string b;
	std::string resp[10] = { "25", "9f", "63", " 25", "36", "42", "15", "EC", "5b", "a9" };
	b = dfg(resp);
	return 0;
}

推荐答案

此代码示例根本不是编程,是相反的。我不知道为什么你会使用所有十六进制和二进制,它们只能是字符串,而不是数字。我不确定你真的需要使用字符串。



这是创建二进制字符串的想法。执行一个从0到 NumberOfBits - 1 的循环,比方说,位数是。对于每个位,创建一个位掩码。为简单起见,我们假设您使用数字(不是蜇,实际上,您的签名功能没有实际意义)。假设你使用 int

This code sample is not programming at all, is something opposite. I have no idea why would you work with all that "hex" and "binary", which can be only strings, not numbers. I'm not sure you really need to work with strings.

Here is the idea of creation of "binary string". Execute a loop from 0 to NumberOfBits − 1, let's say, bit number is bit. For each bit, create a bit mask. For simplicity, let's assume you work with number (not stings, indeed, the function of your signature makes no practical sense). Let's assume you work with int:
int mask = 1 << bit;



然后使用您的测试编号计算按位AND:


Then calculate bitwise AND with your test number:

int test = value & mask;



此测试将返回零或不返回。如果为零,则位置中的测试位未设置(清除,0),如果它不为零,则设置为(1)。根据这一点,将char'0'或'1'写入输出。



请参阅:http://www.cplusplus.com/doc/tutorial/operators [ ^ ]。



始终使用数字,而不是代表数字的字符串。你不应该想要将十六进制字符串转换为二进制字符串。您可以先解析十六进制字符串为数字(不能是十六进制或十进制)。有很多方法可以做到这一点。其中一个可以基于使用流: http://stackoverflow.com/ questions / 1070497 / c-convert-hex-string-to-signed-integer [ ^ ]。



-SA


This test will return either zero or not. If it is zero, the test bit in position bit is not set (clear, 0), if it is non-zero, it is set (1). Depending on that, write the char '0' or '1' to output.

Please see: http://www.cplusplus.com/doc/tutorial/operators[^].

Always work with numbers, not strings representing numbers. You should not "want" to "convert" "hex string" to "binary string". You can parse "hex string" into number (which cannot be "hex" or "decimal") first. There can be many ways to do it. One of them can be based on using streams: http://stackoverflow.com/questions/1070497/c-convert-hex-string-to-signed-integer[^].

—SA


这篇关于将数据帧十六进制转换为二进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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