Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

How to convert 16 bit byte to 2 string characters

Posted on 2008-10-29
Medium Priority
1,185 Views
I have a C# app that reads binary data from an app through a C++ import.

The data i am trying to read is held in short integers so i need to split the int16 into its byte pairs and read the values into its two strings.

I do it in VB.net with the following function
Public Shared Function ByteToString(ByVal bytStr As Byte) As String
Dim strCon As String        'hold the string
strCon = Convert.ToChar(bytStr)
'return the string
Return strCon
End Function

But i cannot find the same functionality in C#.

Supposedly this should work but all i store then is the numeric value of the byte
bytByteArr[1].ToString();

Thanks for any help!!
0
Question by:Halon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3
• 3

Author Comment

ID: 22836177

I read in from a byte array 2 bytes at a time
'read the next 2 bytes in

for example first two bytes are 73 and 114 - which are the letters I and r
Then i get 101 and 110 - which are e and n respectively

Hope this helps my clear as mud question
0

LVL 13

Expert Comment

ID: 22836332
If you have a 16-bit integer:

int input = 1234;
int lower = input & 0x00FF;
int upper = (input & 0xFF00) >> 4;
0

LVL 13

Expert Comment

ID: 22836371
Sorry, that should be a shift of 8-bits not 4!

If you have a 16-bit integer:

``````int input = 1234;
int lower = input & 0x00FF;
int upper = (input & 0xFF00) >> 8;
``````
0

LVL 13

Accepted Solution

numberkruncher earned 2000 total points
ID: 22836427
If you have an array of bytes and you want to convert them to a string you can use the following:
``````// You get this from the file.
byte[] bytes = new byte[] { 73, 114 };
// Then convert byte array to string, you can use a different encoding if you need to.
string result = System.Text.Encoding.Default.GetString(bytes);
``````
0

Author Comment

ID: 22841416
numberkruncher, that works great. i think i am taking the long way at getting what i need done but your idea gets my string out of the binary data.

A question though. I know the field i am grabbing can be up to 20 ints long, thats 40 short ints that i grab. At any point there can be null terminators within the data ('\0' i belive is the code from C++ i am grabbing). How can i strip this from my string or toss it as i create it?

Thanks!!
0

Author Comment

ID: 22841747
Its OK, i found it.

//set the char we want to trim, in this case the nulls
char[] trimNulls = { '\0' };

//return the result
return strTextResult.Trim(trimNulls);

Thanks!!
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses
Course of the Month7 days, 23 hours left to enroll