Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to convert 16 bit byte to 2 string characters

Posted on 2008-10-29
6
Medium Priority
?
1,187 Views
Last Modified: 2012-05-05
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
Comment
Question by:Winston Smith
  • 3
  • 3
6 Comments
 

Author Comment

by:Winston Smith
ID: 22836177
Little more info

I read in from a byte array 2 bytes at a time
'read the next 2 bytes in                  
                bytByteArr = brContents.ReadBytes(2);

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

by:numberkruncher
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

by:numberkruncher
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;

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 13

Accepted Solution

by:
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);

Open in new window

0
 

Author Comment

by:Winston Smith
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

by:Winston Smith
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question