473,395 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

String Char and Bitstrings Conversion Help

Hello
I am having trouble figuring out how to take a string input and ultimately convert each letter of the string into binary then print out the binary results then convert from binary back to a string. I have searched on how to convert from string to bitstring but have not been successful.

Can anyone give me a hand here?

Thanks
Mar 26 '08 #1
4 1057
gpraghuram
1,275 Expert 1GB
Hello
I am having trouble figuring out how to take a string input and ultimately convert each letter of the string into binary then print out the binary results then convert from binary back to a string. I have searched on how to convert from string to bitstring but have not been successful.

Can anyone give me a hand here?

Thanks
Check whether the following idea works.

1)Read every character
2)Get the ASCII value of it.
3)get the binary value for the ASCII value
4)print it
5)Do the reverse to convert the binary to string

One thing you should take acre is always ensure that every binary number is of caertain number of bits in it that is 16 bits or 32 bits

raghuram
Mar 27 '08 #2
cjpos
1
Sorry - this should be in .NET forum ...

I had the same problem in recalling a MD-5 hashed password field that is binary(16) in the database. Unless I was resetting the password, I needed to write the original back to the database. Here is what I created:

Expand|Select|Wrap|Line Numbers
  1.     ''' <summary>
  2.     ''' Function to convert binary data to it's hexadecimal string representation.
  3.     ''' Useful for saving binary database values as a string.
  4.     ''' </summary>
  5.     ''' <param name="ByteVal">A byte array</param>
  6.     ''' <returns>String - 2 character for every byte, i.e. byte(0)= decimal 80 returns hex 50</returns>
  7.     Public Shared Function BinaryToString(ByVal ByteVal As Byte()) As String
  8.         Dim sValue As String = ""
  9.         Dim xElement As Integer
  10.         For xElement = 0 To (UBound(ByteVal))
  11.             sValue = sValue & Right("00" & Convert.ToString(ByteVal(xElement), 16).ToUpper(), 2) 
  12.           '  hexadecimal is base 16, parameter of Convert function
  13.         Next
  14.         Return sValue
  15.     End Function
  16.  
  17.     ''' <summary>
  18.     ''' Function to convert a hexadecimal string representation to binary data.
  19.     ''' Useful for converting binary database values represented as a string back to binary.
  20.     ''' </summary>
  21.     ''' <param name="StringVal">A hexadecimal string, ex. DA hex</param>
  22.     ''' <returns>binary byte() array, i.e. hex DA returns a decimal 218</returns>
  23.     Public Shared Function StringToBinary(ByVal StringVal As String) As Byte()
  24.         Dim iStringLength As Integer = 0
  25.         Dim X As Integer = 0
  26.         iStringLength = (StringVal.Length / 2)
  27.         Dim DataBytes As Byte() = New Byte(iStringLength - 1) {}
  28.         Dim dbyte As Byte
  29.         Dim sOut As String
  30.  
  31.         For X = 0 To (iStringLength - 1)
  32.             sOut = StringVal.Substring((X * 2), 2)
  33.             dbyte = Convert.ToByte(sOut, 16)
  34.             DataBytes(X) = dbyte
  35.         Next
  36.         Return DataBytes
  37.     End Function
  38.  
  39.  
Apr 2 '08 #3
weaknessforcats
9,208 Expert Mod 8TB
Moving this to the .NET forum.
Apr 2 '08 #4
Plater
7,872 Expert 4TB
Now, by "binary data" do you mean their ascii values?
With the ASCII encoding, you can just go from char to byte.

Expand|Select|Wrap|Line Numbers
  1. string mystr="Alan is going far.";
  2. char c = mystr[0];
  3. int cval= (int)c;
  4. byte bval = (byte)c;
  5. //cval now = 65, which is the ascii value
  6. //bval also now = 65
  7.  
Apr 2 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Marcin Kalicinski | last post by:
Why string literals are regarded as char * not as const char *? (1) void f(char *); (2) void f(const char *); f("foo") will call version (1) of function f. I understand that the exact type...
7
by: john | last post by:
On my form i have a message box called txtItemDesc that displays the french phrase qualité Père Noël. Now then when i run this code on that text box: Dim chrArr() As Char chrArr =...
12
by: ABeck | last post by:
Hello List, I have ar more or less academical question. Can there arise runtime errors in a program, if the include of <string.h> has been forgotten? If all the arguments to the functions of...
11
by: Zordiac | last post by:
How do I dynamically populate a string array? I hope there is something obvious that I'm missing here Option Strict On dim s() as string dim sTmp as string = "test" dim i as integer ...
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
4
by: costantinos | last post by:
Hello. I have a string which looks like and I need to read these numbers. The logical implementation was to read the string char by char and check whether I have a space and when I find one I...
3
by: ryan.gilfether | last post by:
I have a problem that I have been fighting for a while and haven't found a good solution for. Forgive me, but my C++ is really rusty. I have a custom config file class: class ConfigFileValue...
3
by: Kevin Frey | last post by:
I am porting Managed C++ code from VS2003 to VS2005. Therefore adopting the new C++/CLI syntax rather than /clr:oldSyntax. Much of our managed code is concerned with interfacing to native C++...
8
by: drjay1627 | last post by:
hello, This is my 1st post here! *welcome drjay* Thanks! I look answering questions and getting answers to other! Now that we got that out of the way. I'm trying to read in a string and...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.