# Storing a Char array into a CString variable

Page 1 of 1

## 5 Replies - 2660 Views - Last Post: 01 December 2011 - 05:15 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=257968&amp;s=bc11c15ca0c36d1a25fb719db0ac3f20&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 C_Dingo

Reputation: 0
• Posts: 27
• Joined: 04-December 08

# Storing a Char array into a CString variable

Posted 01 December 2011 - 10:58 AM

I've been trying to rewrite some functions for a school project that I did earlier in the year. The program itself simply took Binary, Hex, Decimal, or Octal numbers and converted them to whatever the user chose. I'm mainly having trouble with the function I'm writing to convert from Hex to Binary. Here is the code that I have so far (for reference, I'm using Visual Studio 2008):

```void HexToBinary(CString input, CString& output)
{
// Get length of input and calculate length that the binary number will be
int hex_length = input.GetLength();
int binary_length = hex_length*4;

CString binary_segment;
char binary_segment_array[4];

int binary_start = 0;
int binary_position = 0;

int index = binary_length-1;

// Create a dynamic array where the size is based on the length that the binary number will be
typedef char* arrayPtr;
arrayPtr binary_output;
binary_output = NULL;
binary_output = new char[binary_length];

for(int count=0; count<hex_length; count++)
{
char chr = input[hex_length - 1 - count];
switch(chr)
{
case '0':
binary_segment = "0000";
break;
case '1':
binary_segment = "0001";
break;
case '2':
binary_segment = "0010";
break;
case '3':
binary_segment = "0011";
break;
case '4':
binary_segment = "0100";
break;
case '5':
binary_segment = "0101";
break;
case '6':
binary_segment = "0110";
break;
case '7':
binary_segment = "0111";
break;
case '8':
binary_segment = "1000";
break;
case '9':
binary_segment = "1001";
break;
case 'A':
case 'a':
binary_segment = "1010";
break;
case 'B':
case 'b':
binary_segment = "1011";
break;
case 'C':
case 'c':
binary_segment = "1100";
break;
case 'D':
case 'd':
binary_segment = "1101";
break;
case 'E':
case 'e':
binary_segment = "1110";
break;
case 'F':
case 'f':
binary_segment = "1111";
break;
default:
AfxMessageBox(_T("Invalid Number"));
}
int rev_order = 4;
for(int i=0; i<4; i++)
{
rev_order--;
chr = binary_segment.GetAt(rev_order);
binary_segment_array[i] = chr;
}
binary_position+=4;
int segment_position = 0;
while(binary_start < binary_position)
{
binary_output[binary_start] = binary_segment_array[segment_position];
binary_start++;
segment_position++;
}
}
};

```

Basically what I'm doing is getting the Hex input as a CString and passing it to the function. I get the length of the input and the length that the Binary number will be based off of the Hex input. I then create an array that has a size based on the Binary length. After that I go through a loop which eventually populates the array with the respective Binary number.

Now, my main problem is trying to put the value stored in the array into a CString. In the function definition I have CString& output as a variable being called by reference. I'd like to store the value in the array into the the variable output, but I'm running into a problem there. I've tried a couple ways to do this, mainly going through a loop with something like this just as an example:
```output.SetAt(index, binary_output[i]);

```

The main reason I can see that this doesn't work, is because the CString need to be as long as the array so that it can have all the characters stored in it; otherwise it only stores the first character only.

I apologize if their are any unnecessary lines of code, I haven't really went through to try and clean it up yet since I'm still trying to get it working. Hopefully my description of what I'm trying to accomplish with this function isn't too hard to follow, I tried to describe it as best I could. I appreciate any help.

Is This A Good Question/Topic? 0

## Replies To: Storing a Char array into a CString variable

### #2 r.stiltskin

• D.I.C Lover

Reputation: 2030
• Posts: 5,430
• Joined: 27-December 05

## Re: Storing a Char array into a CString variable

Posted 01 December 2011 - 12:57 PM

How did you define CString? A c-string is a null-terminated char array and as such, there's no reason to be passing a reference -- because the name of the array is a pointer.

But when you call input.GetLength() you seem to be dealing with a class object. So what is it?

And exactly what does "it doesn't work" mean? Error messages? Crashes? Won't compile?

Can you post a small but complete program that illustrates your problem in its simplest form?

### #3 C_Dingo

Reputation: 0
• Posts: 27
• Joined: 04-December 08

## Re: Storing a Char array into a CString variable

Posted 01 December 2011 - 01:29 PM

Okay, well, here is a short program with the code in it. I'm just getting user input from an edit box and sending it to the function. You'll see that here:

```void Cbinary_converstion_testDlg::OnBnClickedConvert()
{
CString m_output, m_hex;

GetDlgItemText(IDC_HEX, m_hex);
HexToBinary(m_hex, m_output);
SetDlgItemText(IDC_OUTPUT, m_output);
}

```

From there the function is the same as the one listed above. I just need a way to put the value stored in the array in the HexToBinary function into the value of output, or if there is another way I can return that value to be set in the output edit box then that will work too. As for "it doesn't work", I just mean that I can't figure out a way to store the value of the array into the output variable. The program itself compiles without a problem, and when I debug and check to see how it is working within the function, everything works fine; I just need a way to store the value of the array into output that way I can get it to display in the edit box.

This post has been edited by C_Dingo: 01 December 2011 - 01:35 PM

### #4 jimblumberg

Reputation: 5451
• Posts: 16,978
• Joined: 25-December 09

## Re: Storing a Char array into a CString variable

Posted 01 December 2011 - 02:08 PM

Quote

How did you define CString?

The CString class is a Microsoft specific class available through the MFC group of classes.

Jim

### #5 r.stiltskin

• D.I.C Lover

Reputation: 2030
• Posts: 5,430
• Joined: 27-December 05

## Re: Storing a Char array into a CString variable

Posted 01 December 2011 - 02:10 PM

It looks like binary_output (the char array that you declare in HexToBinary) should have the correct data. You can verify that by temporarily adding a print statement in that function. The only potential problem that I see there is that binary_output is an ordinary char array. It might be useful to add a null char at the end.

As far as how to copy it into output, that depends on what output actually is. It's a "CString" but a CString isn't a "c-string". Is CString a class that you've written? Or are you using MSDN's CString? I never use that, but I can see that it has an assignment operator so you should be able to do
```output = binary_output;
```
but to do that you will have to null-terminate the char array.

### #6 C_Dingo

Reputation: 0
• Posts: 27
• Joined: 04-December 08

## Re: Storing a Char array into a CString variable

Posted 01 December 2011 - 05:15 PM

The CString was the MSDN CString. I tried adding a null character to the end of the array and it worked perfectly after that. I greatly appreciate all the help everyone offered.