Solved

How to convert 16 bit byte to 2 string characters

Posted on 2008-10-29
6
1,163 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: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
  • Learn & ask questions
  • 3
  • 3
6 Comments
 

Author Comment

by:Halon
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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 13

Accepted Solution

by:
numberkruncher earned 500 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:Halon
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:Halon
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

738 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