Click here to Skip to main content
15,896,478 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.
C++
#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;
}
Posted
Updated 5-Mar-15 11:07am
v2
Comments
[no name] 5-Mar-15 17:07pm    
I'm not very familiar with C++ but I just spotted this:
switch (TOUPPER(c))
case 'a'
...
Sergey Alexandrovich Kryukov 5-Mar-15 18:18pm    
This is impossible to look at. Please see my answer.
—SA
Sergey Alexandrovich Kryukov 5-Mar-15 17:45pm    
A-aaa! Can I unsee it?
—SA

1 solution

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:
C++
int mask = 1 << bit;

Then calculate bitwise AND with your test number:
C++
int test = value & mask;

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
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900